1860526 Members
1626 Online
110411 Solutions
New Discussion

Re: shell script

 
wanaka_1
Occasional Advisor

shell script

Hi,

I need to compile a mailing list from multiple line to one single line

From multiple line
[email protected]
[email protected]
[email protected]

to single line
[email protected],[email protected],[email protected]

Of course I can write the output to a file and do a while loop. Is there anyway I can do it in one single line of command
7 REPLIES 7
RAC_1
Honored Contributor

Re: shell script

tr '\n' ',' < your_file
There is no substitute to HARDWORK
Kenan Erdey
Honored Contributor

Re: shell script

hi;

awk '{printf "%s,",$0}' file_name > out_file
Computers have lots of memory but no imagination
wanaka_1
Occasional Advisor

Re: shell script

The method is ok but the kena script will print the last one end with ','.
Peter Godron
Honored Contributor

Re: shell script

Hi,
how about:
paste -s -d"," filename | sed "$ s/,$//"
Peter Nikitka
Honored Contributor

Re: shell script

Hi,

so you need something more awk:
awk 'last {printf last","};{last=$0};END {print last}' filename

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
H.Merijn Brand (procura
Honored Contributor

Re: shell script

Soooo many ways to do that

These two will not add a trailing comma

# perl -nle'push@x,$_}END{chomp@x;print join","@x' file

# perl -le'BEGIN{$/=undef}chomp(@x=<>);print join",",@x' file

The good tr example and this short command will

# perl -pe's/\n/,/' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Godron
Honored Contributor

Re: shell script