2

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

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.

Error.txt:

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

  • 2
    Add some sample text from the file including the line(s) that you want to edit and the expected output. – Nasir Riley Jun 13 at 11:33
  • check now i have added the file – Coolstrike Jun 13 at 11:39
  • 2
    If you're using a perl script to obtain user input, it's hard to see a good reason to shell out to sed for the substitutions – steeldriver Jun 13 at 11:39
  • I will take the input that on which line number does the user want to use the sed command. – Coolstrike Jun 13 at 11:40
  • This looks like wdiff output. What is the overall issue you are solving? – Kusalananda Jun 13 at 12:13
0

You can specify a condition in the form of e.g a search pattern:

sed -i -r '/^10?:/ {s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt

The substitutions are performed only if the line starts with 1: or 10:. This is the 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 condition can be on line numbers: this will only process the first line:

sed -i -r '1 {s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt

Since you mention Perl, you might prefer to use the following:

perl -i -pe 'if (/^10?:/) {s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt

or:

perl -i -pe 'if ($.==1 or $.==10) {s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt
  • For this code sed -i -r '1 {s/\{\+//; s/\+}//; s/[-.*-]//g}' error.txt ; Cant I use variable instead of 1? like $var=1 sed -i -r '$var {s/\{\+//; s/\+}//; s/[-.*-]//g}' error.txt – Coolstrike Jun 14 at 6:14
  • @Coolstrike Yes, you can use a variable, but you should edit your post with the exact question and requirements. It seems that you are exposing only a part of a bigger problem: if you want better answers you should give a more comprehensive picture (as was suggested in the comments). – simlev Jun 14 at 7:22
0

You can add a line or an range befor your sed command.

For your first example you can try for the 3rd line wich is numbered as #4:

sed "3 s/{+//" error.txt

Or if you need a script, you could work on this snippet:

#!/bin/bash

echo -e "Usage:\t ./my_sed.sh linenumber file\n"
sed "$1 s/{+//" $2
  • Can't I use a user input say a variable and then apply on the varaible value – Coolstrike Jun 14 at 4:51
  • Of course! You need to write a script to achive this. Review my edit, I give you a hint. – John Goofy Jun 14 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.