Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.

Use sed commands on multiple lines

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Use sed commands on multiple lines

I have a text file and i want to run 3 sed commands for the lines entered by the user using perl script. I am doing this manually till now.

need some help with this

The sed commands I have to use are :

sed -i "s/{+//" error.txt
sed -i "s/+}//" error.txt
sed -i "s/\[-.*-]//g" error.txt

error.txt is the file where I want to run these commands but i want them to run on a particular line which i will enter


1:module counter ( clk, reset, enable, {+dat_out+} );

4: input clk, [-reset ;-] {+reset, enable;+}

5: wire {+N69, N70, N71,+} N72, N73, N74, N75, N76, N77, N78, N79, N80,

10: EDFFX2AD \dat_out_sig_reg[0] ( [-.D(synopsis_unconnected),-] {+.D(N69),+} .E(N96), .CK(clk), .Q(dat_out[0])

12: EDFFX2AD \dat_out_sig_reg[1] ( {+.D(N70),+} .E(N94), .CK(clk), .Q(dat_out[1])

14: EDFFX2AD \dat_out_sig_reg[2] ( .D(N71), [-.E(synopsis_unconnected),-] {+.E(N92),+} .CK(clk), .Q(dat_out[2])

expected output:

1:module counter ( clk, reset, enable, dat_out );

4: input clk, [-reset ;-] {+reset, enable;+}

5: wire {+N69, N70, N71,+} N72, N73, N74, N75, N76, N77, N78, N79, N80,

10: EDFFX2AD \dat_out_sig_reg[0] ( .D(N69), .E(N96), .CK(clk), .Q(dat_out[0])

12: EDFFX2AD \dat_out_sig_reg[1] ( {+.D(N70),+} .E(N94), .CK(clk), .Q(dat_out[1])

14: EDFFX2AD \dat_out_sig_reg[2] ( .D(N71), [-.E(synopsis_unconnected),-] {+.E(N92),+} .CK(clk), .Q(dat_out[2])

The codes are applied on Line 1 and Line 10. Please suggest something

Last edited by Scrutinizer; 4 Weeks Ago at 05:17 AM.. Reason: code tags correction
# 2  
You can add line number like this to run substitute only on first line
sed -i "1{s/{+//;s/+}//;}" error.txt

# 3  
Try also
sed '1bL; 10bL; b; :L; p; s/{+\|+}\|\[-.*-]//g;' file

EDIT: removed p; leftover from testing / debugging:

sed '1bL; 10bL; b; :L; s/{+\|+}\|\[-.*-]//g;' file

Last edited by RudiC; 4 Weeks Ago at 05:23 AM..
# 5  
Yes but use double quotes for variable substitution
sed "${var}..." file

# 6  
Originally Posted by utkarshkhanna44
cant i use a variable instead of that 1?
In principle you can, but sed uses a lot of characters with a special meaning to the shell. Therefore, to protect the shell from reading it, sed-commands are usually enclosed in single-quotes:

sed '<...sed-commands...> ' /some/file

This, in turn, has a side effect: the shell ignores anything inside single-quotes and hence variables are not expanded. If you write something like:

command $var

then prior to executing the line the shell replaces "$var" with the content of the variable. This is not the case inside single quotes. You can use double quotes instead ("), but this may have side effects too (i.e. "*" means something to the shell it might expand it), therefore you should use a mix of single- and double-quoting:

sed '<...sed-commands...> '"$var"'<...more sed-commands...>' /some/file

This looks cumbersome at first but offers the best protection and the most robustness. Notice that your variables can contain i.e. numbers and other simple things but under no circumstances you should put parts of regexps and special characters into your variables! Even if it works this is a great recipe for disaster.

I hope this helps.

These 3 Users Gave Thanks to bakunin For This Post:
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Using sed to execute multiple commands
Let's say I have a file called test.out. In this file I want to do the following: 1. Search for DIP-10219 and with this: 2. Remove everything in front of cn= 3. Remove everything after *com 4. Remove duplicate lines 5. Replace ( with \( 6. Replace ) with \) For 1-3 I have figured out this...... Shell Programming and Scripting
Shell Programming and Scripting
Variables into SED or AWK and multiple commands
Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used...... Shell Programming and Scripting
Shell Programming and Scripting
Can I spread commands over multiple lines?
Below an example of what I mean. The first attempt does what I want; the second doesn't, because bash assumes a line break means the end of an individual "command unix". Is there some way that I can convince bash to parse out, eg, to the closing parenthesis? I'm thinking this would allow for...... UNIX for Dummies Questions & Answers
UNIX for Dummies Questions & Answers
multiple sed commands
hello! I have a few sed commands sed '/^$/d' < $1 > tmp.t sed '/^ \{3,\}/d' < tmp.t > tmp1.txt ..... how can I write them in a single line? sed '/^$/d' < $1 > | '/^ \{3,\}/d' < $1 > tmp1.txt any idea? thanks.... Shell Programming and Scripting
Shell Programming and Scripting
sed: Multiple Commands applied to an address
Trying to write a sed command that applies multiple replacements to a specific address. Need a second pair of eyes I guess cause my syntax appears to be correct (obviously not though) I am getting an error. Any Help would be appreciated! Thanks in advance. sed -f foo envOracle sed: Function...... Shell Programming and Scripting
Shell Programming and Scripting

Featured Tech Videos