The cut command
A useful command in bash scripts you can use to parse data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
⚙️ Check out my series on Automating Cybersecurity Metrics | Code.
🔒 Related Stories: Bugs | AWS Security | Secure Code
💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
While trying to parse out errors and bits of logs or information in bash scripts I often find myself using cut. I thought I’d write a quick summary of how that command works here.
The cut command allows you to obtain a piece of a string based on a delimiter. Here’s the gist of it.
A delimiter can be any character such as a comma. It is the character that separates the parts of the string you want to get into a variable of view on the screen.
Let’s say you have a value like this:
apples,organges,bananas
The delimiter is the comma.
Let’s say you want to get the “apples” out of that string.
Set the variable to the value:
fruits="apples,oranges,bananas"
Echo the value to screen:
echo $fruits
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-dUug0UoMRVe_IHVp1pM3g.png)
Add a pipe character after the echo command which allows us to send the output of that command to some other command:
echo $fruits |
Now we’re going to add the cut command to get the value after the first comma.
- Add the command (cut)
- Add the delimiter by adding -d followed by the single character in quotes that is the delimiter.
- Add -f which indicates which fields you want to return.
- Add the field number (2) that you want to return.
echo $fruits | cut -d "," -f2
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9RbtEOjNwlNuIOprM4VPvA.png)
What if you want to get the string “apples,bananas”? Well you want fields 2 and three so:
cut -d "," -f2-3
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FoVc_YXF0ityDDhUq0P3QA.png)
But you could also just get all the fields from 2 until the end.
cut -d "," -f2-
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yQKDpJaE6u3UTwLN8u7UfQ.png)
You can also combine the cut commands with other commands like I’m doing here where I know the last word before .git in a git repository is the fifth field.
dir=$(echo $repo | cut -d "/" -f5 | sed 's/\.git//')
However what I really want to do in the last example is to get everything after the last / and then remove .git to get the directory name. So that might be better achieved with another command like sed.
Follow for updates.
Teri Radichel | © 2nd Sight Lab 2024
About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
![](https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4oxP4LXk8l8c3mpRvO7ejg.png)