sed replace command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace command
# 1  
Old 01-13-2017
sed replace command

Hi.

I need to append/prefix an & character to every 'single' & character (not when there are 2 or more grouped together) I find in a file. I can do it using this cmd:
Code:
cat ${file} | sed -e 's/&/&&/g' > ${new_file}

How can I modify this to ensure I only replace single &'s and not operate on multiple groupings like && or &&& etc.?

Many thanks.

Last edited by rbatte1; 01-13-2017 at 09:45 AM.. Reason: Added ICODE tags
# 2  
Old 01-13-2017
Try
Code:
sed 's/\([^&]\)\(&\)\([^&]\)/\1\2\2\3/g' file

# 3  
Old 01-13-2017
Thanks Rudi. It only seems to partially work for me. looks like it only updates single &s if they're on a line with other &s.
Here's the output:

Code:
[oracle@xtest54 bin]$ cat test1
&

&

&&

&&&

& & &
&& && &&&&&&
[oracle@xtest-54 bin]$ sed 's/\([^&]\)\(&\)\([^&]\)/\1\2\2\3/g' test1
&

&

&&

&&&

& && &
&& && &&&&&&
[oracle@xtest-54 bin]$

# 4  
Old 01-13-2017
Had you posted a meaningful input sample, a better solution could have been offered. Try
Code:
sed ':L; s/\(^\|[^&]\)\(&\)\([^&]\|$\)/\1\2\2\3/g; tL' file
&&

&&

&&

&&&

&& && &&
&& && &&&&&&

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-13-2017
Perfect. Yes, I should've added an example to be clear. Sorry for that and thanks for the solution.
# 6  
Old 01-13-2017
I find sed scripts with a lot of escapes much more readable if other separator is used.


Same code Rudi posted is much more readable and does the same :
Code:
sed ':L; s#\(^\|[^&]\)\(&\)\([^&]\|$\)#\1\2\2\3#g; tL'

In this case it's hash, but it can be anything but the patterns required for transformation.
These 2 Users Gave Thanks to Peasant For This Post:
# 7  
Old 01-13-2017
With perl's lookbehind and lookahead:
Code:
perl -pe 's/(?<!&)&(?!&)/&&/g' file

This User Gave Thanks to MadeInGermany For This Post:
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