Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash cat multiple files content in to single string without newlines

Tags:

file

bash

cat

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

like image 596
user271785 Avatar asked Oct 14 '10 19:10

user271785


People also ask

How does cat << EOF work in bash?

The EOF operator is used in many programming languages. This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

How do you echo without newline?

Standard usage of echo If one wants to exclude the trailing newline character, the -n option can be passed, as in: echo -n "no trailing newline" .

What is cat EOF in shell script?

cat is a bash command used to read, display, or concatenate the contents of a file, while EOF stands for End Of File . The EOF is an indication to the shell that the file that was being read has ended.


2 Answers

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)

like image 102
Daniel DiPaolo Avatar answered Oct 21 '22 10:10

Daniel DiPaolo


Using only one command

url=$(awk '{printf "%s",$0}' eg*)
like image 30
ghostdog74 Avatar answered Oct 21 '22 08:10

ghostdog74