sed search alternately for patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed search alternately for patterns
# 1  
Old 08-26-2010
sed search alternately for patterns

hi all,

I am trying to do search on a gzip file. The file has

<pattern1>

Data..

<pattern2>

data

<pattern1>

data
<patter2>

I want to print each pattern 1 and the corrresponding pattern2. If pattern 2 fails to appear and pattern 1 appears, I do not want to print pattern1 and continue for searching for patttern2 for the next pattern1. It looks simple..but have been pullingmy hair over this for a day now. Is there a sipmel sed/awk way of achieving this ..Thanks
# 2  
Old 08-26-2010
Posting your input sample and expected output would help us to help.
# 3  
Old 08-26-2010
How about something like this?
Code:
pat1=
while read line; do
    case "$line" in
        pattern1)
            pat1="$line"
            ;;
        pattern2)
            if [ -n "$pat1" ]; then
                echo -e "Pattern 1: \"$pat1\"\nPattern 2: \"$line\"\n"
            fi
            pat1=
            ;;
    esac
done < file

Of course, this is using file globs instead of more powerful regular expressions.
But if you need the extra power, you could adapt it to use grep instead of case.
# 4  
Old 08-27-2010
From your description I've interpreted it this way... Given the following data with foo and bar being pattern1 and pattern2:

Code:
foo
data 1
data 2
bar
data 10
data 20
foo
data 3
data 4
foo
data 5  should not print
data 6  should not print
bar
data 30
data 40

The assumption I made was that you wanted the data records between the patterns to be printed, ignoring the data from consecutive (2+) sections with the same pattern. Thus the data5/6 lines should not print.

Code:
gzip -dc your-file-name | awk '
        BEGIN {
                seen1 = 0;         # state flags -- what section we last captured
                seen2 = 1;
                snarf = 0;          # don't assume pattern1 is first
        }

        /foo/   {                       # assume that pattern1/2 isnt in the data
                if( seen2 )             # weve seen pattern 2; ok to snarf data from this section
                        snarf = 1;      # ok to print data
                        # add print if you want pattern line printed
                else
                        snarf = 0;      # consecutive occurance of pattern 1, snarfing not allowed
                seen2 = 0;            # invert state to prevent printing consec foo section 
                seen1 = 1;
                next;                  
        }

        /bar/   {
                if( seen1 )           # not a consecutive pattern2 section 
                        snarf = 1;    # ok to snarf
                        # add print if you want pattern line printed
                else
                        snarf = 0;
                seen1 = 0;           # invert state to prevent printing consecutive bar section 
                seen2 = 1
                next;
        }

        snarf > 0 { print; }          # if ok to snarf, print the data record
'

Output generated from the sample input above:
Code:
data 1 
data 2
data 10
data 20
data 3
data 4 
data 30
data 40

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for patterns on different lines

im using the following code to search a log for entries on two different lines: awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt firstpattern="start error log" secondpattern="i am logging the... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Search and count patterns

Hi, I have a text file the contents are like this now i want to search patterns Z , Z etc and count the occurrence of such patterns, after Z value can be any random digits, please help me it is urgent... output like this Z .............>5 Z ............>8 (9 Replies)
Discussion started by: sreejithalokkan
9 Replies

3. Shell Programming and Scripting

String search between patterns using sed

Hi, I am trying to find a way to get sed/awk/grep to help me find a string in a log file that exists between two datestamps and then print the preceding datestamp up to the next datestamp. Here is an example of my logfile: +++ 2013/03/28 17:01:37.085 SIGNALING HIGH ACTIVE Failure Response... (5 Replies)
Discussion started by: raytx
5 Replies

4. Shell Programming and Scripting

Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed. My Existing Text file is Users.txtpaul, Paul Smith, Stevn Smiley, REQ000001, ARS (11) sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11) mike, Mike Conway, Stevn... (8 Replies)
Discussion started by: evrurs
8 Replies

5. Shell Programming and Scripting

Search by patterns case

42 network read failed sv1 sv23 sv4 sv11 sv23 sv5 sv 7 48 client hostname could not be found sv21 sv78 sv19 sv22 sv111 sv203 sv5 sv 33 49 client did not start sv1 sv21 54 timed out connecting to client sv2 sv4 sv12 above is my file , I'd like to use a script to list all name... (5 Replies)
Discussion started by: Sara_84
5 Replies

6. UNIX for Dummies Questions & Answers

Search and extract matching patterns

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

7. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

8. Shell Programming and Scripting

search multiple patterns

I have two lists in a file that look like a b b a e f c d f e d c I would like a final list a b c d e f I've tried multiple grep and awk but can't get it to work (8 Replies)
Discussion started by: godzilla07
8 Replies

9. Shell Programming and Scripting

search patterns

hello, i have an input file of about 50,00,000 lines. few of its lines are as follows: <CR:0023498789,TPO-14987084;BO=IC&SUB=ALLP <CF:0023498789,CB=YES;BIL&NC=NO <CF:0023498789,CW=NO;NS=NO <GC:0023498789,CG=YES;TPO&NC=YES <CR:0024659841,TPO-14484621;BO=NO&BA=OC&SUB=ALLH... (1 Reply)
Discussion started by: rochitsharma
1 Replies

10. Programming

alternately writings

Hi, can anyone show me how to solve the following exercise in ANSI C? Create 2 process (parent & child). The parent have to create a file called file<pid>, where <pid> is the process ID of the child process. For n times (where n is a constant previously defined) the child process and... (1 Reply)
Discussion started by: FastMagister
1 Replies
Login or Register to Ask a Question