LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2018, 09:52 PM   #1
Essejjj
LQ Newbie
 
Registered: Mar 2018
Posts: 1

Rep: Reputation: Disabled
SED append question


Hey, I'm trying to append a columns contents to the end of each line. For example, I have:

0,John L Doe,Addenda Circle,Cornwall
1,Jane L Doe,Wyatt Way,Peterborough
2,John L Doe,Turnagain Street,Sault Ste. Marie
3,John L Doe,Curt Court,Strathcona
4,Jane L Doe,Chugach Meadows Loop,Cape Breton
5,John L Doe,Blackwell,Anton Way,Drummondville
6,John L Doe,Loon Cove Circle,Clarington
7,Jane L Doe,Ransom Ridge Road,Ajax

Trying to append the first column deliminated by a comma to the end of each line including a character "I" to become:

0,John L Doe,Addenda Circle,Cornwall I0
1,Jane L Doe,Wyatt Way,Peterborough I1
2,John L Doe,Turnagain Street,Sault Ste. Marie I2
3,John L Doe,Curt Court,Strathcona I3
4,Jane L Doe,Chugach Meadows Loop,Cape Breton I4
5,John L Doe,Blackwell,Anton Way,Drummondville I5
6,John L Doe,Loon Cove Circle,Clarington I6
7,Jane L Doe,Ransom Ridge Road,Ajax I7

Adding the "I" is easy enough to add with: sed 's/$/I/' file but I'm having issues with copying and appending the first columns contents
 
Old 03-03-2018, 10:02 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,140

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
You need to capture the first "column" in a regex subexpression, then use the back-reference for it in the substitution.
Have a read of this.
 
Old 03-04-2018, 12:58 AM   #3
BudiKusasi
Member
 
Registered: Apr 2017
Posts: 54

Rep: Reputation: Disabled
Code:
cat >a
0,John L Doe,Addenda Circle,Cornwall
1,Jane L Doe,Wyatt Way,Peterborough
2,John L Doe,Turnagain Street,Sault Ste. Marie
3,John L Doe,Curt Court,Strathcona
4,Jane L Doe,Chugach Meadows Loop,Cape Breton 
5,John L Doe,Blackwell,Anton Way,Drummondville
6,John L Doe,Loon Cove Circle,Clarington
7,Jane L Doe,Ransom Ridge Road,Ajax

sed -r 's/([0-9]+).+/\0 I\1/' a
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] append text with sed tripialos Linux - Newbie 6 11-12-2014 04:33 AM
how to append a file using sed command radhikamody Linux - Newbie 1 02-27-2012 01:54 AM
[SOLVED] sed trick to do conditional append the_gripmaster Linux - General 9 01-24-2012 05:39 PM
[SOLVED] Append text with AWK or SED? LostChild1 Linux - Newbie 6 03-20-2010 08:46 AM
Recursive line append with sed usrb Programming 3 04-01-2009 05:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration