Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell replace cr\lf by comma

I have input.txt

1 2 3 4 5 

I need to get such output.txt

1,2,3,4,5 

How to do it?

like image 471
vinnitu Avatar asked Jan 04 '11 13:01

vinnitu


People also ask

How do you replace a line with a comma in Unix?

Example 1: Replace \n with a comma using -zThe `sed` command will convert the newline into the null character and replace each \n with a comma by using the first search and replace pattern. Here, 'g' is used to globally search for \n. With the second search and replace pattern, the last comma will be replaced with \n.

What is carriage return shell?

The carriage return, also referred to as Ctrl+M, character would show up as an octal 15 if you were looking at the file with an od octal dump) command. The characters CRLF are often used to represent the carriage return and linefeed sequence that ends lines on Windows text files.


2 Answers

Try this:

tr '\n' ',' < input.txt > output.txt 
like image 99
eumiro Avatar answered Sep 19 '22 16:09

eumiro


With sed, you could use:

sed -e 'H;${x;s/\n/,/g;s/^,//;p;};d' 

The H appends the pattern space to the hold space (saving the current line in the hold space). The ${...} surrounds actions that apply to the last line only. Those actions are: x swap hold and pattern space; s/\n/,/g substitute embedded newlines with commas; s/^,// delete the leading comma (there's a newline at the start of the hold space); and p print. The d deletes the pattern space - no printing.

You could also use, therefore:

sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' 

The -n suppresses default printing so the final d is no longer needed.

This solution assumes that the CRLF line endings are the local native line ending (so you are working on DOS) and that sed will therefore generate the local native line ending in the print operation. If you have DOS-format input but want Unix-format (LF only) output, then you have to work a bit harder - but you also need to stipulate this explicitly in the question.

It worked OK for me on MacOS X 10.6.5 with the numbers 1..5, and 1..50, and 1..5000 (23,893 characters in the single line of output); I'm not sure that I'd want to push it any harder than that.

like image 23
Jonathan Leffler Avatar answered Sep 21 '22 16:09

Jonathan Leffler