How can I use ‘cut’ to get URLs from my bookmarks?

0
1
Asked By CreativeFlame82 On

Hey everyone! I've got a file full of URLs and descriptions like this: `www.lol.com //description`. I want to set up a way to open those URLs using dmenu. Right now, I'm using this command: `cat ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:" | xargs -0 -I {} firefox "{}"`. The issue is that when the lines contain spaces, xargs takes the whole line including the description, causing it to fail. What I need is to make dmenu display the entire line but only grab the URL part when I select one. How can I do that?

4 Answers

Answered By TechieGuru99 On

You can use `cut` to fix this! Try modifying your command like this:

`cat ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:" | cut -d' ' -f1 | xargs -0 -I {} firefox "{}"`

Basically, this will let you show everything in the dmenu while ensuring only the URL is passed to Firefox. You could also skip `xargs` altogether using this:

`firefox "$(cat ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:" | cut -d' ' -f1)"`.

Answered By HelpfulFriend77 On

Thanks for the tips, everyone! I finally got it working. I just realized I needed to include the 'f1' option in the cut command. Great solution, TechieGuru!

Answered By CodeWhiz123 On

Here's another approach:

```bash
url=$(cat ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:")
url="${url% ///*}"
firefox "$url"
```
This gets the full line from dmenu first, then trims it down to just the URL before passing it to Firefox.

Answered By CuriousCoder88 On

I recommend using `cut -d' ' -f1` right after dmenu. This tells `cut` to treat spaces as delimiters and grab only the first part which should be the URL.

So instead of using `cat`, you might streamline the command like this instead:

```bash
firefox "$(cut -d' ' -f1 ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:")"
```
This ensures you're getting the URL after selecting an item without dropping what you want to display in dmenu.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.