Shell scripting:from text file to CSV

Hello friends,
I have a file as follows:

"empty line" 
content1 
content2 
content3

content1 
content2 
content3  

content1 
content2 
content3

It starts with an empty line,

how can i get a csv like this:

content1,content2,content3 
content1,content2,content3 
content1,content2,content3

many thanks for your attention and big help.

awk -F"\n" 'BEGIN{RS=""; OFS=",";} {print $1,$2,$3}' file
1 Like

Did you try the hint in http://www.unix.com/302820927-post10.html?

1 Like

Another approach:

xargs -n3 < file | tr ' ' ','
1 Like

Many thanks,

awk -F"\n" 'BEGIN{RS=""; OFS=",";} {print $1,$2,$3}' file

please, can you explain a bit your code, i�d like to learn how to use AWK for this kind of tasks!

---------- Post updated at 10:11 AM ---------- Previous update was at 10:07 AM ----------

For Yoda, your code doesn't works, anyway many thanks!

Try also:

awk  '{$1=$1}1' RS="" OFS="," file
1 Like

I don't know why, but it works for me:

$ cat file

content1
content2
content3

content1
content2
content3

content1
content2
content3
$ xargs -n3 < file | tr ' ' ','
content1,content2,content3
content1,content2,content3
content1,content2,content3
1 Like

Many thanks RudiC,

and for a normal file like this:

content1
content2
content1
content2
content1
content2
content1
content2

thanks

paste -s -d",,\n" file
content1,content2,content3
content1,content2,content3 
content1,content2,content3
1 Like

hello rudiC,

somthing like this doesn't works

awk -F -s -d",,\n" 'BEGIN{RS=""; OFS=",";} {print $1,$2}'

?

thnaks

$ cat file | xargs -n 2 echo | tr " " ","
content1,content2
content1,content2
content1,content2
content1,content2
$ cat file | xargs -n 3 echo | tr " " ","
content1,content2,content1
content2,content1,content2
content1,content2
1 Like

Another paste approach:

paste -d, - - < file

Another awk approach:

awk 'NR%2{ORS=OFS}!(NR%2){ORS=RS}1' OFS=, file
1 Like

No. Try

awk 'NR%2 {x=$1;next} {print x","$1}' file
content1,content2
content1,content2
content1,content2
content1,content2

And, in my other post, remove one comma from -d",,\n" . Sorry, should have read your question more carefully.

1 Like