Use sed to add comma to end of first field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use sed to add comma to end of first field
# 1  
Old 11-09-2012
Use sed to add comma to end of first field

Example data
Code:
Gi1/10  
Gi1/12  	xl32lytscb07
3/11 		to nyc
3/12 		41764 ecomm
3/13 		hxcsxsa 2/1
3/14 		ziim570-rsvd
3/15 		xl3NDSADM
Po1            VPC trunk to xl3-i
Po2            ***DO NOT ENABLE**
Po13           *** VPC link to
Po101          
Po102          xl3-2lyg1accsgh-fe
Po200          
Po201          09-tietie22834
Po202          16-tietie22834
Po205          
Po206          19-tietie22834
mgmt0          OOB Management
Eth101/1/1     08-tietie2283
Eth101/1/2     08-tietie2283
Eth101/1/3     
Eth101/1/4     08-tietie2283
Eth101/1/5     08-tietie2283
Eth101/1/6     
Eth101/1/7     16-tietie2283
Eth101/1/8     16-tietie2283
Eth101/1/9     16-tietie2283

Desired Result
Code:
Gi1/10,  
Gi1/12,  	xl32lytscb07
3/11, 		to nyc
3/12, 		41764 ecomm
3/13, 		hxcsxsa 2/1
3/14, 		ziim570-rsvd
3/15, 		xl3NDSADM
Po1,            VPC trunk to xl3-i
Po2,           ***DO NOT ENABLE**
Po13,           *** VPC link to
Po101,          
Po102,          xl3-2lyg1accsgh-fe
Po200,          
Po201,          09-tietie22834
Po202,          16-tietie22834
Po205,          
Po206,          19-tietie22834
mgmt0,          OOB Management
Eth101/1/1,     08-tietie2283
Eth101/1/2,     08-tietie2283
Eth101/1/3,     
Eth101/1/4,     08-tietie2283
Eth101/1/5,     08-tietie2283
Eth101/1/6,     
Eth101/1/7,     16-tietie2283
Eth101/1/8,     16-tietie2283
Eth101/1/9,    16-tietie2283

Code that Ive tried

Code:
sed 's/\/[0-9]\/[0-9]*/&,/g' < $x.v >$x.vl


Last edited by Scott; 11-09-2012 at 05:53 PM.. Reason: Code tags for data too
# 2  
Old 11-09-2012
You can use awk instead:-
Code:
awk ' { $1 = $1","; print; } ' input_file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-09-2012
You can also do that slightly shorter with:
Code:
$ awk '$1=$1","' file

but neither will maintain the spacing between fields. Perhaps using awk's sub function would be the answer.

This seems a bit yuck, but:
Code:
$ sed "s/ /,&/;/ /!s/$/,/" file

(I'm sure that can be done in one expression!)
This User Gave Thanks to Scott For This Post:
# 4  
Old 11-09-2012
Code:
sed 's/^[^ ]*/&,/' file

These 2 Users Gave Thanks to balajesuri For This Post:
# 5  
Old 11-09-2012
Yay! That was the one of course Smilie
# 6  
Old 11-09-2012
Thanks for the solutions guys. I was trying to figure this out for 2 days.
# 7  
Old 11-12-2012
Quote:
Originally Posted by balajesuri
Code:
sed 's/^[^ ]*/&,/' file

Depending on the delimiter, you may want to modify slightly:
Code:
sed 's/^[^ \t]*/&,/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

sed add double quotes and comma

Hi, 00000119EEEC3F25 feedoor 20171103 0000011A4F152077 feedard 20171024 00000191FA295F61 feedzipperhola 20171023 00000213C57BB856 feedriodapple 20171005 0000025F778EF9D5 joobakoolrk 20171004 I needed the result as: "00000119EEEC3F25", "feedoor", ... (9 Replies)
Discussion started by: ashokvpp
9 Replies

3. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

4. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

5. Shell Programming and Scripting

Add a new field at the end of each line

i want to add a white-space at the end of each line for my inp.file, but when i do it, the result is a new line with a white-space between each line! my input: 2012 0811 1223 15.2 L 38.393 46.806 9.0 Teh 78 0.5 6.5LTeh 1 GAP=74 ... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

6. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

Want to use sed to add some parameters at the end of the line

Hello All, I have a doubt in sed, i want to add some parameter at the end of the tag inside a xml tag. how to i do that. so i want to add Results="true" value="high" inside the xml tag. Orignal <execute description="reboot"> <execute description="Stop Servlet"> After adding the... (5 Replies)
Discussion started by: asirohi
5 Replies

9. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 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