LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed - Problem appending at the end of line (https://www.linuxquestions.org/questions/linux-newbie-8/sed-problem-appending-at-the-end-of-line-842485/)

jdom 11-05-2010 12:48 AM

sed - Problem appending at the end of line
 
Hi,

Using sed, I am trying to append four commas ',,,,' at the end of lines containing the pattern 'Response' in a text file with lines such as these:

6,Pulse,50,254968,14886,NA,,,,
7,Picture,8,265157,0,1,15045,2,0,15000
7,Response,1,271553,6396,1
7,Pulse,50,274969,9812,NA,,,,
8,Picture,1,290232,0,1,15045,2,0,15000
8,Pulse,50,294969,4737,NA,,,,
8,Response,1,296837,6605,2
8,Pulse,50,314970,24738,NA,,,,
9,Picture,4,315307,0,1,15045,2,0,15000
9,Response,1,321090,5783,1
9,Pulse,50,334971,19664,NA,,,,
10,Picture,14,340382,0,1,15045,2,0,15000
10,Response,1,350102,9720,2

The script I am using for this is

sed -i '/Response/s/$/,,,,/'

However, when I run the scrip, the commas are appended in a new line inserted after the line with the pattern. When I open the modified txt file it looks like this:

6,Pulse,50,254968,14886,NA,,,,
7,Picture,8,265157,0,1,15045,2,0,15000
7,Response,1,271553,6396,1
,,,,
7,Pulse,50,274969,9812,NA,,,,
8,Picture,1,290232,0,1,15045,2,0,15000
8,Pulse,50,294969,4737,NA,,,,
8,Response,1,296837,6605,2
,,,,
8,Pulse,50,314970,24738,NA,,,,
9,Picture,4,315307,0,1,15045,2,0,15000
9,Response,1,321090,5783,1
,,,,
9,Pulse,50,334971,19664,NA,,,,
10,Picture,14,340382,0,1,15045,2,0,15000
10,Response,1,350102,9720,2
,,,,

Any idea what's wrong here and how to get the desired result?

Many thanks

Dark_Helmet 11-05-2010 01:21 AM

The command you gave worked fine on my end.

Could it be a line ending problem? That is, are you trying to modify a Windows file (which uses different line endings than Linux)? That might explain it.

Alternatively, you can try this version of the command:
Code:

sed -i '/Response/s/\(.*\)/\1,,,,/' data_file
Where "data_file" should be replaced with your appropriate filename.


All times are GMT -5. The time now is 05:19 PM.