Results 1 to 5 of 5
Hello,
I need some help with some BASH scripting. I got a file as shown below.
<code>
name1.name2@somesite.com
name2.name3@somesite.com
name4.name5@somesite.com
name6.name7@somesite.com
name8.name9@somesite.com
</code>
I would change this file to CSV.
...
- 06-04-2010 #1
Flat file to CSV
Hello,
I need some help with some BASH scripting. I got a file as shown below.
<code>
name1.name2@somesite.com
name2.name3@somesite.com
name4.name5@somesite.com
name6.name7@somesite.com
name8.name9@somesite.com
</code>
I would change this file to CSV.
<code>
name1.name2@somesite.com,name2.name3...9@somesite.com
</code>
Any idea?
-Ace
- 06-04-2010 #2
This should do,
replace "list" with the filename of your file
There will be a trailing ",".Code:for line in $(cat list); do echo -n "$line,";done
If you dont want that, sed can help:
Code:for line in $(cat list); do echo -n "$line,";done | sed s/,$//
You must always face the curtain with a bow.
- 06-04-2010 #3
If all you want is to change the newline into a comma:
Code:cat file|tr '\n' ','>newfile
Can't tell an OS by it's GUI
- 06-04-2010 #4
yes, Freston´s apporach is better, as it invokes only two commands.
Not two + "echo" times number of lines + "sed" like mine
There will still be trailing "," but a sed line can deal with thatYou must always face the curtain with a bow.
- 06-04-2010 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Using paste:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate transform column to comma-separated-values, paste # Infrastructure details, environment, commands for forum posts. # Uncomment export command to run script as external user. # export PATH="/usr/local/bin:/usr/bin:/bin" set +o nounset pe() { for i;do printf "%s" "$i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } LC_ALL=C ; LANG=C ; export LC_ALL LANG pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG" pe "(Versions displayed with local utility \"version\")" c=$( ps | grep $$ | awk '{print $NF}' ) version >/dev/null 2>&1 && s=$(_eat $0 $1) || s="" [ "$c" = "$s" ] && p="$s" || p="$c" version >/dev/null 2>&1 && version "=o" $p printf specimen paste set -o nounset pe FILE=${1-data1} # Display sample of data file, with head & tail as a last resort. pe " || start [ first:middle:last ]" specimen $FILE \ || { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; } pe " || end" pl " Results:" paste -s -d "," $FILE exit 0
Best wishes ... cheers, drlCode:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0 GNU bash 3.2.39 printf - is a shell builtin [bash] specimen (local) 1.17 paste (GNU coreutils) 6.10 || start [ first:middle:last ] Whole: 5:0:5 of 5 lines in file "data1" name1.name2@somesite.com name2.name3@somesite.com name4.name5@somesite.com name6.name7@somesite.com name8.name9@somesite.com || end ----- Results: name1.name2@somesite.com,name2.name3@somesite.com,name4.name5@somesite.com,name6.name7@somesite.com,name8.name9@somesite.com
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote