sed replace command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace command
# 8  
Old 01-14-2017
Note: The use of \| for alternation is a GNU extension

Since one would need to use GNU sed anyway, one might as well use its -r option for extended regular expressions:
Code:
sed -r ':L; s/(^|[^&])(&)([^&]|$)/\1\2\2\3/g; tL' file

or
Code:
sed -r ':L; s/(^|[^&])&([^&]|$)/\1\&\&\2/g; tL' file


--
In regular sed:
Code:
sed -e 's/^&$/\&\&/; s/^&\([^&]\)/\&\&\1/; s/\([^&]\)&$/\1\&\&/' -e :L -e 's/\([^&]\)&\([^&]\)/\1\&\&\2/g; t L' file


Last edited by Scrutinizer; 01-15-2017 at 10:11 AM.. Reason: Adedd s/^&$/\&\&/ ; thanks Don. For clarity sake and to avoid back reference, used \& rather than & in sed replacement part
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 01-14-2017
Thanks as well. Could you please help me understand how the sed command works in this case?
# 10  
Old 01-15-2017
The regular sed command Scrutinizer suggested:
Code:
sed -e 's/^&\([^&]\)/\&\&\1/; s/\([^&]\)&$/\1\&\&/' -e :L -e 's/\([^&]\)&\([^&]\)/\1\&\&\2/g; t L' file

could also be written as:
Code:
sed -e 's/^&\([^&]\)/\&\&\1/
s/\([^&]\)&$/\1\&\&/
:L
s/\([^&]\)&\([^&]\)/\1\&\&\2/g
t L' file

The 1st substitute command (s/^&\([^&]\)/\&\&\1/) looks at the start of an input line (^) for a literal ampersand character (&) followed by a single character that is not an ampersand ([^&]) remembering the character that matched (because it is between a pair of escaped parenthese (\(...\)) and, if there was a match, replaces it with two literal ampersands (\&\&) and the string that was matched by the 1st expression found between escaped parenthese (\1).

The 2nd substitute command (s/\([^&]\)&$/\1\&\&/) performs equivalent logic looking for a match at the end of the line ($) instead of at the beginning of the line.

The :L creates a label (L) in the script that can be branched to later.

The 3rd substitute command (s/\([^&]\)&\([^&]\)/\1\&\&\2/g) looks for a character that is not an ampersand followed by a literal ampersand followed by a character that is not an ampersand and replaces them with the 1st non-ampersand character, two literal ampersand characters, and the 2nd non0-ampersand character. The g flag at the end of the command tells sed to make this substitution as many times as it can on the line (without the g flag, it would only make the 1st possible substitution). Note that with input like a&b&c&d&e, this substitution will double the ampersand characters after the a and c, but not after the b and d. This is because the b and d were matched as the 2nd non-ampersand character after the a and after the c and can't also be matched as the 1st non-ampersand character in the b&c and d&e substrings in the input.

The transfer command (t L) tells sed to transfer to the line in the script with the label L if and only if a substitute command successfully matched and substituted text since the last t command was executed. This lets it run the 3rd substitute command again if it made one or more substitutions the 1st time it was processed.

It wasn't clear to me from your discussion whether or not a line that contains just a single ampersand character and nothing else is supposed to double that ampersand. The script above does not double an ampersand in this case. If you do want to double an ampersand that is the only character on an input line, you could add another substitute command to take care of that case:
Code:
s/^&$/&&/

Note that the ampersands in the replacement pattern do not have to be escaped in this case. An unescaped & in the replacement string in a substitute command is replaced by the entire string matched by the basic regular expression pattern in the substitute command. Since the string matched in this case is just an & the replacement strings && and \&\& produce identical results.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 01-15-2017
Quote:
Originally Posted by Don Cragun
[..]
It wasn't clear to me from your discussion whether or not a line that contains just a single ampersand character and nothing else is supposed to double that ampersand. The script above does not double an ampersand in this case. If you do want to double an ampersand that is the only character on an input line, you could add another substitute command to take care of that case:
Code:
s/^&$/&&/

Note that the ampersands in the replacement pattern do not have to be escaped in this case. An unescaped & in the replacement string in a substitute command is replaced by the entire string matched by the basic regular expression pattern in the substitute command. Since the string matched in this case is just an & the replacement strings && and \&\& produce identical results.
Thanks, Don for spotting the bug. Corrected in my post..
# 12  
Old 01-16-2017
Making use of sed's unescaped &, the Unix-(=all-)sed solution becomes
Code:
sed -e 's/^&$/&&/; s/^&[^&]/\&&/; s/[^&]&$/&\&/' -e :L -e 's/\([^&]\)&\([^&]\)/\1\&\&\2/g; t L' file

Another solution
Code:
sed -e 's/^/ /; s/$/ /' -e :L -e  's/\([^&]\)&\([^&]\)/\1\&\&\2/g; t L' -e 's/^ //; s/ $//' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Command to replace particular value.

Hi , My input file contain : list = 3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29 Expected output : list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0 I want to keep the 8,10,16,17,22 value from the list and put 0 on rest of the... (9 Replies)
Discussion started by: Preeti Chandra
9 Replies

2. Shell Programming and Scripting

Need to log the sed command used to replace

In a shell script I am replacing the asterisks in a file: sed "s/\*/"0"/g" /home/download/$COMPANY_CODE/file_new > /home/download/$COMPANY_CODE/fileI need to log which positions were replaced & position(01:20) from the line it was replaced in. I am not sure how to do so. Also, instead of... (11 Replies)
Discussion started by: tomj5141
11 Replies

3. Shell Programming and Scripting

Find and replace using sed command

The content of the file filea.txt is as follows. --------- case $HOSTNAME in aaa) DS_PARM_VALUE_SET=vsDev APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt ;; bbb) DS_PARM_VALUE_SET=vsQA... (3 Replies)
Discussion started by: kmanivan82
3 Replies

4. Shell Programming and Scripting

sed command to find and replace

Hello All, I need a sed command to find and replace below text in multiple files in a directory. Original Text :- "$SCRIPT_PATH/files" Replace with :- "$RESOURCE_FILE" Thank you in advance !!! Regards, Anand Shah (1 Reply)
Discussion started by: anand.shah
1 Replies

5. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

6. Shell Programming and Scripting

search and replace with sed command

hi, suggest me in the below script.. if In above I wanna replace "" with "]". (2 Replies)
Discussion started by: divya bandipotu
2 Replies

7. Shell Programming and Scripting

sed command to replace a character at last

Hi All, I have a file having one line only. It is like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037| I want to replace the numbers in last two columns by As. It should look like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA| Please, suggest me any shell... (12 Replies)
Discussion started by: mukeshbaranwal
12 Replies

8. Shell Programming and Scripting

Replace with a variable in sed command

Hello, I have this command and it works fine. My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed. Code: $ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Discussion started by: rany1
2 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

sed search and replace command

Hi, I would like to seek help on how i can arrive on this result. sample.txt: product_code IN (param001) and product_type IN (param004) product_code IN (param002) and product_type IN (param005) product_code IN (param003) and product_type IN (param006) I would like to change the param001... (1 Reply)
Discussion started by: janzper
1 Replies
Login or Register to Ask a Question