| | |
matching two consecutive identical characters in awk
![]() |
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 :
However, as far as i know, tagged expression are not applicable in awk. I wrote the solution in awk using substr() :
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
i could write the solution in sed, using tagged regular expression, as follows :
•
•
•
•
sed -n '/^.\(.\)\1.*$/' emp.list
•
•
•
•
awk '{ if (substr($0,2,1) == substr($0,3,1)) print $0; }' emp.list
Also, is there any other way to write this in sed as well?
Thank you
Last edited by Bhoot; Mar 23rd, 2009 at 12:44 am. Reason: I forgot to quote the code.
Bhoot
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
You seem to be right; awk/gawk do seem to have limited REs.
But I do have a wiseacre solution.
But I do have a wiseacre solution.

Shell Scripting Syntax (Toggle Plain Text)
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.
Shell Scripting Syntax (Toggle Plain Text)
alias awk=egrep awk "^.(.)\1" emp.list
lol..i couldnt help myself from laughing when i saw your solution

a good one; but certainly not applicable for me

anyways thanks for that too
Bhoot
>I wrote the solution in awk using substr() :
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 '{ if (substr($0,2,1) == substr($0,3,1)) print $0; }' emp.list
Shell Scripting Syntax (Toggle Plain Text)
awk '{ split($1, a, ""); if(a[2] == a[3]) print }' emp.list
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
regular expressions are usually not needed for string manipulations.
Shell Scripting Syntax (Toggle Plain Text)
# echo "abcdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}' # echo "abbdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}' ok
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: writing part of files and filenames to a new file
- Next Thread: Need help writing a shell script to do the following:
| Thread Tools | Search this Thread |






