views:

122

answers:

9

Hey there everyone ... I have the following data, and I need to put it all into one line...

I have this:

22791

;

14336

;

22821

;

34653

;

21491

;

25522

;

33238

;

I need this: 22791;14336;22821;34653;21491;25522;33238;

If anyone could help me it would be a great !!

Thanks in advance!

--------- EDIT

No of these commands is working perfectly...

most of them let the data like this:

22791

;14336

;22821

;34653

;21491

;25522

Thanks for the help. But if someone can solve this problem it's going to be really good!

Solved:

cat inputFile | tr -d "\n\r" > outputFile

+1  A: 
tr -d '\n' < file.txt

Or

awk '{ printf "%s", $0 }' file.txt

Or

sed ':a;N;$!ba;s/\n//g' file.txt

This page here has a bunch of other methods to remove newlines.

edited to remove feline abuse :)

Vivin Paliath
Correct, but feline abuse.
Tyler McHenry
Never heard that term before. Thanks for pointing it out :)
Vivin Paliath
+4  A: 
tr -d '\n' < yourfile.txt

Edit:

If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I would expect the Perl solutions to work even in that case)?

Try:

tr -d "\n\r" < yourfile.txt

If that doesn't work then you're going to have to inspect your file more closely (e.g. in a hex editor) to find out what characters are actually in there that you want to remove.

Tyler McHenry
This last one works !! thanks...These data came from an apache log file !... Actually I don't know if it was generated on Windows... my uni's teacher gave to me for doing a homework !
Alucard
I hope removing those newlines wasn't your actual homework!
Lars Haugseth
A: 
$ perl -0777 -pe 's/\n+//g' input >output
$ perl -0777 -pe 'tr/\n//d' input >output
Greg Bacon
A: 
paste -sd "" file.txt
Amardeep
A: 

I would do it with awk, e.g.

awk '/[0-9]+/ { a = a $0 ";" } END { print a }' file.txt

(a disadvantage is that a is "accumulated" in memory).

EDIT

Forgot about printf! So also

awk '/[0-9]+/ { printf "%s;", $0 }' file.txt

or likely better, what it was already given in the other ans using awk.

ShinTakezou
A: 

If the data is in file.txt, then:

echo $(<file.txt) | tr -d ' '

The '$(<file.txt)' reads the file and gives the contents as a series of words which 'echo' then echoes with a space between them. The 'tr' command then deletes any spaces:

22791;14336;22821;34653;21491;25522;33238;
Jonathan Leffler
A: 

Using man 1 ed:

# cf. http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed 
ed -s file <<< $'1,$j\n,p'  # print to stdout 
ed -s file <<< $'1,$j\nwq'  # in-place edit
carlmund
A: 
perl -p -i -e 's/[\n\r\R\Zl]//g;' filename

Must do the job.

ZyX
A: 
sed 's/\n//g' file
Emacs