cut: command-line tools #2

Welcome back to my command-line tools series. If you missed the first post, it’s here: grep. Today we’re talking about cut. I use it almost as much as I use grep

Unlike grep, where I have multiple use-cases, I mostly use cut for a single purpose. I use it to split a string into pieces and return the piece I want. My cut commands almost always use only 2 flags: -d and -f . The d flag sets the delimiter. The f flag sets the fields you want to output.

When using bug bounty tools, almost all of them return data delimited in some way (usually spaces or commas). For example, I use ffuf a lot. I always store the data from my scans. My output type of choice is csv because I’m no jq expert. When going back through the output to find interesting things, cut is invaluable. (Usually from within vim, like this :%!cut -d, -f1,2,6 but also from the command line like below)

~ ❯ cat output.csv 

~ ❯ cat output.csv | cut -d, -f2,5,6

As you can see, I was quickly able to pull out the url, status code, and response size (in bytes) of my output. I also prefer spaces over commas so I’ll replace all the commas with spaces either:

in vim with :%s/,/ /


from the command line: cat output.csv | cut -d, -f2,5,6 | sed 's/,/ /g'

Oh actually, one other use-case

While typing this up, I remembered another good use-case for cut. Sometimes I’ll want to strip off the last element of a path for all the urls in a file. Or I’ll want to strip off the top-level domain off a bunch of subdomains. I’m sure there are fancy ways to do this with regular expressions or awk, but here’s a really easy way to do this with cut that’s easy for me to remember. Here’s the command assuming / as a delimiter and stripping off the last element:

rev | cut -d/ -f2- | rev

rev reverses the line (for example a/b/c becomes c/b/a)

cut with / as the delimiter and -f2- means the second field to the end

rev reverses the line back to normal

Here it is in action:

~ ❯ cat test.txt
~ ❯ cat test.txt | rev | cut -d/ -f2- | rev

Notice how it chops off the last part of the path.

Thanks for reading, and I hope you found this useful. Next week, I’ll either cover sort , feature some of tomnomnoms tools, or show some useful bash aliases. Feel free to suggest something to me on twitter at . To be updated for the next post, either follow me on twitter or subscribe to the newsletter.

- rez0