Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing last field header leaves comma (,) instead 2

Status
Not open for further replies.

VitasL

Programmer
May 6, 2007
9
US
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.


 
Try this:

Code:
awk 'BEGIN{FS="[red],[/red]";OFS=","} ; {[red]NF--[/red];print}' test3.csv > test4.csv


HTH,

p5wizard
 
It complains ...

syntax error The source line is 1.
The error context is
BEGIN{FS=",";OFS=","} ; >>> {NF=--; <<<
awk: The statement cannot be correctly parsed.
The source line is 1.

Thanks for the fast response!!!!
 
Somehow you put an extra "=" in there when you copied the solution.

Annihilannic.
 
Annihilannic,

Not sure I follow you. Which extra "=" are you referring to?
p5wizard's solution or my response to it?

Thanks for helping!

 
In p5's solution he has NF--. In yours you have NF=--. Take out the '=' and it should run without errors.

Annihilannic.
 
Annihilannic,

Good eye! It ran, but now I've introduced another character after the comma I am trying to remove (along with the last header and field):

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

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

Any ideas? Thanks for your prompt responses!
 
Hi

Yet another extra character...
Code:
[gray]# wrong[/gray]
awk 'BEGIN{FS=",";OFS=","} ; {[s][red]$[/red][/s]NF--; print}' test3.csv > test4.csv

[gray]# correct[/gray]
awk 'BEGIN{FS=",";OFS=","} ; {NF--; print}' test3.csv > test4.csv

Feherke.
 
Sorry for the typing errors. Now, it seems, output looks same as input. No changes made. I'm puzzled ...

awk 'BEGIN{FS=",";OFS=","} ; {NF--; print}' test3.csv > test4.csv

COMPANY,TRANS-TYPE,INVOICE,CUSTOMER,STATUS,GL-STATUS,BATCH-NBR,PROCESS-LEVEL,TRANS-DATE,GL-DATE,DESC,ORIG-AMT,TEST
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","12.00","TEST"
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","-12.00","TEST"

I really appreciate all the responses today! Thank You!
 
Feherke,

Not sure what you are asking. My version of awk? I am on AIX 5.3, IBM 9110-510.

Field is removed with no problem, just the pesky comma remains. Unfortunately, I need csv file for further processing, just not one on the last field ...

Any other ideas?
 
I wanted to thank everyone for their input. Your responses were amazing for this time of week and day. Thank you very, very much.

I think that I will have to resort to some variation of the printf command where I specifically detail each field and which separator to use, excluding the last one which I don't need. ($1 ","$2 "," $3 "," $4 "," $5) ... something like that ...

Thanks everyone again for all the assistance!
Vitas
 
You need to force the re-evaluation of the record.
Code:
awk 'BEGIN{FS=OFS=","} ; {NF--; [COLOR=red]$1=$1[/color]; print}' test3.csv > test4.csv

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thank you for the suggestion! However, I see no change in the output file. TEST field and header still remain. Did I miss something?

$ more test3.csv
COMPANY,TRANS-TYPE,INVOICE,CUSTOMER,STATUS,GL-STATUS,BATCH-NBR,PROCESS-LEVEL,TRANS-DATE,GL-DATE,DESC,ORIG-AMT,TEST
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","12.00","TEST"
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","-12.00","TEST"

$ awk 'BEGIN{FS=OFS=","} ; {NF--; $1=$1; print}' test3.csv > test4.csv

$ more test4.csv
COMPANY,TRANS-TYPE,INVOICE,CUSTOMER,STATUS,GL-STATUS,BATCH-NBR,PROCESS-LEVEL,TRANS-DATE,GL-DATE,DESC,ORIG-AMT,TEST
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","12.00","TEST"
"30","C","123","ABC","1","2","1","ACTUL","20070228","20070228","ACTUAL CLAIMS UPLOAD","-12.00","TEST"

Thanks for your input!
 
my solution (second post of this thread) works on ubuntu at home...

here's a solution for AIX:

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


HTH,

p5wizard
 
Hi

VitasL said:
Not sure what you are asking. My version of awk?
No, I asked about the flavor/vendor/whatever. For example I use [tt]gawk[/tt] ( GNU [tt]awk[/tt] ) and [tt]mawk[/tt] ( Mike's [tt]awk[/tt] ) and rarely [tt]awk95[/tt] ( Win95 [tt]awk[/tt] ). A good and popular one is [tt]nawk[/tt] ( New [tt]awk[/tt] ). But there are more, like ( [tt]tawk[/tt] ( Thompson [tt]awk[/tt] ), [tt]jawk[/tt] ( Java [tt]awk[/tt] ), etcetera. Those are mostly compatible, but have special features and/or misses.

Feherke.
 
The following should work even with old awk flavor:
Code:
awk -F, '{for(i=1;i<NF-1;++i)printf $i",";print $i}' test3.csv > test4.csv

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
p5wizard and PHV - your solutions both worked! Thank you very much for your persistence on this issue. Thanks again for everyone for contributing. I learned a lot, and that's probably the most valuable thing of all! Time to donate some $$ to this site! It was worth it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top