The usage of sed
3
0
Entering edit mode
10 months ago
bill • 0
sed -e 's/_scATAC_hg19_noDup_noMT.bam//g' -e 's/\/directory\/to\/singleCell\///g' bamlist.txt | sed -e 's/\//\t/g' | awk 'OFS="\t"{print $2}' | tr '\n' '\t' > header.txt  This replacement command is too complex. Can someone explain what this means? linux sed shell • 666 views ADD COMMENT 1 Entering edit mode 10 months ago sed -e 's/_scATAC_hg19_noDup_noMT.bam//g' -e 's/\/directory\/to\/singleCell\///g' bamlist.txt |\ sed -e 's/\//\t/g' | \ awk 'OFS="\t"{print$2}' |\


replace all instances of "_scATAC_hg19_noDup_noMT.bam" with empty string

replace all instances of "/directory/to/singleCell/" with empty string

replace all instances of "/" with a tabulation

print the 2nd column

convert all instances of <carriage-return> with a tabulation

which I would write as

sed 's%_scATAC_hg19_noDup_noMT.bam%%g;s%/directory/to/singleCell/%%g' | tr "/" "\n"  | cut -f 2 |  tr '\n' '\t'

1
Entering edit mode
replace all instances of "\t" with a tabulation


typo there

replace all instances of "/" with a tab (\t)


I thought carriage return is \r.

0
Entering edit mode

a yes, thank, I'll fix it.

1
Entering edit mode
10 months ago

This command

1. Removes two words: "_scATAC_hg19_noDup_noMT.bam" and "/directory/to/singleCell/" (all occurrences) from bamlist.txt
2. Then it replaces all occurrences of "/" with a tab ("\t")
3. It prints second column, from the output from step2, and I think output field separator is not necessary here.
4. Collapses all the lines from output from 3 , into a row with tab separated values.

I think -e 's/\/directory\/to\/singleCell\///g' bamlist.txt | sed -e 's/\//\t/g' | awk 'OFS="\t"{print \$2}' | tr '\n' '\t' is not necessary to be complex and a simple code may get the same result. Please post few lines from bamlist.txt

1
Entering edit mode
10 months ago

Not answer but I wanted to point out that in a sed script the pattern and replacement do not need to be separated by /. (Almost) any character will do which is useful to make scripts more readable. E.g.

echo "foo/bar/spam" | sed 's/\/bar\//\/eggs\//'


is more readable in this case if you use | as separator:

echo "foo/bar/spam" | sed 's|/bar/|/eggs/|'

0
Entering edit mode

that is what Pierre Lindenbaum did

0
Entering edit mode

Oh, I see it now...! He uses %. I don't mind deleting my answer/comment but I think it's useful to have it explicitly stated since I have seen (and done) a lot of /-escaping mess before realizing this! I would suggest Pierre to add a note to his answer about it.