Find the answer to your Linux question:
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. ...
  1. #1
    Just Joined! AceAll's Avatar
    Join Date
    May 2005
    Posts
    67

    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

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    This should do,
    replace "list" with the filename of your file
    Code:
    for line in $(cat list); do echo -n "$line,";done
    There will be a trailing ",".
    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.

  3. #3
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    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

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    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 that
    You must always face the curtain with a bow.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Using paste:
    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
    producing:
    Code:
    % ./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
    Best wishes ... cheers, drl
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...