convert a file content using shell script

Viewed 231

Hello everyone I'm a beginner in shell coding. In daily basis I need to convert a file's data to another format, I usually do it manually with Text Editor. But I often do mistakes. So I decided to code an easy script who can do the work for me. The file's content like this

/release201209
a1,a2,"a3",a4,a5
b1,b2,"b3",b4,b5
c1,c2,"c3",c4,c5

to this:

a2>a3
b2>b3
c2>c3

The script should ignore the first line and print the second and third values separated by '>'

I'm half way there, and here is my code

#!/bin/bash
#while Loops

i=1
while IFS=\" read t1 t2 t3
do
    test $i -eq 1 && ((i=i+1)) && continue
    echo $t1|cut -d\, -f2 | { tr -d '\n'; echo \>$t2; } 
done < $1 

The problem in my code is that the last line isnt printed unless the file finishes with an empty line \n And I want the echo to be printed inside a new CSV file(I tried to set the standard output to my new file but only the last echo is printed there). Can someone please help me out? Thanks in advance.

3 Answers

Rather than treating the double quotes as a field separator, it seems cleaner to just delete them (assuming that is valid). Eg:

$ < input tr -d '"'  | awk 'NR>1{print $2,$3}' FS=, OFS=\>
a2>a3
b2>b3
c2>c3

If you cannot just strip the quotes as in your sample input but those quotes are escaping commas, you could hack together a solution but you would be better off using a proper CSV parsing tool. (eg perl's Text::CSV)

Here's a simple pipeline that will do the trick:

sed '1d' data.txt | cut -d, -f2-3 | tr -d '"' | tr ',' '>'

Here, we're just removing the first line (as desired), selecting fields 2 & 3 (based on a comma field separator), removing the double quotes and mapping the remaining , to >.

Use this Perl one-liner:

perl -F',' -lane 'next if $. == 1; print join ">", map { tr/"//d; $_ } @F[1,2]' in_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.
-a : Split $_ into array @F on whitespace or on the regex specified in -F option.
-F',' : Split into @F on comma, rather than on whitespace.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches

Related