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:
or
--
In regular sed:
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:
The regular sed command Scrutinizer suggested:
could also be written as:
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:
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:
[..]
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:
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..
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)
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)
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)
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)
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)
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)
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)
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)