awk word boundaries not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk word boundaries not working
# 1  
Old 12-12-2014
awk word boundaries not working

Hi,

I am trying below code but the word boundaries not seem to be working. What am I doing incorrectly?

Code:
 echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\bq\b/) print "HELLO" ; }'

OR

 echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\b'$q'\b/) print "HELLO" ; }'

Or 

 echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\<'$q'\>/) print "HELLO" ; }'

# 2  
Old 12-12-2014
echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ q) print "HELLO" ; }'
HELLO
# 3  
Old 12-12-2014
That's not what I was looking for. Your example wouldd match the string present anywhere. Hence wanted to check the word boundaries
# 4  
Old 12-12-2014
Try:
Code:
awk '/(^|[[:blank:]])ECHO([[:blank:]]|$)/{print "HELLO"}'

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 5  
Old 12-12-2014
Seems like variables cannot be used as part of a regex enclosed in slashes. Apart from that, \b is a escape sequence for backspace...

Try this:
Code:
$ echo " ECHO" | awk '{ q="ECHO" ; if ( $0 ~ " " q " " )  print "HELLO" ; }'
$ echo "ECHO " | awk '{ q="ECHO" ; if ( $0 ~ " " q " " )  print "HELLO" ; }'
$ echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ " " q " " )  print "HELLO" ; }'
HELLO
$ echo " ECHO  " | awk '{ q="ECHO" ; if ( $0 ~ " " q " " )  print "HELLO" ; }'
HELLO
$ echo " ECHO  " | awk '{ q="ECHO" ; if ( $0 == " " q " " )  print "HELLO" ; }'
$

Hope this helps.
# 6  
Old 12-12-2014
Don Cragun, I was working with variables and wanted to check the whole words only. Do you know how to do it?


Junior - helper , That's exactly what I was struggling with.


Apart from concatenation and checking the begining and end of strings, isnt there any other way of doing this?
# 7  
Old 12-12-2014
Code:
awk -v word="$variable" '$0 ~ "(^|[[:blank:]])" word "([[:blank:]]|$)"{print "found"}'

will print found if the expansion of $variable (treated as an extended regular expression) appears at the start of a line or follows a space or tab AND a space of tab follows it or it appears at the end of a line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

3. Shell Programming and Scripting

awk - why this is not working? trying next word!

Hi Experts, Can you please advise , why I am not able to make it work, or why this is not working: I spent quite a lot of time on this figuring out , but not working, file : This is a test file thanks for your reply This is another file again Have a nice day this is a small file... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

awk Script: removing periodic boundaries

SOLVED, thank you! Edit2: Good news everyone, I managed to get it down to a "simple" problem, but I still have some syntax issues. Here is the code which troubles me: awk 'BEGIN{x2=0;x1=0;crit=0;} $1 < 1000000 {x2=$4; diffx=x2-x1; x1=x2; diffx > 3.6 ? {crit=1} : {crit=0};... (2 Replies)
Discussion started by: Consti
2 Replies

5. Shell Programming and Scripting

sed working as uniq 1st word only

Hello, everyone. I am having trouble figuring out sed command which emulates uniq. The task I want to do is that 2 consecutive lines in file should be considered the same using the first word only. Example: cat tmp.txt ddd eee aaa bbb ccc ddd eee fff asd fdd asd fdd bbb aaa bbb asd fgh... (4 Replies)
Discussion started by: motorcek
4 Replies

6. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

7. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

8. UNIX for Dummies Questions & Answers

grep for word not working

Hi All..I need a help i am trying to find a word using below script whereas the word exists in my file nitin.txt as a directory but still i am getting "word not found" output..Your suggestions welcomed.: #to check for existence of nitin #!/bin/bash cd /apps/uat1/deploy/app ls -lrt >... (4 Replies)
Discussion started by: nattynitin
4 Replies

9. Programming

key_t type max length or boundaries value

Hello, In shared memory, when using shmget function, first parameter is ket_t key. I know it is an integer type, but length of it is system dependent. That means may not be have integer's ranges. What is range of key_t in Linux? Is it different in distros, for example in ubuntu & fedora? (2 Replies)
Discussion started by: pronetin
2 Replies

10. Shell Programming and Scripting

Word boundaries in GAWK?

I wanted to use GAWK's 'word boundary' feature but can't get it to work. Doesn't GAWK support \<word\>? Sample record: Title Bats in the fifth act of Chushingura (top); the... (6 Replies)
Discussion started by: Bubnoff
6 Replies
Login or Register to Ask a Question