How can we remove comma from end of each line ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we remove comma from end of each line ?
# 1  
Old 05-20-2014
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

Code:
 
Name,age,gender,location,
Joel,18,M,Newyork,
Monoj,21,M,Japan,
Litu,23,M,turki,

Expected o/p
file1.csv

Code:
 
Name,age,gender,location
Joel,18,M,Newyork
Monoj,21,M,Japan
Litu,23,M,turki

Using shell script command how can we do this. Kindly suggest.
# 2  
Old 05-20-2014
try
Code:
sed '$s/.$//' filename

This User Gave Thanks to Makarand Dodmis For This Post:
# 3  
Old 05-20-2014
Hello,

Here is an awk utility for same.

Code:
awk 'gsub(/\,$/,X) 1' file_name


Output will be as follows.

Code:
Name,age,gender,location
Joel,18,M,Newyork
Monoj,21,M,Japan
Litu,23,M,turki


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 05-20-2014
Its not working dear.

Code:
 
sed '$s/.$//' filename

Below is the actual data I have in csv file.

Code:
 
Template,Target Server,Target Component,Rule Group,Rule,Rule Reference Number,Rule Definition,Mismatched Conditions,Result,Documented Exception,Compliant,Exception Name,Exception Reference,Expiration Date,
 
RHEL_Pwd_Compliance,Server1,RHEL_Pwd_Compliance (Server1),/User Exist,User cpm,,"""Extended Object Entry:RHEL_Pwd_Compliance//rootUser"".""Value1 as String (All OS)"" equals ""Available""",,Pass,,Y,,,,

Expected output

Code:
 
Template,Target Server,Target Component,Rule Group,Rule,Rule Reference Number,Rule Definition,Mismatched Conditions,Result,Documented Exception,Compliant,Exception Name,Exception Reference,Expiration Date
 
RHEL_Pwd_Compliance,Server1,RHEL_Pwd_Compliance (Server1),/User Exist,User cpm,,"""Extended Object Entry:RHEL_Pwd_Compliance//rootUser"".""Value1 as String (All OS)"" equals ""Available""",,Pass,,Y,,,

---------- Post updated at 04:41 PM ---------- Previous update was at 04:33 PM ----------

Hi Ravinder,

Thanks for your prompt response. Its working fine.

Regards,
Litu
# 5  
Old 05-20-2014
Change Makarand Dodmis' proposal to
Code:
sed 's/,$//' file

This User Gave Thanks to RudiC 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

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

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,,,,... (13 Replies)
Discussion started by: ken6503
13 Replies

3. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 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. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

7. Shell Programming and Scripting

Remove comma and next rows beginning from the end

Hello friends, I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas, 9855162267,4,5,2010-11-03 17:02:07.627 9594567938f,5,5,2010-11-02... (9 Replies)
Discussion started by: EAGL€
9 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. Shell Programming and Scripting

Add a comma at end of every line

hello A small shell scripting help.. I have a file say with 5 lines of text (text file). At the end of everyline I need to add a comma at the end of the file. Thanks, ST2000 (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question

Featured Tech Videos