15

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

user271785
  • 415
  • 1
  • 4
  • 13
  • Related: http://stackoverflow.com/questions/8739427/bash-add-string-to-the-end-of-the-file-without-line-break – redolent Dec 01 '14 at 21:36

3 Answers3

25

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)

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
2

Using only one command

url=$(awk '{printf "%s",$0}' eg*)
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

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."

Andy Lester
  • 91,102
  • 13
  • 100
  • 152