Basename the contents of a file
3
0
Entering edit mode
2.9 years ago

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 • 1.1k views
ADD COMMENT
4
Entering edit mode
2.9 years ago

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##*/} 
ADD COMMENT
4
Entering edit mode
2.9 years ago
$ 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
ADD COMMENT
2
Entering edit mode
2.9 years ago
jkim ▴ 190

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

Login before adding your answer.

Traffic: 2524 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