Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

BASH command to convert lines of text into comma seperated string?

Hello everyone,

I have a text file with lines of text:
239847234
938443895
234972344

and I want to output it as "239847234, 938443895, 234972344".

Not sure the sed or awk command or if there is another way anyone recommends.

Thanks!!
0
parlays
Asked:
parlays
  • 4
  • 2
5 Solutions
 
jeremycrussellCommented:
one way

#!/bin/bash

for f in `cat /path/to/textfile`
 do
  echo -n "$f," >>/new/textfile
done

Open in new window

0
 
woolmilkporcCommented:
tr '\n' ',' < textfile | sed 's/,$//'

wmp
0
 
woolmilkporcCommented:
awk '{printf $0","}' textfile | sed 's/,$//'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
awk -v L="$(wc -l < textfile)" '{if(NR<L) printf $0","; else print $0}' textfile
0
 
woolmilkporcCommented:
xargs -I{} echo -n {}, < textfile | sed 's/,$//'
0
 
parlaysAuthor Commented:
Great stuff!!!  Thanks so much guys, appreciate it all.
0
 
parlaysAuthor Commented:
Thanks!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now