Complex string search query.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex string search query.
# 8  
Old 05-03-2013
As vidyadhar85 says, please post more of your file. It will help us to understand how to search for it.
# 9  
Old 05-03-2013
You could try:
Code:
#!/bin/ksh
if [ $# -ne 2 ]
then    echo "Usage: ${0##*/} z1:Number file" >&2
        exit 1
fi
awk -v REQ="$1" '
BEGIN { res = "Request "REQ" not found" }
$0 ~ ".*<z1:Number>" REQ "</z1:Number>.*" {
        keep = 1
} 
/<y1:Status>/ && keep {
        res = "Request "REQ" has " substr($0, index($0, "<y1:Status>") + 11, 6)
        keep = 0
}
END {   print res }' "$2"

I tested this using ksh, but it will also work with bash (or any other POSIX conforming shell). If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 05-05-2013
Hi.

To address the question of tac not found, here are a number of solutions for reversing the file (narrow, solution of sub-problem):
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate reversal of file by lines.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C tac tail perl sed ./ppt-tail

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, tac:"
tac $FILE

pl " Results, BSD / Solaris tail -r:"
v1=$( uname -a )
if  echo "$v1" | grep -q -i "bsd"
then
  tail -r $FILE
elif echo "$v1" | grep -q -i "sunos"
then
  /usr/xpg4/bin/tail -r $FILE
else
  pe " Not in a BSD or Solaris OS, cannot use BSD grep."
fi

pl " Results, perl one-liner:"
perl -e 'print reverse <>' $FILE

pl " Results, ppt perl, bsd tail work-alike:"
pe "http://cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/tail/index.html"
./ppt-tail -r $FILE

pl " Results, sed 1:"
sed '1!G;h;$!d' $FILE

pl " Results, sed 2:"
sed -n '1!G;h;$p' $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86
bash GNU bash 3.00.16
tail - ( /usr/xpg4/bin/tail, Jun 8 2006 )
perl 5.8.4
sed - ( /usr/xpg4/bin/sed, Aug 9 2005 )
./ppt-tail - ( local: ./ppt-tail, May 3 06:10 )

-----
 Input data file data1:
foo
bar
baz

-----
 Results, tac:
./s1: line 19: tac: command not found

-----
 Results, BSD / Solaris tail -r:
baz
bar
foo

-----
 Results, perl one-liner:
baz
bar
foo

-----
 Results, ppt perl, bsd tail work-alike:
http://cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/tail/index.html
baz
bar
foo

-----
 Results, sed 1:
baz
bar
foo

-----
 Results, sed 2:
baz
bar
foo

Best wishes ... cheers, drl
# 11  
Old 05-10-2013
Quote:
Originally Posted by Don Cragun
You could try:
Code:
#!/bin/ksh
if [ $# -ne 2 ]
then    echo "Usage: ${0##*/} z1:Number file" >&2
        exit 1
fi
awk -v REQ="$1" '
BEGIN { res = "Request "REQ" not found" }
$0 ~ ".*<z1:Number>" REQ "</z1:Number>.*" {
        keep = 1
} 
/<y1:Status>/ && keep {
        res = "Request "REQ" has " substr($0, index($0, "<y1:Status>") + 11, 6)
        keep = 0
}
END {   print res }' "$2"

I tested this using ksh, but it will also work with bash (or any other POSIX conforming shell). If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

I tried your code but it does not yeild the desired output.


Code:
 
bash-3.2$ /tmp/Extract.sh 5458 my.log.10
awk: syntax error near line 1
awk: bailing out near line 1
bash-3.2$ uname -a
SunOS myhost 5.10 Generic_147440-22 sun4v sparc SUNW,SPARC-Enterprise-T5220

# 12  
Old 05-10-2013
Like Don Cragun suggested, on Solaris use /usr/xpg4/bin/awk rather than awk
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search the specific content from the complex file

Hi, I have a file with complex data without delimiter, have requirement to fetch the specific record based on some charcters. here is my file data ... (12 Replies)
Discussion started by: Riverstone
12 Replies

2. Shell Programming and Scripting

Complex Query regarding folder size

Hi, I provide the path to a folder. I would like so drill in each folder inside the folder i gave and show me the size in human readable form du -sh <folder-name> of the top 8 folders with maximum sizes. For example: Path of the folder: "/opt/app/myapp" has these directories. ls... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

awk command for complex search and print

Input: 9999~cbc~100~209~ozzxczcz~10001001001~100~abc10 123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456 125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345 127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123 128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345... (7 Replies)
Discussion started by: onesuri
7 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 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. Shell Programming and Scripting

Complex query

A file whose location i am not aware of contains the below text <url>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(FAILOVER=on)(ADDRESS=(PROTOCOL=TCP) (HOST=ngm2sn1p2-vip.mybank.net)(PORT=4001))(ADDRESS=(PROTOCOL=TCP)(HOST=ngm2sn2p2-vip.mybank.net)... (1 Reply)
Discussion started by: mohtashims
1 Replies

7. Web Development

Complex MySQL Query(s)

Hi all, I have a bit of an issue, I am working on a bit of a CMDB for a friend, it's to do with real estate. Anyway, this is my situation. I have a table which contains all the properties (forsale, sold, etc) in the DB named "property". Now, this is what I want to achieve, I wish to... (5 Replies)
Discussion started by: STOIE
5 Replies

8. Shell Programming and Scripting

Complex search and output

Hi, I have somewhat of a complex task at hand. I am on a HP-UX box and I need to search for information embedded in files, further embedded into a file/folder structure. I compiled a list of information that I need found: File=list.csv DATE FIELD 1 FIELD2... (2 Replies)
Discussion started by: doza22
2 Replies

9. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

10. Programming

Need help with complex SQL query (Sybase)

Hello, I have three tables. I need an SQL query (preferably Sybase) that will return all of the stringID values of table B where the following conditions exist: (1) B.intID = A.intID (2) B.intID != C.intID or (B.intID = C.intID and (C.v1 = 0 or C.v2... (2 Replies)
Discussion started by: chatieremerrill
2 Replies
Login or Register to Ask a Question