Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Reformatting Text with AWK.

 
SOLVED
Go to solution
Highlighted
Valued Contributor

Reformatting Text with AWK.

Next dilemma,

user is generating a file.

"Whitman, Slim J",slim@HOTMAIL.COM,Public Schools,01/17/2008,60,

Supposed to be "CSV", however, format "" FULLNAME

I need to grab only email, district, date, and score.

Used following command:
awk -F'"' '{print $3, $4, $5, $6, $7}' filename.csv > outputfilename.csv

Since the "Full Name" is encapulated in "" I had to use -F'"' to leave the comma in place, if I use ',' it removes all commas.

Get following:
,slim@HOTMAIL.COM,Public Schools,01/17/2008,60,

Don't need leading comma..

Any thoughts appreciated.

Rex M
6 REPLIES 6
Highlighted
Acclaimed Contributor
Solution

Re: Reformatting Text with AWK.

Hi:

Well, if the leading comma is your only requirement to remove:

# awk -F'"' '{print substr($3,2),$4,$5,$6,$7}'

Regards!

...JRF...
Highlighted
Honored Contributor

Re: Reformatting Text with AWK.

awk -F'"' '{print $3, $4, $5, $6, $7}' filename.csv > outputfilename.csv

given the FS is a double qoute, then you really only needed to print $3, as the *first* field is null (line begins w/ separator), the second field is now the full name, and the 3rd field is the rest of the line.

if you are absolutely positive that the name field will always have a comma in it, the following will work:

awk -F, -v OFS=, '{print $3,$4,$5,$6}'

if there is a case where there that can't be relied on, then the following will work as long as the name is surrounded by quotes:

cut -f 3 -d'"' filename.csv | sed -e 's/^,//g' -e 's/,$//g' > outputfilename.csv
Highlighted
Honored Contributor

Re: Reformatting Text with AWK.

Try the awk construct below. It uses a comma as the field delimiter and the output can still be CSV as long as the OFS (output field separator) variable which is internal to awk(1) is set to a comma i.e.

# cat file
"Whitman, Slim J",slim@HOTMAIL.COM,Public Schools,01/17/2008,60,

# awk -F, '{OFS=",";print $3,$4,$5,$6}' file
slim@HOTMAIL.COM,Public Schools,01/17/2008,60

~hope it helps
Highlighted
Valued Contributor

Re: Reformatting Text with AWK.

Thanks all,

Got it working.

no offense to Sandman and oldschool I used Jim's coding. The others worked as well for any one whose curious.

Highlighted
Valued Contributor

Re: Reformatting Text with AWK.

Thanks All.
Highlighted
Acclaimed Contributor

Re: Reformatting Text with AWK.

Hi (again):

NO POINTS FOR THIS...

Sandman's code is really an improvement over mine. His trims the trailing comma, too. My suggestion merely addressed the fundamental question you asked.

Regards!

...JRF...