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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I use
cut
to split and parse content a lot. The syntax is short, sweet and easy to remember: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:But it does so much more. Since we're talking about searching text with
find
,sed
andgrep
,awk
lets you search for text based on its specific position in a file:Test can be combined:
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.ps -ef
) with PID and Parent PID in columns 2 and 3 respectively;|
) output toawk
;$VAR
and$$
toawk
variablesprogram
andmysid
;&&
; the "or" operator is||
): ** Check columns 2 and 3 do not matchmysid
(!= mysid
); ** Check the whole line ($0
) for a match withprogram
;'{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). Usebasename
to get the script name, look for other processes running it andexit
. As a bonus, exclude processes editing the file and not running it.