How to remove comma from first and last line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove comma from first and last line?
# 8  
Old 10-21-2016
The shortest way :
Code:
$ awk -v last=$(wc -l <file.txt) 'NR==1 || NR==last{gsub(",","")}1' file.txt

This User Gave Thanks to blastit.fr For This Post:
# 9  
Old 10-21-2016
RavinderSingh13: Interesting approach ...
Code:
awk 'gsub(/,/, _, $1) && gsub (/,/, _, $NF)' RS="" FS="\n" OFS="\n" file

This User Gave Thanks to RudiC For This Post:
# 10  
Old 10-21-2016
Quote:
Originally Posted by RudiC
Try also
Code:
awk 'FNR == 1 || FNR == c {c = NR-1; gsub(",",X)} FNR < NR' file file

What a funny contest.
On the same way :
Code:
$ awk 'FNR == 1 && NR != 1{last=NR-1}NR == FNR{next}FNR == 1 || FNR ==last{gsub(",",X)} 1 ' file.txt file.txt


Last edited by blastit.fr; 10-21-2016 at 05:38 PM.. Reason: typo
This User Gave Thanks to blastit.fr For This Post:
# 11  
Old 10-21-2016
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.
Code:
awk -F',,,,' '{for(i=1;i<=NF;i++){if($i){print $i}}}'  Input_file

Output will be as follows.
Code:
 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.
Code:
awk -F',,,,' '{print $1}'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-21-2016 at 05:47 PM.. Reason: Added one more solution too successfully now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 12  
Old 10-21-2016
Or
Code:
awk 'gsub(/,,+$/,_)+1' file

# 13  
Old 10-22-2016
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:

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

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

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

or (stretching it):
Code:
awk -F, 'q{print p}{q=$1; p=NR>1?$0:q} END{print q}' file

or
Code:
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).

--
Quote:
Originally Posted by blastit.fr
The shortest way :
Code:
$ awk -v last=$(wc -l <file.txt) 'NR==1 || NR==last{gsub(",","")}1' file.txt

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

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

Last edited by Scrutinizer; 10-22-2016 at 04:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 10-22-2016
Interesting solutions so far Smilie

Here is my try, in perl:
Code:
perl -pe 'if($. == 1 || eof) {s/,/ /g}' file


Last edited by greet_sed; 10-22-2016 at 05:29 AM.. Reason: Update text
This User Gave Thanks to greet_sed For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove comma?

hi all, in the 3rd field i am having comma. can anyone tell me how to remove the comma in the 3rd field and 4th field. |1.77|0.1|1,335.20|3,513.30|190|7.00 |4.40 |2.50 |1|1|5|5|Section903-Liquor|StLouis|0||||||||||| 40997|9999999|9999999|195186280|0102796|36949|00083089660358|2016|MAY ... (2 Replies)
Discussion started by: arun888
2 Replies

2. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

3. Shell Programming and Scripting

How to remove last comma?

Hi Gurus, I have file like below: a b c d .. I want to get 'a','b','c', 'd' I try to use below command to get it, but I got one extra comma sed "s/.*/'&'/" code_LIST.txt |tr -s '\n' "," I got below result: 'a','b','c','d', there is one extra comma at end of the line. How can I... (10 Replies)
Discussion started by: ken6503
10 Replies

4. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

5. UNIX for Dummies Questions & Answers

Remove First Char from Line in File Only if it's a comma

I have a file, I need to remove the first character of each line, but only if it's a comma. I don't want to delete any other commas in each line. Trying cat or sed but I really don't know them very well, would love some help. This removes the first comma, but it removes the first comma no... (6 Replies)
Discussion started by: Cynthia
6 Replies

6. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

7. Shell Programming and Scripting

Remove the tail comma

Hi, I have one file with the following details, file1.txt ====== The following are the accounts which are having the balance 22,22,22,10,12,00000013, Here I want to delete the tail comma. Can anybody help me out... Thanks in advance..!! (3 Replies)
Discussion started by: Kattoor
3 Replies

8. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies

9. UNIX for Dummies Questions & Answers

How to remove comma from the last line of the file

Hi, I have a file which has records which end with a comma. for example: My file looks like 1234, 5678, 3455, 3566, 4444, 9999, I need to remove comma for the last line in the file so that my file should look like: 1234, 5678, 3455, (5 Replies)
Discussion started by: sandeep_1105
5 Replies

10. UNIX for Advanced & Expert Users

remove unnecessary comma from file

HI all, I have a file with following data - test1 "ABC,D",1234,"XYZ,QWER",1234 "SZXA",9870,"ASD,QWERT",234 "XZ,SD",9478,"ADCS,AXZ",876 "WESR",8764,"AQZXAS",9888 "WESR",9898,"WESDRTSAW",3323 I need to get rid of unnecessary commas in fields having double quotes. Ouput - ... (1 Reply)
Discussion started by: sumeet
1 Replies
Login or Register to Ask a Question