how to find a unknown string and then the next line with the string

how to find a unknown string and then the next line with the string

Post by Claudio Gutierre » Fri, 11 Jun 1999 04:00:00



Any help will be appreciated and ,of course, excuse my English

I need to get data from a file in the next way
1) find a line with a known string.
2)from that line, reserve the 2nd column for later and get the 3rd field
wich will become new pattern to search for.
3) get the next line with such string and print the known string ,  the
reserved 2nd column and the 3rd column of this line
4) repeat from 1 with the next line where appear the known string.

Example with string = MA

aaa aaa aaaaaa
MA bbb US bbbb
ccc cccccccc
MA ddddd  CA dddddd
eeeee ee eeeeeeee
fffffff US fff
ggggg  ggggg gg
hh CA  hhhhhh

will print:

MA bbb fff
MA ddddd hhhhh

 
 
 

how to find a unknown string and then the next line with the string

Post by dharz.. » Fri, 11 Jun 1999 04:00:00



Quote:> I need to get data from a file in the next way
> 1) find a line with a known string.
> 2)from that line, reserve the 2nd column for later and get
> the 3rd field wich will become new pattern to search for.
> 3) get the next line with such string and print the known
> string, the reserved 2nd column and the 3rd column of this
> line
> 4) repeat from 1 with the next line where appear the known
> string.

I would recommend that you take a look at awk, two dimensional
arrays in ksh, and maybe sed.

Noal
--
"Sure, it's every American's right to post false or misleading
information on the Internet."
-The Detroit News

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 
 
 

how to find a unknown string and then the next line with the string

Post by John McKo » Sat, 12 Jun 1999 04:00:00



>Any help will be appreciated and ,of course, excuse my English

>I need to get data from a file in the next way
>1) find a line with a known string.
>2)from that line, reserve the 2nd column for later and get the 3rd field
>wich will become new pattern to search for.
>3) get the next line with such string and print the known string ,  the
>reserved 2nd column and the 3rd column of this line
>4) repeat from 1 with the next line where appear the known string.

>Example with string = MA

>aaa aaa aaaaaa
>MA bbb US bbbb
>ccc cccccccc
>MA ddddd  CA dddddd
>eeeee ee eeeeeeee
>fffffff US fff
>ggggg  ggggg gg
>hh CA  hhhhhh

>will print:

>MA bbb fff
>MA ddddd hhhhh

Your English is fine. I think the following perl program will do what you want.
However, I was unsure of two points. First, my program returns ALL lines
which match the second search argument (like your US or CA). Second, the
program will return lines that match (the US or CA) that occur BEFORE the
line which defines the extra search criteria (like the MA).

In particular, suppose your input file was:

aaa aaa aaaaaa
xx US xxxx added line
MA bbb US bbbb
ccc cccccccc
MA ddddd CA dddddd
eeeee ee eeeeeeee
fffffff US fff
ggggg ggggg gg
hh CA hhhhhh
yy CA yyyyyy added line

My output would be
MA bbb xxxx
MA bbb fff
MA ddddd hhhhhh
MA ddddd yyyyyy

Also, if the secondary search string is specified twice, my code will return
more lines. In the above example, insert the line as the last line:

MA zzz US zzzz

You'd get the result

MA bbb xxxx
MA bbb fff
MA ddddd hhhhh
MA ddddd yyyyy
MA zzz xxxx
MA zzz fff

I.e. I search the ENTIRE file for ALL occurrances. Not just all the remaining
records for the next match only. I hope that's OK. Anyway, the code:

#!/usr/bin/perl
#
#This perl program requires two parameters.
#The first parameter is the search string.
#The second parameter is the file to search.
if (1 != $#ARGV) {
    print "This program requires two parameters.";
    print "The first is the search string.";
    print "The second is the file to search.";
    exit 1;

Quote:}

$string=$ARGV[0];
$file=$ARGV[1];
die "Input file does not exist.\n" if ! -e $file;
die "Input file cannot be read.\n" if ! -r $file;
open(SEARCH1,$file) || die "Cannot open input file.";
while ($record = <SEARCH1>) {
    ($match, $hold, $newtest, $junk) = split(/ /,$record);
    if ($match eq $string && 0 != length($newtest)) {
        $newtest =~ tr/\n//d;
        open(SEARCH2,$file);
        while ($record2 = <SEARCH2>) {
            ($junk, $match2, $result, $junk) = split(/ /,$record2);
            $result =~ tr/\n//d;
            print ($string, " ", $hold, " ", $result, "\n") if ($newtest eq $match2);
        }
        close SEARCH2;
    }
Quote:}

close SEARCH1;
 
 
 

1. find a string in a file then delete the next n lines

Hi

I have what sounds like a fairly simple problem.

I have a large file that contains a line with a unique identifier for a record
then (always exactly) n lines of information.

So to delete a record I need to find the unique identifier and then delete
that line and the next n lines.

Any ideas on the most efficient way to do this, preferably in a ksh
script/command ?

Many thanks
Gwyn

2. remove root meta device

3. Find a string, delete that line, delete 1 line before it and all lines after it...

4. Solaris X86 gcc config

5. Find a string in a text file and replace with another string to eof

6. secure copy (scp)

7. How can you find a string within a string?

8. Free statistical software: XploRe

9. search string bbb in block b after string aaa found in block a

10. script to recursively find files containing string and replace string

11. How to search for string and assign the value on next line to a variable

12. find a string in a file and print n lines

13. Delete remaining lines below a found string?