Geparada
Geparada

Reputation: 3038

running sed with two instructions

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?

Upvotes: 5

Views: 245

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 754020

In this example with just two edits to make, you can use -e or the semi-colon in between operations quite easily, as shown in other answers. I have scripts with 10 or more -e options on successive lines.

If it gets more complex still (or you need to generate many mappings from some data), you can write a file and have sed read its instructions from the file with the -f option:

sed -f sed.script -i file

Upvotes: 4

Hai Vu
Hai Vu

Reputation: 40733

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.

Upvotes: 5

jcollado
jcollado

Reputation: 40394

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

sed -e <expr> -e <expr> -i <file>

Upvotes: 6

Related Questions