Jump to content

dumb bash basename question

Go to solution Solved by elpiop,

is this what you're looking for? (each url needs to be on a new line, though)

while read i; do basename $i; done < urls.txt >> out.txt

 

How do I use basename to get multiple endings?
I want to get the end of this url (the number part) and put it into a file

twitch.tv/videos/231867291

I tried 

basename twitch.tv/videos/231867291


and it works perfectly

but when the text file has multiple URLS I can't extract the endings correctly

 

cat urls.txt | basename

//second attempt
//

read -r -a array <<< `cat urls.txt`

for element in "${array[@]}"
do
    basename $element >> file.txt
done

//this doesnt work? i tried to do basename $element, which would be URL



The only other way I can think of is a dumb stupid way, which would be appending the text "basename" to it with sed or awk, naming the file .sh and executing the script and using the output 
that obviously would be a retarded way to do it but how else can I do it?

 

can someone help me?

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to comment
https://linustechtips.com/topic/899833-dumb-bash-basename-question/
Share on other sites

Link to post
Share on other sites

2 minutes ago, elpiop said:

something like this?

while read i; do basename $i; done < urls.txt >> out.txt

So

while read i

do basename $i

done < urls.txt >> out.txt

 

How does the ending work?

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

1 minute ago, elpiop said:

what do you mean by ending?

"

done < urls.txt >> out.txt

"

specifically 

< urls.txt >> out.txt

 

what's going on here?

i understand the >> is to append to file w/o overwriting, but why the <?

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

3 minutes ago, elpiop said:

It redirects the contents of the file (urls.txt) to stdin

Why do you need to redirect it to stdin?

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

5 minutes ago, babadoctor said:

Why do you need to redirect it to stdin?

read

reads a line from the standard input stream, so if you want to use it with a file you need to redirect the contents of the file to stdin.

 

here's the man page for read

 http://linuxcommand.org/lc3_man_pages/readh.html

Link to post
Share on other sites

1 minute ago, elpiop said:

read

reads a line from the standard input stream, so if you want to use it with a file you need to redirect the contents of the file to stdin.

 

here's the man page for read

 http://linuxcommand.org/lc3_man_pages/readh.html

Okay I will take your word for it, since I can't find "stdin" using command f on the manpage to read it.

 

Thank you!

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×