How to remove comma from first and last line?

Hi Gurus,

I need to remove comma from first and last line. I tried below code, but no luck. It only remove first line.
Gurus, please help.

 
 awk -F"," '{if(NR==1||NR==$NR) print $1; else print $0}' TEST
 

sampe file:

 
 ABC HEADER TOTAL RECORDS ARE 2.00,,,,
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00,,,,
 

expect:

 
 ABC HEADER TOTAL RECORDS ARE 2.00
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00
 

Your sample file has an empty first line, so no commas, and the following wouldn't work:

sed '1s/,//g;$s/,//g' file
 ABC HEADER TOTAL RECORDS ARE 2.00
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00
1 Like

Hi RudiC,

as always, thank you very much.

awk doesn't know about the last line when it works on the last line, only afterwards. So it's not THAT easy in awk :

awk 'LAST {print LAST}; NR==1 {gsub (/,/,_)}; {LAST = $0} END {gsub (/,/, _, LAST); print LAST}' file
 ABC HEADER TOTAL RECORDS ARE 2.00
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00
1 Like

Another approach by reading file twice:-

awk 'NR==FNR{++c;next}FNR==1||FNR==c{gsub(",",X)}1' file file
1 Like

Try also

awk 'FNR == 1 || FNR == c {c = NR-1; gsub(",",X)} FNR < NR' file file
1 Like

Hello Ken6503,

Could you please try following too and let me know if this helps you.

awk '{for(i=1;i<=NF;i++){if(i==1 || i==NF){gsub(/,/,X,$i);print $i} else {print $i};}}' RS="" FS="\n"  Input_file

Output will be as follows.

 ABC HEADER TOTAL RECORDS ARE 2.00
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00

Thanks,
R. Singh

1 Like

The shortest way :

$ awk -v last=$(wc -l <file.txt) 'NR==1 || NR==last{gsub(",","")}1' file.txt
1 Like

RavinderSingh13: Interesting approach ...

awk 'gsub(/,/, _, $1) && gsub (/,/, _, $NF)' RS="" FS="\n" OFS="\n" file
1 Like

What a funny contest.
On the same way :

$ awk 'FNR == 1 && NR != 1{last=NR-1}NR == FNR{next}FNR == 1 || FNR ==last{gsub(",",X)} 1 ' file.txt file.txt
1 Like

Hello All/Ken6503,

One more approach here which is based on that if first or last lines will always have consecutive 4 commas as like shown Input_file, then following may help in same too.

awk -F',,,,' '{for(i=1;i<=NF;i++){if($i){print $i}}}'  Input_file

Output will be as follows.

 ABC HEADER TOTAL RECORDS ARE 2.00
C,300,ABC,BC,
C,400,ABC,TO,
ABC TRAILER. ACCOUNT SUM IS 3000.00

EDIT: Or even more simpler approach as follows.

awk -F',,,,' '{print $1}'  Input_file

Thanks,
R. Singh

1 Like

Or

awk 'gsub(/,,+$/,_)+1' file

Indeed, sed is the most suitable for this application, when removing trailing comma's only on the first and last line.

Yet, here is another approach using awk, removing trailing comma's only on the first and last line, while using a single read:

awk -F, 'NR==1{p=$1; next} {print p; p=$0; q=$1} END{print q}' file

or

awk -F, 'NR>1{print p; p=$0; q=$1; next} {p=$1} END{print q}' file

or

awk -F, 'NR>1{print p}{q=$1; p=NR>1?$0:q} END{print q}' file

or (stretching it):

awk -F, 'q{print p}{q=$1; p=NR>1?$0:q} END{print q}' file

or

awk -F, 'q{print p}{p=q=$1} NR>1{p=$0} END{print q}' file

(all approaches assuming the input has a starting and a trailing line).

--

Like all approaches that read the file twice, whether it is truly the shortest depends on the length of the filename :wink:

There would need to be double quotes around the command substitution "$(wc -l <file.txt)" [/COLOR] , otherwise the -v assignment will fail if there is leading space in the output of wc , as is the case with non-GNU versions.

1 Like

Interesting solutions so far :b:

Here is my try, in perl:

perl -pe 'if($. == 1 || eof) {s/,/ /g}' file
1 Like