How to remove last comma?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove last comma?
# 1  
Old 10-29-2013
How to remove last comma?

Hi Gurus,

I have file like below:
Code:
a
b
c
d

..
I want to get
Code:
'a','b','c', 'd'

I try to use below command to get it, but I got one extra comma
Code:
sed "s/.*/'&'/" code_LIST.txt |tr -s '\n' ","

I got below result:
Code:
'a','b','c','d',

there is one extra comma at end of the line.
How can I remove this extra comma and get result like:
Code:
'a','b','c','d'

Thanks in advance
# 2  
Old 10-29-2013
Try

Code:
$ echo "'a','b','c','d'," | awk 'gsub(/,$/,x)'
'a','b','c','d'

OR

Code:
$ echo "'a','b','c','d'," | sed 's/,$//g'
'a','b','c','d'

OR
Code:
$ echo "'a','b','c','d'," | grep -Po '.*(?=,)'
'a','b','c','d'


Last edited by Akshay Hegde; 10-29-2013 at 04:33 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-29-2013
Code:
sed -n "s/.*/'&'/;1{h;d};H;\${x;s/\n/,/g;p;q}" code_LIST.txt

Emanuele
This User Gave Thanks to targzeta For This Post:
# 4  
Old 10-29-2013
Code:
 sed -e 's/^/\x27/;s/$/\x27/' input_file | sed -e ':a;N;$!ba;s/\n/,/g'

Code:
 perl -pe 's/^(.*)\n/\x27$1\x27,/;s/,$// if eof' input_file


Last edited by greet_sed; 10-29-2013 at 04:59 PM.. Reason: Add perl solution
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 10-29-2013
Hi,


one more awk approach.

Code:
awk -vORS="," -vs1="'" '{print s1$0s1}'  text_data1213 | sed 's/, $//g'

output will be as follows.


Code:
'a','b','c','d'



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 10-29-2013
Another awk approach:
Code:
awk '{gsub(/[a-z]/,"\x27&\x27");$1=$1}1' RS= OFS=, file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 10-29-2013
try also:
Code:
awk '$1=$1 {printf ("\x27%s\x27\n", $0);}' RS= OFS="','" infile

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove duplicates from comma separated list

Hi, I have following input file: niki niki niki1 niki niki2 niki,niki2 niki3 niki,niki3,niki niki4 niki4,blabla niki5 jkjkl niki6 niki60,niki6 I would like to delete lines with identical matches completely and remove the selfmatches in the other lines. ... (2 Replies)
Discussion started by: niki0211
2 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

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

4. 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

5. Shell Programming and Scripting

Remove comma from decimal value using sed command

Hi Experts , My requirement is like this .. I have source comming as 4,234.55 I need and out put = 4234.55 I need to write a sed command .. I have already used sed command for multiple conditions in a file for replacing comma , double quotes , brackets , retain negative values..... (3 Replies)
Discussion started by: bshivali
3 Replies

6. 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

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