How can awk can read the following lines of a line searched? 
Author Message
 How can awk can read the following lines of a line searched?

Hi all,

I have some files that I want to replace the specific line after  the
condition search matches.

For example:

awk '/A B/ { if  find a line begins with character ) , whatever after that
and terminate with the character ; then replace this line with );
together }'

A B                            ------>  A B
( abc defg                   ------>  ( abc defg
df8343 efd                 ------>  df8343 efd
kdf g  gfgfg                 ------>  kdf g  gfgfg
) sun det zidan;           ------>  );

or how can I print out the 2nd line "df8343 efd" (after 'A B' line)  if
found "A B" strings

I'm  successful to replace it inside the 'vi' but I'm trying to build a awk
script to do it to make my  life easier:-)

Thanks in advance for any help.

H.V



Wed, 24 Jul 2002 03:00:00 GMT  
 How can awk can read the following lines of a line searched?

Quote:

> I have some files that I want to replace the specific line after the
> condition search matches.

> For example:

> awk '/A B/ { if find a line begins with character ) , whatever after that
> and terminate with the character ; then replace this line with );
> together }'

> A B              ------>  A B
> ( abc defg       ------>  ( abc defg
> df8343 efd       ------>  df8343 efd
> kdf g  gfgfg     ------>  kdf g  gfgfg
> ) sun det zidan; ------>  );

> or how can I print out the 2nd line "df8343 efd" (after 'A B' line) if
> found "A B" strings

This is a simple text substitution problem in Perl:

$ cat hv.txt
A B              ------>  A B
( abc defg       ------>  ( abc defg
df8343 efd       ------>  df8343 efd
kdf g  gfgfg     ------>  kdf g  gfgfg
) sun det zidan; ------>  );

A B
( abc defg
df8343 efd
kdf g  gfgfg
) sun det zidan;
$ perl -00wpe 's/(A B.*\n\)).*;/$1;/s' hv.txt
A B              ------>  A B
( abc defg       ------>  ( abc defg
df8343 efd       ------>  df8343 efd
kdf g  gfgfg     ------>  kdf g  gfgfg
);

A B
( abc defg
df8343 efd
kdf g  gfgfg
);
$ perl -00wpe 's/(A B.*\n\)).*?;/$1;/s' hv.txt
A B              ------>  A B
( abc defg       ------>  ( abc defg
df8343 efd       ------>  df8343 efd
kdf g  gfgfg     ------>  kdf g  gfgfg
); ------>  );

A B
( abc defg
df8343 efd
kdf g  gfgfg
);
$ perl -0777wpe 's/(A B.*?\n\)).*?;/$1;/gs' hv.txt
A B              ------>  A B
( abc defg       ------>  ( abc defg
df8343 efd       ------>  df8343 efd
kdf g  gfgfg     ------>  kdf g  gfgfg
); ------>  );

A B
( abc defg
df8343 efd
kdf g  gfgfg
);
$

Notice the difference between using the maximal matching .* and
the minimal matching .*? after the having matched a right parenthesis
at the beginning of the line (i.e., immediately following a newline).
Also notice some subtle differences between using paragraph mode
(-00) and slurp mode (-0777), and between using a single substitution
and a global substitution. Naturally, the best solution depends upon
your actual input text.

The simplest awk solution might look something like this:

$ awk '/A B/ { f=1 } f && /^\).*;/ { print ");"; f=0; next } { print }' hv.txt
A B              ------>  A B
( abc defg       ------>  ( abc defg
df8343 efd       ------>  df8343 efd
kdf g  gfgfg     ------>  kdf g  gfgfg
);

A B
( abc defg
df8343 efd
kdf g  gfgfg
);
$

Blech!

--
Jim Monty

Tempe, Arizona USA



Thu, 25 Jul 2002 03:00:00 GMT  
 How can awk can read the following lines of a line searched?

writes:

Quote:

...
>> For example:

>> awk '/A B/ { if find a line begins with character ) , whatever after that
>> and terminate with the character ; then replace this line with );
>> together }'

>> A B              ------>  A B
>> ( abc defg       ------>  ( abc defg
>> df8343 efd       ------>  df8343 efd
>> kdf g  gfgfg     ------>  kdf g  gfgfg
>> ) sun det zidan; ------>  );
...
>This is a simple text substitution problem in Perl:

This is pedantic and off-topic, though this response is also pedantic.

...

Quote:
>The simplest awk solution might look something like this:

<lightly edited to minimize line wrapping>

Quote:
>awk '/A B/{f=1} f && /^\).*;/ {print ");"; f=0; next} {print}' hv.txt

...

You've been doing so much perl programming your awk skills are atrophying.

awk '/A B/ {f=1} f && /^\).*;$/ {$0 = ");"; f=0} {print}' hv.txt

- you don't need the next
- you missed the part about the /^)/ line _terminating_ with ; though that begs
the question whether anything other than newline following the ; but before the
next \n should be retained.
- out of curiousity, since your perl script's reading paragraphs at a time,
would it mangle possibly pathological cases like

--begin case 1--
A B
( abc
def (
ghi jkl
) mno
pqr; stu
vwx
);
--end case 1--

and

--begin case 2--
A B
( abc

def
) ghi;
--end case 2--

so maybe -0777 and a global s/// would work better.

gawk comes close to the perl solution, but not quite as tersely.

gawk '{ print gensub("(A B.*\n\\))[^\n]*;$", "\\1;\n", "G", $0) }' RS="\000"
hv.txt

As for the original poster's 2nd question

Quote:
>> or how can I print out the 2nd line "df8343 efd" (after 'A B' line) if
>> found "A B" strings

the most direct (if inelegant) way would be

awk '/A B/ { getline; getline; print }' hv.txt

more generally, make the line offset a variable passed as a parameter

awk '/A B/ { for (i = 0; i < n; ++i) getline; print }' n=2 hv.txt



Thu, 25 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. awk -- pattern match a line and the line that follows

2. CA Cans VO ?

3. It's not bad canned meat...

4. It's not bad canned meat...

5. It's not bad canned meat...

6. Using CGI module with 'canned queries'

7. It's not bad canned meat...

8. Search file and print line + next line?

9. reading a line and executing a command on that line

10. how to read text files line by line?

11. reading file line by line

12. new question in how can i read data from file line by line

 

 
Powered by phpBB® Forum Software