Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running sed with two instructions

Tags:

bash

sed

I wanted to run these sed lines:

sed 's/\/1\/1/\/1/g' file -i && sed 's/\/2\/2/\/2/g' file -i

over a file like this:

chr9_paternal   126628489       126629719       616L7AAXX_HWUSI-EAS627_0005:1:1:1157:5733/1/1   0       +       126628489       126629719       255,0,0 2
       19,57   0,1173
chr20_paternal  34093622        34093697        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:4527/1/1   0       +       34093622        34093697        255,0,0 1
       75      0
chr17_paternal  44627748        44633513        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:5412/2/2   0       +       44627748        44633513        255,0,0 2
       36,40   0,5725
chr1_paternal   224204536       224204611       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:2312/1   0       +       224204536       224204611       255,0,0 1
       75      0
chr7_paternal   132309510       132309585       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:1687/2/2   0       +       132309510       132309585       255,0,0 1
       75      0
chr20_paternal  45708069        45708144        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:13916/1  63      +       45708069        45708144        255,0,0 1
       75      0
chr9_paternal   134850662       134850737       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:10141/1/1  72      +       134850662       134850737       255,0,0 1
       75      0
chrX_paternal   71603273        71603348        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:8367/2   30      +       71603273        71603348        255,0,0 1

in order to get this output:

chr9_paternal   126628489       126629719       616L7AAXX_HWUSI-EAS627_0005:1:1:1157:5733/1   0       +       126628489       126629719       255,0,0 2
       19,57   0,1173
chr20_paternal  34093622        34093697        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:4527/1   0       +       34093622        34093697        255,0,0 1
       75      0
chr17_paternal  44627748        44633513        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:5412/2   0       +       44627748        44633513        255,0,0 2
       36,40   0,5725
chr1_paternal   224204536       224204611       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:2312/1   0       +       224204536       224204611       255,0,0 1
       75      0
chr7_paternal   132309510       132309585       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:1687/2   0       +       132309510       132309585       255,0,0 1
       75      0
chr20_paternal  45708069        45708144        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:13916/1  63      +       45708069        45708144        255,0,0 1
       75      0
chr9_paternal   134850662       134850737       616L7AAXX_HWUSI-EAS627_0005:1:1:1158:10141/1  72      +       134850662       134850737       255,0,0 1
       75      0
chrX_paternal   71603273        71603348        616L7AAXX_HWUSI-EAS627_0005:1:1:1158:8367/2   30      +       71603273        71603348        255,0,0 1

But the file is pretty big and I don't want to read it twice. It is possible run a sed code that execute two instructions at once?

like image 845
Geparada Avatar asked Mar 08 '12 16:03

Geparada


People also ask

How run multiple commands using sed?

There are several methods to specify multiple commands in a sed program. Using newlines is most natural when running a sed script from a file (using the -f option). The { , } , b , t , T , : commands can be separated with a semicolon (this is a non-portable GNU sed extension).

Which option is used to combine multiple sed commands?

In this article let us review how to combine multiple sed commands using option -e as shown below. Note: -e option is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer.

Which character can be used to separate two commands on the same line in Linux?

The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds. For example, open a Terminal window (Ctrl+Alt+T in Ubuntu and Linux Mint). Then, type the following three commands on one line, separated by semicolons, and press Enter.


2 Answers

You can use -e to execute multiple expressions in one sed call:

sed -e <expr> -e <expr> -i <file>
like image 190
jcollado Avatar answered Oct 05 '22 23:10

jcollado


If I understood it correctly, you want to replace /1/1 with /1 and likewise, /2/2 with /2:

sed -i 's:/1/1:/1:;s:/2/2:/2:' file

Use the colon : instead of / so you can avoid escaping.

like image 36
Hai Vu Avatar answered Oct 05 '22 23:10

Hai Vu