Write first 10 characters of each line to new file using bash

Ziska220

I apologize for using code designation with my file contents but the formatting was being weird otherwise.

I have a 6,6494,940 line file that contains the following:

@MSQ-M01247R:81:000000000-ACWD8:1:1101:11811:1088 2:N:0:
CCCCCTCTTCCCTTTCTTCCCCCCTCTTTCTTCTTTCTCTTTTCTCCCTCTCCTTTTTTTCTCCTTTTTTTCCTTT
+
############################################################################

I want to write the first 10 characters of each line to a new file in order:

@MSQ-M0124
CCCCCTCTTC
+
##########

I used the following bash script:

while read line
do
        long=$line
        short=${long:0:10}
        echo ${short} 
done < $1

With the following command:

./bashscript.sh fileread.fastq >> filewrite.fastq

Results

Something went wrong. My new/write file had 628,429,568 but it should have had 66,494,940 like the original/read file. So it looks like it kept looping.

When I use the head command on the new/write file I get:

@MSQ-M0124
CCCCCTCTTC
+
##########
@MSQ-M0124
CCTCCTCCTT
+
##########
@MSQ-M0124
CCTTCTTCTT

When I use the tail command I get:

CCCCCGGGGG
+
CCCCCGGGGG
GAGTCGTCTG
+
CCCCCGGGGG
+
CCCCCGGGGG
GAGTCGTCTG
+
CCCCCGGGGG
+
CCCCCGGGGG
GAGTCGTCTG
+
fedorqui 'SO stop harming'

cut is your friend!

$ cut -c-10 file
@MSQ-M0124
CCCCCTCTTC
+
##########

This uses cut with the option -c, from characters, to get the range -10, which implicitly means from the 1st to the 10th.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Bash - pair each line of file

분류에서Dev

Grep last N characters of each line in a file

분류에서Dev

remove the first 15 characters from every other line in a file

분류에서Dev

Printing and deleting the first line of a file using `sed`

분류에서Dev

C - Using strtok gives me only the first word of each line?

분류에서Dev

remove first line in bash

분류에서Dev

Read file using urllib and write adding extra characters

분류에서Dev

Write bash output to file while using here document

분류에서Dev

What command(s) will feed a tab-delimited text file and cut each line to 80 characters?

분류에서Dev

How to read lines of a text file with specific word to an array and get the first four characters of the last line in that array?

분류에서Dev

What is an efficient BASH one-liner for adding line every 130 characters to a file without lines?

분류에서Dev

Extract specific characters from each line

분류에서Dev

How to Rename Multiple Files With Their First 10 Characters?

분류에서Dev

Find a string in a file and then find the first line above containing another string with bash

분류에서Dev

How to skip first element in each line of matrix?

분류에서Dev

How to add new line in a file

분류에서Dev

Bash: Path or link to a line in a file?

분류에서Dev

Move each Cell after column A on new line

분류에서Dev

New Terminal bash file error

분류에서Dev

File read and write while reading the file line by line

분류에서Dev

How to do data analysis using Python, of a file with thousands of dictionaries in each line

분류에서Dev

Python won't rewrite file with first line removed using readlines() and for loop

분류에서Dev

merge two files, first line from first file followed by first line from second file

분류에서Dev

remove characters from a list and add a line break from each element

분류에서Dev

Write hexadecimal values to binary file with bash

분류에서Dev

Frequency of specific symbols on each line of a text file

분류에서Dev

Number of same line in each text file

분류에서Dev

Importing a CSV file into postgres - skip the first line

분류에서Dev

Android TextView - Add A New Line After Every 20 Characters

Related 관련 기사

  1. 1

    Bash - pair each line of file

  2. 2

    Grep last N characters of each line in a file

  3. 3

    remove the first 15 characters from every other line in a file

  4. 4

    Printing and deleting the first line of a file using `sed`

  5. 5

    C - Using strtok gives me only the first word of each line?

  6. 6

    remove first line in bash

  7. 7

    Read file using urllib and write adding extra characters

  8. 8

    Write bash output to file while using here document

  9. 9

    What command(s) will feed a tab-delimited text file and cut each line to 80 characters?

  10. 10

    How to read lines of a text file with specific word to an array and get the first four characters of the last line in that array?

  11. 11

    What is an efficient BASH one-liner for adding line every 130 characters to a file without lines?

  12. 12

    Extract specific characters from each line

  13. 13

    How to Rename Multiple Files With Their First 10 Characters?

  14. 14

    Find a string in a file and then find the first line above containing another string with bash

  15. 15

    How to skip first element in each line of matrix?

  16. 16

    How to add new line in a file

  17. 17

    Bash: Path or link to a line in a file?

  18. 18

    Move each Cell after column A on new line

  19. 19

    New Terminal bash file error

  20. 20

    File read and write while reading the file line by line

  21. 21

    How to do data analysis using Python, of a file with thousands of dictionaries in each line

  22. 22

    Python won't rewrite file with first line removed using readlines() and for loop

  23. 23

    merge two files, first line from first file followed by first line from second file

  24. 24

    remove characters from a list and add a line break from each element

  25. 25

    Write hexadecimal values to binary file with bash

  26. 26

    Frequency of specific symbols on each line of a text file

  27. 27

    Number of same line in each text file

  28. 28

    Importing a CSV file into postgres - skip the first line

  29. 29

    Android TextView - Add A New Line After Every 20 Characters

뜨겁다태그

보관