GREP function in ksh which ignores LINE Breaks


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers GREP function in ksh which ignores LINE Breaks
# 15  
Old 09-22-2014
Quote:
Originally Posted by Corona688
You are leaving out the single quotes, which totally changes the meaning of anything you do.

Put the single quotes back in and try again.
I did that as well but same result.
Code:
#!/bin/ksh

awk -v P1="ST.820" -v P2="RITE AID" '
# set A if P1 found, set B if P2 found
0~P1{A=1} $0~P2{B=1} 
# If filename changes, and A set, and B set, print filename.  Reset A and B.
(L != ARGIND) { L++; if(A && B) print ARGV[L];  A=B=0 }
# Check A and B for the last filename and print.
END { if(A&&B) print ARGV[L] }' /edi/editst/archive/sterling/in/*


Last edited by Corona688; 09-22-2014 at 02:14 PM..
# 16  
Old 09-22-2014
Hi.

Given the characterisitcs of this problem, a 2-step grep can be used, but the grep needs to be able collect 1 line before and after matched lines. For example, with GNU grep:
Code:
#!/usr/bin/env bash

# @(#) s3	Demonstrate match across lines, grep.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
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 grep

FILE=${1-data1}

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

pl " Results, grep for RITE AID back to ST*82 across lines:"
if grep -F -B1 'RITE AID' $FILE |
grep -q -F -A1 'ST*82'
then
  printf "$FILE\n"
fi

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

pl " Results, grep for RITE AID back to ST*82 across lines:"
if grep -F -B1 'RITE AID' $FILE |
grep -q -F -A1 'ST*82'
then
  printf "$FILE\n"
fi

exit 0

producing:
Code:
$ ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
grep GNU grep 2.5.3

-----
 Input data file data1:
ISA*00* 00 *ZZ*NATIONSBANK *14*0030020520500 *140918*1200*U*00401*000006436*0*P*^ 
GS*RA*014578892*IGIHCJEEJ*20140918*1200*6442*X*004010
ST*820*000006482 
N1*PR*RITE AID HDQTRS. CORP.*1*014578892

-----
 Results, grep for RITE AID back to ST*82 across lines:
data1

-----
 Input data file data3:
ISA*00* 00 *ZZ*NATIONSBANK *14*0030020520500 *140918*1200*U*00401*000006436*0*P*^ 
GS*RA*014578892*IGIHCJEEJ*20140918*1200*6442*X*004010
ST*820*000006482 
not a matching line
N1*PR*RITE AID HDQTRS. CORP.*1*014578892

-----
 Results, grep for RITE AID back to ST*82 across lines:

The way this works is that for each line that matches the second pattern, we capture it and the preceding line. Those pairs of lines are piped into a second grep, which lists the pairs only if the first pattern occurs.

Note that the second data file, data2, has an intervening line between the two patterns of interest, which should disqualify that file from being listed and it does. I'm sure it's possible to construct pathological cases where this fails, but then the sample provided would not have been representative.

I have not tested this extensively, but it seems to make sense for the problem at hand.

Best wishes ... cheers, drl
# 17  
Old 09-22-2014
Quote:
Originally Posted by Raghav Garg
I did that as well but same result.
Considering the result last time should have been various syntax errors, I have my doubts.

Try nawk if you're on solaris.
This User Gave Thanks to Corona688 For This Post:
# 18  
Old 09-22-2014
Quote:
Originally Posted by Corona688
Considering the result last time should have been various syntax errors, I have my doubts.

Try nawk if you're on solaris.
This works without any error but with no output, I need to have the file name from this process so that I can use them in the next set of process.

Code:
#!/bin/ksh


nawk -v P1="ST.820" -v P2="RITE AID" '
# set A if P1 found, set B if P2 found
0~P1{A=1} $0~P2{B=1} 
# If filename changes, and A set, and B set, print filename.  Reset A and B.
(L != ARGIND) { L++; if(A && B) print ARGV[L];  A=B=0 }
# Check A and B for the last filename and print.
END { if(A&&B) print ARGV[L] }' /edi/editst/archive/sterling/in/*


Last edited by Corona688; 09-22-2014 at 03:04 PM..
# 19  
Old 09-22-2014
You also left a $ out of the program which totally changed its meaning.

It works fine when I use the entire unmodified program:

Code:
$ awk -v P1="ST.820" -v P2="RITE AID" '
# set A if P1 found, set B if P2 found
$0~P1{A=1} $0~P2{B=1}
# If filename changes, and A set, and B set, print filename.  Reset A and B.
(L != ARGIND) { L++; if(A && B) print ARGV[L];  A=B=0 }
# Check A and B for the last filename and print.
END { if(A&&B) print ARGV[L] }' data1

data1

$

Also, please use code tags for code, not icode. Image

