avatarTeri Radichel

Summary

The article provides an overview of the cut command in bash scripts, explaining its use for parsing strings based on a delimiter to extract specific parts of data.

Abstract

The cut command is a powerful tool in bash scripting for parsing and extracting segments of text. The article illustrates how to use cut with a delimiter, such as a comma, to isolate desired fields from a string. It demonstrates practical examples, such as extracting "apples" from a comma-separated list of fruits and combining cut with other commands like sed for more complex parsing tasks. The author, Teri Radichel, emphasizes the utility of cut in automating data extraction processes and references additional resources for readers interested in learning more about command-line data manipulation.

Opinions

  • The author, Teri Radichel, finds the cut command particularly useful for parsing errors and extracting bits of logs or information in bash scripts.
  • Radichel suggests that the cut command is a valuable asset in automating cybersecurity metrics and provides a series of related content for further reading.
  • The article conveys that while cut is versatile, it may be more effective to use other commands like sed for certain tasks, such as removing a specific substring (e.g., ".git") from the end of a string.
  • The author encourages readers to follow for updates and to reach out for cybersecurity consulting services, indicating a belief in the importance of continuous learning and professional expertise in the field.

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

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

What if you want to get the string “apples,bananas”? Well you want fields 2 and three so:

cut -d "," -f2-3

But you could also just get all the fields from 2 until the end.

cut -d "," -f2-

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
Cut
Bash
Delimiter
Command Line
Script
Recommended from ReadMedium