0

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
Mike
  • 921
  • 7
  • 26

2 Answers2

1

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.

Recep Gunes
  • 157
  • 1
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '21 at 21:45
1

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

KamilCuk
  • 120,984
  • 8
  • 59
  • 111