943,754 Members | Top Members by Rank

Ad:
Mar 23rd, 2009
0

matching two consecutive identical characters in awk

Expand Post »
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 :
Quote ...
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() :
Quote ...
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Bhoot is offline Offline
53 posts
since Oct 2008
Mar 23rd, 2009
0

Re: matching two consecutive identical characters in awk

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
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 24th, 2009
0

Re: matching two consecutive identical characters in awk

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Bhoot is offline Offline
53 posts
since Oct 2008
Mar 25th, 2009
0

Re: matching two consecutive identical characters in awk

>I wrote the solution in awk using substr() :
Quote ...
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
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 27th, 2009
0

Re: matching two consecutive identical characters in awk

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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: writing part of files and filenames to a new file
Next Thread in Shell Scripting Forum Timeline: Need help writing a shell script to do the following:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC