LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2020, 12:44 PM   #1
Pinguino99
LQ Newbie
 
Registered: Dec 2019
Posts: 29

Rep: Reputation: Disabled
Question AWK - Word search


hi, i have a little problem in the code, if don't find the word it doesn't change to second condition

What is wrong?

File
Code:
1: zzz|zzz|bbb|[X]|999|222    
2: xxx|xxx|bbb|[Y]|999|222
Code:
FILE=~/path/file

read -p "word: " STRING
if
awk -F '|' '{if($4 && NR==1) print $4~/^\['$STRING']/}' $FILE ; then
   echo "found" 
else
   echo "not found"
fi

Last edited by Pinguino99; 06-13-2020 at 02:56 PM.
 
Old 06-13-2020, 02:32 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,422

Rep: Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027Reputation: 2027
Please don't use QUOTE tags for code. It does not preserve formatting, and makes it hard to quote your code in a response. Use [CODE]...[/CODE] tags instead (the "#" sign in the advanced menu).
Quote:
Originally Posted by Pinguino99 View Post
Code:
awk -F '|' '{if($4 && NR==1) print $4~/^\['$STRING']/}' $FILE
Your awk script is just printing 0 or 1, and always exits with a 0 return code. The "if" construct only cares arout the return code. A simple fix:
Code:
awk -F '|' 'END {exit found==0} {if($4 && NR==1) found = $4~/^\['$STRING']/}' $FILE
 
Old 06-13-2020, 02:54 PM   #3
Pinguino99
LQ Newbie
 
Registered: Dec 2019
Posts: 29

Original Poster
Rep: Reputation: Disabled
thank very much!!!
 
Old 06-14-2020, 04:23 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,789

Rep: Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059
Of course we don't need both bash and awk here, but as this is the exercise, here is an alternative to utilise both:
Code:
FILE=~/path/file

read -p "word: " STRING

if [[ -n "$(awk -F'|' -vstr=$STRING 'NF >= 4 && NR == 1 && $4 ~ "["str"]"' "$FILE")" ]]
then
  echo "found"
else
  echo "not found"
fi
The caveat is of course that STRING is not empty, but you could add a simple test for that
 
Old 06-14-2020, 08:21 AM   #5
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
@grail. A slight variation of your code eliminating the need for an if statement:
Code:
#!/bin/sh
file=~/path/file
read -p 'word: ' string
awk -F\| -vn='not ' -vs="$string" \
  'NR==1 && NF>3 && $4 ~ "["s"]" {n=""} END{print n"found"}' "$file"

Last edited by shruggy; 06-14-2020 at 08:24 AM.
 
Old 06-14-2020, 08:45 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,663

Rep: Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777Reputation: 4777
Code:
awk -F'|' -vstr="$STRING" 'BEGIN { ec="Not found" } $4 == "["str"]" { ec="found" } END { print ec }'
awk -F'|' -vstr="$STRING" 'BEGIN { ec=1 } $4 == "["str"]" { ec=0 } END { exit ec }'
(for example)
Code:
$4 ~ "["str"]" will match if STRING=[
but I don't know the exact requirements
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 06:36 AM
[SOLVED] Search for a character in specific word in file and replace it in the word RM2015 Linux - Newbie 9 06-02-2015 07:38 AM
using 'awk' to parse From word To word malony101 Linux - Newbie 8 12-05-2012 06:20 PM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 09:17 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration