matching two consecutive identical characters in awk

Reply

Join Date: Oct 2008
Posts: 52
Reputation: Bhoot is an unknown quantity at this point 
Solved Threads: 1
Bhoot's Avatar
Bhoot Bhoot is offline Offline
Junior Poster in Training

matching two consecutive identical characters in awk

 
0
  #1
Mar 23rd, 2009
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
Last edited by Bhoot; Mar 23rd, 2009 at 12:44 am. Reason: I forgot to quote the code.
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: matching two consecutive identical characters in awk

 
0
  #2
Mar 23rd, 2009
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)
  1. alias awk=egrep
  2. awk "^.(.)\1" emp.list
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 52
Reputation: Bhoot is an unknown quantity at this point 
Solved Threads: 1
Bhoot's Avatar
Bhoot Bhoot is offline Offline
Junior Poster in Training

Re: matching two consecutive identical characters in awk

 
0
  #3
Mar 24th, 2009
Originally Posted by Fest3er View Post
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)
  1. alias awk=egrep
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: matching two consecutive identical characters in awk

 
0
  #4
Mar 25th, 2009
>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.
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ split($1, a, ""); if(a[2] == a[3]) print }' emp.list
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: matching two consecutive identical characters in awk

 
0
  #5
Mar 27th, 2009
regular expressions are usually not needed for string manipulations.
Shell Scripting Syntax (Toggle Plain Text)
  1. # echo "abcdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}'
  2. # echo "abbdd" | awk 'BEGIN{FS=""}$2==$3{print "ok"}'
  3. ok
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC