0

Is there a way to use the sed command this to search for a special character at the beginning of line, replace it with another special character, and append a special character to that line only?

I did some digging and and have looked thru a few forums, and tried to understand the man page for sed. It does look like I have to do something with the special characters. I am using the sed command to append the lines that would start with a [ and add a ] but it does not do anything. I think I am still missing something. The other issue is that its reliant on another sed* that replaces a //for a[`.

The find and replace:

sed -e 'y#//#[#'

This returns an error:

sed: -e expression #1, char 7: strings for `y' command are different lengths

The append:

sed -e "/^'['/s/$/']/g"

I created a copy of the file and did a find and and replace with a text editor changing the // for a [ however this command does not return any results and it does not add the character to the end.

I think i am still missing something for these command to work properly, and not sure if there is a way i can combine them to a single command.

1 Answer 1

1

As far as I can understand what you are after, this sed command should do it. It uses the 's' command, not 'y':

  • first sed command (up to the semi-colon) replaces any instances of // with [

  • second sed command looks for lines starting with '[' and adds ']' at the end

     echo -e "hello//goodbye\nhello[goodbye\n//hello_and_goodbye\n[hello_and_goodbye"|\
     sed 's~//~[~g;/^\[/s~$~]~'
    
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .