DEV Community

Discussion on: The linux commands that help me work

Collapse
oraclesean profile image
Oracle Sean ♠️

I use cut to split and parse content a lot. The syntax is short, sweet and easy to remember:

cat file.csv | cut -d, -f2

This prints the second field (-f2) delimited by a comma (-d,). Useful for parsing CSV files.

awk is wonderfully powerful, too. Use it for simple result printing and formatting:

awk '{print $2}' file.csv # Print the second column, using the default space delimiter
awk -F',' '{print $2}' file.csv # Print the second column, using the comma as the delimiter

But it does so much more. Since we're talking about searching text with find, sed and grep, awk lets you search for text based on its specific position in a file:

awk -F',' '$2 == Jones {print $0}' file.csv # Print lines where the second field = Jones
awk -F',' -v x="$VAR" '$2 == x {print $0}' file.csv # Print lines where the second field = a shell variable $VAR; variables are assigned/passed using the `-v` flag.

Test can be combined:

ps -ef | awk -v program="$VAR" -v mysid="$$" '$2 != mysid && $3 != mysid && $0 ~ program {print $0}'

This shows all running processes not associated with your own PID that match the shell variable $VAR. $$ is a special shell construct containing the session's process ID.

  • Show processes (ps -ef) with PID and Parent PID in columns 2 and 3 respectively;
  • Pipe (|) output to awk;
  • Assign $VAR and $$ to awk variables program and mysid;
  • Combine three tests with the "and" operator (&&; the "or" operator is ||): ** Check columns 2 and 3 do not match mysid (!= mysid); ** Check the whole line ($0) for a match with program;
  • Print the entire matching line ('{print $0}') when all three conditions are met. Use this to test whether another process is already running a script (without using a lock file). Use basename to get the script name, look for other processes running it and exit. As a bonus, exclude processes editing the file and not running it.