0

I put together this shell script to do two things:

  1. Change the delimiters in a data file ('::' to ',' in this case)
  2. Select the columns and I want and append them to a new file

It works but I want a better way to do this. I specifically want to find an alternative method for exploding each line into an array. Using command line arguments doesn't seem like the way to go. ANY COMMENTS ARE WELCOME.

    # Takes :: separated file as 1st parameters
    SOURCE=$1

    # create csv target file
    TARGET=${SOURCE/dat/csv}
    touch $TARGET

    echo #userId,itemId > $TARGET

    IFS=","
    while read LINE
    do
        # Replaces all matches of :: with a ,
        CSV_LINE=${LINE//::/,}
        set -- $CSV_LINE
        echo "$1,$2" >> $TARGET
    done < $SOURCE
2
  • There has got to be a better title -- please update your title so it is relevant. (-1 for now, for afore mentioned reason, but that can easily be amended later.) Commented Jun 15, 2012 at 3:35
  • 1
    Place the redirection to $TARGET outside the loop, probably as > $TARGET. You can then lose the touch before the loop. Your title line needs to include quotes (echo "#userID,itemId") too. Commented Jun 15, 2012 at 3:42

4 Answers 4

2

Instead of set, you can use an array:

arr=($CSV_LINE)
echo "${arr[0]},${arr[1]}"
Sign up to request clarification or add additional context in comments.

Comments

2

The following would print columns 1 and 2 from infile.dat. Replace with a comma-separated list of the numbered columns you do want.

awk 'BEGIN { IFS='::'; OFS=","; } { print $1, $2 }' infile.dat > infile.csv

Comments

1

Perl probably has a 1 liner to do it.

Awk can probably do it easily too.

My first reaction is a combination of awk and sed:

  • Sed to convert the delimiters
  • Awk to process specific columns
cat inputfile | sed -e 's/::/,/g' | awk -F, '{print $1, $2}'
# Or to avoid a UUOC award (and prolong the life of your keyboard by 3 characters
sed -e 's/::/,/g' inputfile | awk -F, '{print $1, $2}'

3 Comments

Fair comment - never knew I could get an award for it though ;-)
I'll have to find tutorials on sed and awk. Thanks for the example.
That removes the comma. Set OFS = FS in a BEGIN block.
1

awk is indeed the right tool for the job here, it's a simple one-liner.

$ cat test.in
a::b::c
d::e::f
g::h::i
$ awk -F:: -v OFS=, '{$1=$1;print;print $2,$3 >> "altfile"}' test.in
a,b,c
d,e,f
g,h,i
$ cat altfile
b,c
e,f
h,i
$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.