3

I have a file.txt with some urls and the name of the web. Something like:

WEB1: www.url.com
WEB2: www.url2.com

(No space in the name, and there is a space after the colon) I want to be able to open a tab in Firefox for an specific web in that file. I have tried something like this:

xargs -a file.txt firefox -new-tab "$line"

This opens every word in the document as a new tab. I want to be able to select which url to open using the Web's name. I would open a single url at a time. The file would be around 100 lines long or so. Is there any simple way of doing this? Thanks!

As suggested from Bodo here is some details:

  • There is a file urls.txt in your HOME directory that contains lines as shown in the question.
  • The key and the URL are separated by a colon and a space (: ).
  • You want to run a command openurl foo bar baz where foo, bar, baz are keys in the file like WEB1, WEB2 without the trailing colon (:).
8
  • Is it correct that the key/name like WEB 2: can contain spaces? Is the URL always separated from the name by a colon and a space ": "? Please clarify your statement "I want to be able to select which url to open using the Web's name." Show an example command and/or example input how you want to specify which URL (or URLs ?) to open. Please don't use comments to answer, edit your question instead.
    – Bodo
    Feb 22 at 10:01
  • I chose to format the file like that, but it is not necessary. If it is easier writing WEB2 without spaces I'll change it and the same with the colon and space. I just need to be able to choose the url using the name, I can change the file.
    – Ramon
    Feb 22 at 10:16
  • I don't want you to change the requirements but to make them clear. The space became more obvious after my edit changing the formatting, that's why I asked for clarification. The solution may be different depending on your specification, which not necessarily means easier or harder to implement. Artem S. Tashkinov's answer would not work with a space in the name or without a space after the colon, but it can easlily be adapted. The main point is to make your requirements clear in your question.
    – Bodo
    Feb 22 at 10:23
  • Ok, so right now It has no space in the name: WEB1. And there is a space after the colon
    – Ramon
    Feb 22 at 10:30
  • You still did not clarify your requirement "I want to be able to select which url to open using the Web's name." Please show examples what exactly you want to specify to open www.url.com or www.url2.com and how you want to do this. Will you always select a single URL or maybe also multiple URLs? (Possible ways to specify the URL(s): command line argument(s), input file, interactive input...)
    – Bodo
    Feb 22 at 10:36
3

You can use awk and xargs:

awk -F ': ' '$1 == "WEB2" {print $2}' file.txt | xargs -I{} firefox -new-tab {}

-F ': ' lets awk separate the fields by colon followed by a space. This sequence should not appear in the urls.

2

The answer is based on the requirements defined in the question.

You can create a shell script named openurl:

#! /bin/sh

for i in "$@"
do
    grep "^$i": "$HOME"/urls.txt
done | sed 's/.*: //' | while read url
do
    firefox -new-tab "$url"
done

When you run openurl WEB1 it should open www.url.com, When you run openurl WEB1 WEB2 it should open both URLs from the example.

Keys that don't exist in the file, will be ignored. That means e.g. openurl WEB1 WEB3 will open www.url.com and do nothing about WEB3.

Of course you can also create a function openurl instead of a shell script.

If the file that contains the table of URLs would be very big (much more than 1000 lines) or if you want to specify many URL keys (much more than 10) the simple approach might get slow. In this case it might be better to use a different approach to extract all matching lines from the file in a single pass instead of running grep multiple times for every command line argument.


The part how to open the URLs as tabs is copied from Artem S. Tashkinov's answer.

1
awk '{print $2}' file.txt | grep -v ": " | while read url; do firefox -new-tab "$url"; done

Adding | grep -v ": " because the OP indicated " are keys in the file like WEB1, WEB2 without the trailing colon (:)."

1
  • 1
    This doesn’t address this part of the question: “I want to be able to select which url to open using the Web's name.” Feb 22 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.