bash cat multiple files content in to single string without newlines

大憨熊 提交于 2019-11-30 01:25:23

问题


i got some files with name start as eg_. and only each contains one single line

eg_01.txt: @china:129.00

eg_02.txt @uk:219.98

eg_03.txt @USA:341.90

......

i am expecting to cat them in to a single line to send by URL like: @china:129.00@uk:219.98@USA:341.90

i use echo cat eg_*

it give me the output look like a string, but it actually contains new line: "@china:129.00

@uk:219.98 @USA:341.90"

is there any other way i can construct that string which expected and get rid of new line and even the space? is only cat enough to do this?

thanks in advance


回答1:


You could always pipe it to tr

tr "\n" " "

That removes all newlines on stdin and replaces them with spaces

EDIT: as suggested by Bart Sas, you could also remove newlines with tr -d

tr -d "\n"

(note: just specifying an empty string to tr for the second argument won't do)




回答2:


Using only one command

url=$(awk '{printf "%s",$0}' eg*)



回答3:


In Perl, you'd do it like this:

perl -pe'chomp' eg*.txt

The -p says "loop through the input file and do whatever code is specified by the -e switch. The chomp in Perl says "Remove any trailing newlines."



来源:https://stackoverflow.com/questions/3936738/bash-cat-multiple-files-content-in-to-single-string-without-newlines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!