Ubuntu – Adding variable text to text file by line

bashcommand linesedtext processing

I have a text file:

cat test1
ch140/121_------_T_201607061430
ch140/121_------_T_201611070840
ch140/121_------_T_201611071125
ch140/121_------_T_201611071235

I want add to this file line by line this text:

/121_------_T_201607061430
/121_------_T_201611070840
/121_------_T_201611071125
/121_------_T_201611071235

Result must be:

ch140/121_------_T_201607061430/121_------_T_201607061430
ch140/121_------_T_201611070840/121_------_T_201611070840
ch140/121_------_T_201611071125/121_------_T_201611071125
ch140/121_------_T_201611071235/121_------_T_201611071235

I used:

cat test1 | sed -e 's/ch140//' > test2
for a in $(cat test2)
do
????
done

What command can I use for this?
I tried using a sed command, but it didn't work.

Best Answer

sed 's/\/121.*/&&/' test1
ch140/121_------_T_201607061430/121_------_T_201607061430
ch140/121_------_T_201611070840/121_------_T_201611070840
ch140/121_------_T_201611071125/121_------_T_201611071125
ch140/121_------_T_201611071235/121_------_T_201611071235

Explanation

  • s/old/new/ replace old with new
  • \/121.* match /121 and whatever comes after it
  • && the matched pattern two times

You can add tee or use redirection to save to a new file

sed 's/\/121.*/&&/' test1 | tee test2