This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Basename the contents of a file

I've got a file that contains a list of file paths. I'd like to apply basename to the contents of the file.

File contents look like this:

ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239280/NA07037.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239286/NA11829.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239293/NA11918.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239298/NA11994.final.cram

And I'd like to do something like this:

cat cram_download_list.txt | basename

To obtain something like this:

NA07037.final.cram
NA11829.final.cram
NA11918.final.cram
NA11994.final.cram
basename linux

3 answers

you'll need to put that in a loop (for loop for instance)

mock code:

for line in (`cat cram_download_list.txt) do
  basename $line
done

or use bash parameter operations: (in the example above replace basename line with the following:

${line##*/} 
$ awk -F "/" '{print $NF}' file.txt 
$ sed -r 's_.*/__' file.txt
$ cut -f8 -d"/" file.txt
$ datamash basename 1 < file.txt
$ colrm 1 51 <file.txt
$ xargs -n 1  basename  < test.txt
$ parallel echo {/} :::: test.txt

NA07037.final.cram
NA11829.final.cram
NA11918.final.cram
NA11994.final.cram

Another one.

➜  tmp cat cram_download_list.txt
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239280/NA07037.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239286/NA11829.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239293/NA11918.final.cram
ftp://ftp.sra.ebi.ac.uk/vol1/run/ERR323/ERR3239298/NA11994.final.cram
➜  tmp cat cram_download_list.txt| xargs basename
NA07037.final.cram
NA11829.final.cram
NA11918.final.cram
NA11994.final.cram

Log in to answer this question.