Skip to content
Advertisement

Doing an awk command for a conditional statement

I’m doing a bash script using the Awk command to format a file with some user info, and at the beginning of the script, I want it to say “if the value in column 1 is not in the format abc123, skip that line. It doesn’t seem to be working though.

I had this previously:

$1=="UID" || $1=="rslavin" {
  next
}

but of course that wouldn’t work for other input files with different usernames.

So, this is what I’ve tried most recently:

$1!=([a-z]{3}[0-9]{3}) {

 next

}

The input file looks like:

1 UID PID PPID C STIME TTY TIME CMD
2 adz110 5344 5334 0 08:47 pts /2 00:00:00 bash
3 dmq292 6908 6854 0 Jun04 pts /1 00:00:00 bash
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 who
5 erg474 7466 7461 0 08:54 pts /10 00:00:00 ls
6 dmq292 7966 7960 0 Jun04 pts /13 00:00:00 assign1 . sh if of
7 xle135 8983 8636 0 08:59 pts /15 00:00:00 ssh ctf . cs . utsarr . net
8 zeh458 9057 1980 0 08:59 pts /7 00:00:00 vim prog . c
9 rslavin 9150 9139 0 08:59 pts /16 00:00:00 ps - af
10 xle135 8636 8628 0 08:58 pts /15 00:00:00 bash

Advertisement

Answer

With your shown Input_file the column which we are talking about is $2 NOT $1, so ideally it should be:

awk '$2 !~ /[a-z]{3}[0-9]{3}/{next}.........' Input_file

Also in case you want to check small and capital both letters then try:

awk '$2 !~ /[a-zA-Z]{3}[0-9]{3}/{next}..........'  Input_file
Advertisement