LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED append question (https://www.linuxquestions.org/questions/linux-newbie-8/sed-append-question-4175624902/)

Essejjj 03-03-2018 10:52 PM

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

syg00 03-03-2018 11:02 PM

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.

BudiKusasi 03-04-2018 01:58 AM

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



All times are GMT -5. The time now is 07:53 PM.