This is a test version of Biostars. For the public version, visit https://www.biostars.org.
The usage of sed
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

3 answers

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' 
replace all instances of "\t" with a tabulation

typo there

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

I thought carriage return is \r.

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

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/|'

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.

Log in to answer this question.