The usage of sed
3
0
Entering edit mode
9 weeks 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 • 392 views
ADD COMMENT
1
Entering edit mode
9 weeks 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}' |\
   tr '\n' '\t' > header.txt

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' 
ADD COMMENT
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.

ADD REPLY
0
Entering edit mode

a yes, thank, I'll fix it.

ADD REPLY
1
Entering edit mode
9 weeks 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

ADD COMMENT
1
Entering edit mode
9 weeks 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/|'
ADD COMMENT
0
Entering edit mode

that is what Pierre Lindenbaum did

ADD REPLY
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.

ADD REPLY

Login before adding your answer.

Traffic: 2575 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6