Last edited by Corona688; 09-22-2014 at 03:14 PM..
# 20  
Old 09-22-2014
Quote:
Originally Posted by Corona688
You also left a $ out of the program which totally changed its meaning.

It works fine when I use the entire unmodified program:

Code:
$ awk -v P1="ST.820" -v P2="RITE AID" '
# set A if P1 found, set B if P2 found
$0~P1{A=1} $0~P2{B=1}
# If filename changes, and A set, and B set, print filename.  Reset A and B.
(L != ARGIND) { L++; if(A && B) print ARGV[L];  A=B=0 }
# Check A and B for the last filename and print.
END { if(A&&B) print ARGV[L] }' data1

data1

$

Also, please use code tags for code, not icode. Image
I am running what you just suggested and also its nawk version but getting an error :- -v: not found

I know that I am making this tough for all the people helping me around.
Much Thanks to everyone who has contributed to this thread.
# 21  
Old 09-22-2014
Quote:
Originally Posted by Raghav Garg
I am running what you just suggested and also its nawk version but getting an error :- -v: not found
It wasn't doing that before, and adding $ to it wouldn't cause it. Do it carefully, leaving out single characters and moving things around can break it as the last couple attempts illustrated.

If it doesn't work, show exactly what you are doing, word for word, letter for letter, keystroke for keystroke. Use a screenshot if you have to, but show us what you did.

And don't bother using awk, just nawk.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

2. HP-UX

After using @, line breaks for a particular user in shell

Dear Concern, When we using @ sign, line breaks for a particular user in shell. Please advise how to resolve from the problem in HP UX. tabs@tabsdb02:/ccbs/users/tabs$ cat /etc/passwd|grep tabs tabs:RdCgOsmKee7Ps:221:201::/ccbs/users/tabs:/usr/bin/ksh... (3 Replies)
Discussion started by: makauser
3 Replies

3. How to Post in the The UNIX and Linux Forums

GREP function in ksh which ignores LINE Breaks

I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command - grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from this,... (3 Replies)
Discussion started by: Raghav Garg
3 Replies

4. UNIX for Dummies Questions & Answers

Page breaks and line breaks

Hi All, Need an urgent solution to an issue . We have created a ksh file or shell script which generates 1 DAT file. the DAT file contains extract of a select statement . Now the issue is , when we are executing the ksh file , the output is coimng with page breaks and line breaks . We have... (4 Replies)
Discussion started by: Ayaskant
4 Replies

5. Shell Programming and Scripting

ksh while read loop breaks after one record - AIX

#!/bin/ksh for SRV in imawasp01 \ imawasp02 \ imawasp03 \ imawasp04 \ imawasp05 \ imawasp06 \ imawasp07 \ imawasp08 \ imawasp09 do print "${SRV}" while read PASSLINE do SRVNAME=`echo ${PASSLINE} | awk -F\: '{print $1}'` LASTLOGIN=`ssh ${SRV} lsuser ${SRVNAME} | tr '... (2 Replies)
Discussion started by: port43
2 Replies

6. Programming

Clean and keep line breaks

Hello, I want to keep line spaces in comments but clean more then 2 after each. Example: $sentence="This is my first sentence This will be in a new row This will be too in a new row but not separated with 3line breaks just with one "; And i want to... (1 Reply)
Discussion started by: AimyThomas
1 Replies

7. Shell Programming and Scripting

While read line ignores the '\' in file content

I need to read temp.$i file content line by line through while loop but somehow the '\' do not appear in output.. Can someone guide how to read this exact content line by line in unix : if then cat temp.$i | head -1 # the file content appears fine while... (13 Replies)
Discussion started by: Prev
13 Replies

8. Shell Programming and Scripting

Help with wc and line breaks

Hi everyone, I have gone through the forum trying to find an answer to this question but was unsuccessful. I am hoping that someone can help me with this please. I am trying to get my script to recognise line breaks from a file and to give me a result for wc of each line. So basically, if you... (7 Replies)
Discussion started by: stargazerr
7 Replies

9. Shell Programming and Scripting

any better way to remove line breaks

Hi, I got some log files which print the whole xml message in separate lines: e.g. 2008-10-01 14:21:44,561 INFO do something 2008-10-01 14:21:44,561 INFO print xml : <?xml version="1.0" encoding="UTF-8"?> <a> <b>my data</b> </a> 2008-10-01 14:21:44,563 INFO do something again I want... (3 Replies)
Discussion started by: csmklee
3 Replies

10. Shell Programming and Scripting

Grep a number from a line in ksh

In file.name, I have a line that reads $IDIR/imgen -usemonths -dropcheck -monitor -sizelimit 80000000 -interval 120 -volcal HSI How can I get the size limit, i.e. 80000000 out and pass it to a variable called SIZE? Thanks. I tried echo "grep sizelimit file.name" | sed -n -e... (3 Replies)
Discussion started by: rodluo
3 Replies
Login or Register to Ask a Question