Print in next line after coma

Hi,

I have a file like seen below.

apple, orange, grapes
pineapple
tomato, potato
egg

I need a output like

apple
orange
grapes
pineapple
tomato
potato
egg

Please help.

Hello jayadanabalan,

Following may help you in same for provided input.

awk '{gsub(/, /,"\n",$0);} 1'  Input_file

Output will be as follows.

apple
orange
grapes
pineapple
tomato
potato
egg

Thanks,
R. Singh

$ cat infile
apple, orange, grapes
pineapple
tomato, potato
egg
$ awk  '1' RS=",|, " infile
apple
orange
grapes
pineapple
tomato
potato
egg
$ awk  '{$1=$1}1' FS=",|, "  OFS="\n" infile
apple
orange
grapes
pineapple
tomato
potato
egg

Note: RS may only contain a regex in gawk and mawk. In regular awk only the first character is used.

Although the -s (squeeze) option is said to work on the input set only, this seems to work:

tr -s ', ' '\n' < file
1 Like

My choice would be sed, but the 'tr -s' solution is neat if you have it.

sed '
  s/, */\
/g
 ' in_file >out_file

Nice, it needs a small adaptation for it to work on all systems (for example HP-UX, AIX):

tr -s ', ' '[\n*]' < file

or

tr -s ', ' '\n\n' < file

---
-s does not only work on the input set:

tr: Extended Description

Some versions or 'tr' insist on char sets in and out in square brackets, probably a POSIX thing. You could 'tr' both ' ' and ',' to '\n' or 'tr' ',' to '\n' and then "tr -d ' '". The 'tr' is very fast, tolerant of long lines and large files, being entirely char by char.

tr -d '[ ]' < in_file | tr '[,]' '[\n]' > out_file

That was for ranges, not for enumerated strings of characters..

tr: rationale

In fact, using brackets that way will also delete square brackets:

$  echo 'x[  ]x' | tr -d '[ ]'
xx