i have come across a question which asks to write an awk command that displays all records having 2nd and 3rd characters same.

i could write the solution in sed, using tagged regular expression, as follows :

sed -n '/^.\(.\)\1.*$/' emp.list

However, as far as i know, tagged expression are not applicable in awk. I wrote the solution in awk using substr() :

awk '{ if (substr($0,2,1) == substr($0,3,1)) print $0; }' emp.list

What i want to know is is there any other way in awk to solve this question? I just want to use a regular expression as i did in sed.
Also, is there any other way to write this in sed as well?
Thank you :)

You seem to be right; awk/gawk do seem to have limited REs.

But I do have a wiseacre solution. :)

alias awk=egrep
awk "^.(.)\1" emp.list

You seem to be right; awk/gawk do seem to have limited REs.

But I do have a wiseacre solution. :)

alias awk=egrep
awk "^.(.)\1" emp.list

lol..i couldnt help myself from laughing when i saw your solution :D
a good one; but certainly not applicable for me :)
anyways thanks for that too :)

>I wrote the solution in awk using substr() :

awk '{ if (substr($0,2,1) == substr($0,3,1)) print $0; }' emp.list

A slightly different approach. Since the targets to compare are found always in the beginning, use only the first field. Separate individual characters using split and then compare.

awk '{ split($1, a, ""); if(a[2] == a[3]) print }' emp.list

regular expressions are usually not needed for string manipulations.

# echo "abcdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}'
# echo "abbdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}'
ok
This article has been dead for over six months. Start a new discussion instead.