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

above 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

to get this conclusion:

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 quite large, and I do not want to read it twice. Can I run sed code that executes two commands at once?

+5
source share
3 answers

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

sed -e <expr> -e <expr> -i <file>
+6
source

If I understand correctly, you want to replace /1/1with /1, as well as /2/2with /2:

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

Use a colon :instead /to avoid escaping.

+4
source

-e , . 10 -e .

If it gets even harder (or you need to create many mappings from some data), you can write the file and sedread its instructions from the file using the option -f:

sed -f sed.script -i file
+4
source

All Articles