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
CodePudding user response:
You can use a python script like that.
def FileReader(file):
with open(file,'r') as f:
return f.readlines()
def ExtractBasename(file_name):
result = list()
data = FileReader(file_name)
for d in data:
result.append(d.split('/').pop())
return result
path = input("Enter a file: ")
print(*ExtractBasename(path), sep="\n")
Firstly, you write this code in file which has .py extension. Then you run it on terminal, which python's installed already, with python [file_name].py
, and you file name will be asked. Enter the file name, and see the results.
CodePudding user response:
Use xargs to pass content to a command
xargs -d '\n' -n1 basename <inputfile
Or use a read while loop, see https://mywiki.wooledge.org/BashFAQ/001