Computing.Net > Forums > Unix > awk last header/field leaves comma

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk last header/field leaves comma

Reply to Message Icon

Name: VitasL
Date: May 6, 2007 at 10:30:16 Pacific
OS: Unix
CPU/Ram: ?
Product: IBM/AIX
Comment:

I'm totally new at this, so maybe, hopefully, this is an easy one.

I have a file which I want to remvoe the last field and header, keeping the OFS=","

When I execute the command, the last field and header are removed, but what is left is a comma (,) at the end. I am expecting nothing.

my file is:
COMPANY,TRANS-TYPE,INVOICE,CUSTOMER,STATUS,GL-STATUS,BATCH-NBR,PROCESS-LEVEL,TRA
NS-DATE,GL-DATE,DESC,ORIG-AMT,TEST
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UP
LOAD","12.00","TEST"
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UP
LOAD","-12.00","TEST"

I run: awk 'BEGIN{FS=" ";OFS=","} ; {$NF="";print}' test3.csv > test4.csv

and get
COMPANY,TRANS-TYPE,INVOICE,CUSTOMER,STATUS,GL-STATUS,BATCH-NBR,PROCESS-LEVEL,TRA
NS-DATE,GL-DATE,DESC,ORIG-AMT,
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UP
LOAD","12.00",
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UP
LOAD","-12.00",

with vi I see that there is a space there at the end, so I ran
awk '{sub(/[\t]+$/,"");print}' test4.csv > test5.csv
but it doesn't remove the last comma or space after it.

I also ran commands to remove leading spaces, removing sapces within, all to no avail ... what gives? What am I missing?

Thanks for your patience and help.


Thanks!
Vitas



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 7, 2007 at 05:49:00 Pacific
Reply:

[code]
awk '
BEGIN{FS="," ; OFS = ","}
NR==1{next}
{
for(i=1;i<=NF;i++) {
if ( i==NF ) printf $i
else { printf $i ","}
}
print ""
}

' "file"
[/code]


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk last header/field leaves comma

Shell script for Text file parsing www.computing.net/answers/unix/shell-script-for-text-file-parsing/4388.html

awk to print last parameter www.computing.net/answers/unix/awk-to-print-last-parameter/6830.html

combining fields with awk. www.computing.net/answers/unix/combining-fields-with-awk/6025.html