Ensuring a line follows another line

Ensuring a line follows another line

Post by John Glas » Thu, 08 Mar 2001 21:27:03



Here's a test file:

REF*XXXX*XXXX*XXXX
ISA*XXXX*XXXX*XXXX
REF*XXXX*XXXX*XXXX
ISA*XXXX*XXXX*XXXX
REF*XXXX*XXXX*XXXX
REF*XXXX*XXXX*XXXX

Okay, I have a huge file that would contain lines similar to what you see
above.  It would have nothing but REF lines and ISA lines.  An ISA line is
supposed to always follow a REF line.  As you can see on the last two lines,
an ISA line didn't follow it, so I need those lines redirected to an error
file.  How do I state, "Check to make sure that each REF line in this huge
file is always followed by an ISA line, but if it doesn't, kick the REF line
to an error file".

Thanks.

------------------------------------------------------------
 Get your FREE web-based e-mail and newsgroup access at:
                http://MailAndNews.com

 Create a new mailbox, or access your existing IMAP4 or
 POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------

 
 
 

Ensuring a line follows another line

Post by laura fairhe » Fri, 09 Mar 2001 02:39:38



>Here's a test file:

>REF*XXXX*XXXX*XXXX
>ISA*XXXX*XXXX*XXXX
>REF*XXXX*XXXX*XXXX
>ISA*XXXX*XXXX*XXXX
>REF*XXXX*XXXX*XXXX
>REF*XXXX*XXXX*XXXX

>Okay, I have a huge file that would contain lines similar to what you see
>above.  It would have nothing but REF lines and ISA lines.  An ISA line is
>supposed to always follow a REF line.  As you can see on the last two lines,
>an ISA line didn't follow it, so I need those lines redirected to an error
>file.  How do I state, "Check to make sure that each REF line in this huge
>file is always followed by an ISA line, but if it doesn't, kick the REF line
>to an error file".

>Thanks.

An AWK script can make light work of this. Although AWK reads a file line
at a time it can easily do analysis on 2 or more lines using a buffer.
Here one variable 'ref' keeps text from a previous REF line, if NUL then
there was no previous REF line.

I don't quite understand what you are trying to do here; do you want
the invalid lines deleted from the main file? Anyhow, this script will
create two files 'main.out' & 'errors.out'. 'main.out' contains all
the valid REF/ISA pairs in the file, 'errors.out' contains all the
lines that did not form valid REF/ISA pairs. You can easily modify it
so it just prints the errors/ line numbers of the errors, etc..

/^ *$/{next}
ref!="" && /^ISA/{
         print ref>"main.out"
         print >"main.out"
         ref=""
         next
         }
ref!="" {print ref>"errors.out";ref=""}
/^REF/ { ref=$0;next }
{print >"errors.out"}

END {if(ref!="") print ref>"errors.out"}

Just do an;

awk -f awkscriptname mainfilename

And the output files will be written to the cd

See Ya,

L

- Show quoted text -

Quote:>------------------------------------------------------------
> Get your FREE web-based e-mail and newsgroup access at:
>                http://MailAndNews.com

> Create a new mailbox, or access your existing IMAP4 or
> POP3 mailbox from anywhere with just a web browser.
>------------------------------------------------------------


 
 
 

1. 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

2. suppose my /tmp gets corrupted

3. Shell problem: How to change text in a line depending on data in the following line.

4. Apache w/ DBM vs. Apache w/o DBM ?

5. How to display the line with a string AND the following line of text?

6. Buslogic SCSI Controller question...

7. Linux permanent Line (connection two linux machines over a leased line (permanent telephone line)

8. Can't get ftpd, etc.. Correction....

9. line by line reading of the lines in list

10. one liner which prints n lines before and m lines after the line found by grep

11. line by line without using echo "$i" | while read line?

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

13. lilo running is giving the following lines as error..