awk search if else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search if else
# 1  
Old 04-20-2011
Question awk search if else

Hi,
I want to search for a particular string using awk and print "Found" or "Not Found" depending on the search.

On searching this forum i got this code but it is not working:
Quote:
$ awk '{print ~/Reference_Data/?"Found":"Not Found"}' file
awk: syntax error near line 1
awk: illegal statement near line 1
# 2  
Old 04-20-2011
Something like this?
Code:
awk '/Reference_Data/{f=1;exit}END{print f?"Found":"Not Found"}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-20-2011
Can you try as..
Code:
 
awk '{print $0~/Reference_Data/?"Found":"Not Found"}' file
or
awk '{print /Reference_Data/?"Found":"Not Found"}' file # not sure if this would work

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 04-20-2011
Code:
declare -i toggle=0
while read -r line
do
  case "$line" in
     *Reference_Data*) toggle=1;;
  esac
done < file
if [ $toggle -eq 0 ] ;then
   echo "Not found"
else
   echo "Found"
fi

This User Gave Thanks to bash-o-logist For This Post:
# 5  
Old 04-20-2011
thanks for replies, but i am still getting same error.

Quote:
$ awk '/Reference_Data/{f=1;exit}END{print f?"Found":"Not Found"}' tmpp
awk: syntax error near line 1
awk: illegal statement near line 1
$ awk '{print $0~/Reference_Data/?"Found":"Not Found"}' tmpp
awk: syntax error near line 1
awk: illegal statement near line 1
$ awk '{print /Reference_Data/?"Found":"Not Found"}' tmpp
awk: syntax error near line 1
awk: illegal statement near line 1

$ uname -a
SunOS svdw2d02 5.10 Generic_142900-01 sun4u sparc SUNW,Netra-T12
# 6  
Old 04-20-2011
Quote:
Originally Posted by rishav
thanks for replies, but i am still getting same error.
Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 04-20-2011
Quote:
Originally Posted by Franklin52
Use nawk or /usr/xpg4/bin/awk on Solaris.
Thanks.. now working fine..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

How to search for right word using awk?

Hi- I have issue with the code to get the input file reformat --Goal is reformat and print out only column name one per line, leading space or trailing space will be removed... --TABLE1.DDL as input file, and content of file as show below REPLACE VIEW TABLE1 ( ID ,COL1 ... (3 Replies)
Discussion started by: lv99
3 Replies

4. Shell Programming and Scripting

Awk: search and replace

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. going forward, search for another keyword. 3. Replace this line with user supplied input. for e.g., my file has the following... (6 Replies)
Discussion started by: chingupt
6 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. UNIX for Dummies Questions & Answers

Awk search help

Hi, Want to know what does it returns. $ awk '$1 == /Jan/' inv $ awk '$1 = /Jan/' inv --- What does this one signify. 1 13 25 15 115 1 21 36 64 620 $ awk '$1 ~ /Jan/' inv Jan 13 25 15 115 Jan 21 36 64 620 Thanks in advance. (3 Replies)
Discussion started by: vanand420
3 Replies

7. Shell Programming and Scripting

How to do a simple awk search?

Hello I am trying to do global search on access log files for a date and for either 'Error|error' string ls -lrt *access* | grep "Sep 23" | awk '{print $9}'|xargs awk '/23\/Sep\/2011/ && /Error/ || /error' Above matches All lines with 'error' as well unfortunately. Is there a... (6 Replies)
Discussion started by: delphys
6 Replies

8. Shell Programming and Scripting

awk search

i guys, i have a bash script , and it works, but i need an awk file, and i can't convert this code like: #!/bin/awk -f ..... my script #!/bin/bash awk '/\<FIRST\>|\<SECOND\>|\<THIRD\>|\<ZERO\>/' DOC.txt thanks :) (4 Replies)
Discussion started by: felito
4 Replies

9. Shell Programming and Scripting

awk search pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (14 Replies)
Discussion started by: a27wang
14 Replies

10. Shell Programming and Scripting

AWK - no search results

Hi all, I'm new to awk and I'm experiencing syntax error that I don't know how to resolve. Hopefully some experts in this forum can help me out. I created an awk file that look like this: $ cat myawk.awk BEGIN { VAR1=PATTERN1 VAR2=PATTERN2 } /VAR1/ { flag=1 } /VAR2/ { flag=0 } {... (7 Replies)
Discussion started by: hk18
7 Replies
Login or Register to Ask a Question