Tuesday, 18 September 2012

Awk, Sed and TR

I used Awk, but had issues with removing whitespace and inserting commas.

I had a poorly written Awk text file script, that filled out 19 fields with info. Done in thee style of a none-programmer.

{ FS = "," } ; # comma-delimited fields
{ sub($1,"")}
{firstname=$3}
{surname = $4}
{group = $8}
{username =tolower( $3"."$4)}
.....
#more fields here zzzzzzzzz
.....
{$16=" "}
{$17=" "}
{$18=" "}
{$19=" "}
{print}

which was invoked like this:

awk -f mybadscript.awk mydata.csv > newdata.csv

Then I had to run Sed

sed 's/[:space:]+/,/g' newdata.csv > afterseddata.csv
http://stackoverflow.com/questions/8766165/using-awk-to-remove-whitespace

Then ran TR

tr ' ' ',' < afterseddata.csv > aftertrdata.csv
http://stackoverflow.com/questions/1271222/replace-white-spaces-with-a-comma-in-a-txt-file-in-linux

Phew. Perl might be easier next time.


No comments:

Post a Comment