2

I have a bash script called test.sh that uses command-line argument:

lynx -dump $1 > $name".txt"

But I need name to be created from the argument by specific keywords in the argument.

An example is:

http://www.pcmag.com/article2/0,2817,2412941,00.asp
http://www.pcmag.com/article2/0,2817,2412919,00.asp

Both are two separate articles but are the difference can only be seen in those 12 characters. How do I create a variable from a url for those 12 characters? So that when I run test.sh in Terminal:

./test.sh http://www.pcmag.com/article2/0,2817,2412941,00.asp 

there is a text file saved as 0,2817,2412941,00?

2 Answers 2

1

You can do it in awk like this:

$ name=$(echo $1 | awk -F[/.] '{print $(NF-1)}')
$ echo $name
0,2817,2412941,00

By using / and . as delimiters, the stuff you want will be present in the last but one field which can be accessed using NF-1

0
1

You can use the basename command:

basename http://www.pcmag.com/article2/0,2817,2412919,00.asp ".asp"

which will give you:

0,2817,2412919,00
2
  • This works for one link but I'm dealing with over 100 links and the job would become tedious. But thank you nevertheless! Dec 7, 2012 at 22:42
  • You could of course also use another line to get the extension first, and then use it in this line. But then you're indeed better of with Gurus solution. Dec 8, 2012 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .