Bash 编辑数据删除换行符并将所有内容放在一行中

Bash 编辑数据删除换行符并将所有内容放在一行中,bash,shell,sed,awk,Bash,Shell,Sed,Awk,您好,我是一名新的shell脚本编写人员,我无法做到这一点: 我的数据如下(实际上要大得多): 注意:每50个字符后会有一个换行符,但当数据结束并有一个新的示例名称时,换行符有时会更少 我希望在每50个字符之后,换行符将被删除,因此我的数据如下所示: >SampleName_ZN189A 0100000100000000000010001110000000011100000000100000110000100000000000010000000000001100000010000000

您好,我是一名新的shell脚本编写人员,我无法做到这一点:

我的数据如下(实际上要大得多):

注意:每50个字符后会有一个换行符,但当数据结束并有一个新的示例名称时,换行符有时会更少

我希望在每50个字符之后,换行符将被删除,因此我的数据如下所示:

 >SampleName_ZN189A
 0100000100000000000010001110000000011100000000100000110000100000000000010000000000001100000010000000...
 >SampleName_ZN189B
 0011000000110100000101110000000000000000000001000100010000000000000010010000000000100100000001000000...
我尝试使用tr,但出现了一个错误:

tr '\n' '' < my_file

tr: empty string2
tr'\n'
提前感谢

试试这个

cat SampleName_ZN189A | tr -d '\r'
# tr -d deletes the given/specified character from the input
使用简单的awk,同样可以实现

 awk 'BEGIN{ORS=""} {print}' SampleName_ZN189A #Output doesn't contains an carriage return
 at the end, If u want an line break at the end this works.

 awk 'BEGIN{ORS=""} {print}END{print "\r"}' SampleName_ZN189A
 # select the correct line break charachter (i.e) \r (or) \n (\r\n) depends upon the file format.
试试这个

cat SampleName_ZN189A | tr -d '\r'
# tr -d deletes the given/specified character from the input
使用简单的awk,同样可以实现

 awk 'BEGIN{ORS=""} {print}' SampleName_ZN189A #Output doesn't contains an carriage return
 at the end, If u want an line break at the end this works.

 awk 'BEGIN{ORS=""} {print}END{print "\r"}' SampleName_ZN189A
 # select the correct line break charachter (i.e) \r (or) \n (\r\n) depends upon the file format.
您可以使用此awk:

awk '/^ *>/{if (s) print s; print; s="";next} {s=s $0;next} END {print s}' file

>SampleName_ZN189A
010000010000000000001000111000000001110000000010000011000010000000000001000000000000110000001000000000110000000000001110000010010011111000000100010000000001100000010000000101000000000100000010000011100011
>SampleName_ZN189B
001100000011010000010111000000000000000000000100010001000000000000001001000000000010010000000100000000000000000000000000000010000000000010111010000000010001100000001100000010100100000011111101010000001100
您可以使用此awk:

awk '/^ *>/{if (s) print s; print; s="";next} {s=s $0;next} END {print s}' file

>SampleName_ZN189A
010000010000000000001000111000000001110000000010000011000010000000000001000000000000110000001000000000110000000000001110000010010011111000000100010000000001100000010000000101000000000100000010000011100011
>SampleName_ZN189B
001100000011010000010111000000000000000000000100010001000000000000001001000000000010010000000100000000000000000000000000000010000000000010111010000000010001100000001100000010100100000011111101010000001100
带“-d”的tr删除指定字符

$ cat input.txt
00110000001101000001011100000000000000000000010001
00010000000000000010010000000000100100000001000000
00000000000000000000000010000000000010111010000000
01000110000000110000001010010000001111110101000000
1100
$ cat input.txt | tr -d "\n"
001100000011010000010111000000000000000000000100010001000000000000001001000000000010010000000100000000000000000000000000000010000000000010111010000000010001100000001100000010100100000011111101010000001100
带“-d”的tr删除指定字符

$ cat input.txt
00110000001101000001011100000000000000000000010001
00010000000000000010010000000000100100000001000000
00000000000000000000000010000000000010111010000000
01000110000000110000001010010000001111110101000000
1100
$ cat input.txt | tr -d "\n"
001100000011010000010111000000000000000000000100010001000000000000001001000000000010010000000100000000000000000000000000000010000000000010111010000000010001100000001100000010100100000011111101010000001100

您可以使用此
sed

sed '/^>Sample/!{ :loop; N; /\n>Sample/{n}; s/\n//; b loop; }' file.txt

您可以使用此
sed

sed '/^>Sample/!{ :loop; N; /\n>Sample/{n}; s/\n//; b loop; }' file.txt
使用awk

awk '/>/{print (NR==1)?$0:RS $0;next}{printf $0}' file
如果你不关心结果,在第一行有额外的新行,这里是一个较短的

awk '{printf (/>/?RS $0 RS:$0)}' file
使用awk

awk '/>/{print (NR==1)?$0:RS $0;next}{printf $0}' file
如果你不关心结果,在第一行有额外的新行,这里是一个较短的

awk '{printf (/>/?RS $0 RS:$0)}' file
这可能适用于您(GNU-sed):

在保留空间中建立记录,当遇到下一条记录的开始或文件的结束时,删除换行符并打印出来

这可能适合您(GNU-sed):


在保留空间中建立记录,当遇到下一条记录的开始或文件的结束时,删除换行符并打印出来

它不起作用,而且我在每个文件上都有很多相似名称的文件。我试过了,效果很好,没有收到你以前的评论。它不起作用,而且我在每个文件上都有很多相似名称的文件。我试过了,效果很好,没有收到你以前的评论。太棒了!它起作用了,但我的名字实际上与SampleName非常不同,它们有字母、数字和“\ux”,有没有办法叫awk来识别所有这些字符而不是写SampleName?它们都是从
字符开始的?检查更新的答案,希望它对你有用。我刚刚检查过,它们都以>开头,只有字母和数字,例如:>T11A、>BerT9A、>8AJ136A。谢谢你!它起作用了,但我的名字实际上与SampleName非常不同,它们有字母、数字和“\ux”,有没有办法叫awk来识别所有这些字符而不是写SampleName?它们都是从
字符开始的?检查更新的答案,希望它对你有用。我刚刚检查过,它们都以>开头,只有字母和数字,例如:>T11A、>BerT9A、>8AJ136A。谢谢