extracting few lines from file

March 30, 2011 at 19:24:03
Specs: Windows XP
Hi,

I have 1 file which contains information as below. The question is how to extract specific information from the file and save it to newly created file. I just want to print out the output for every line from 'OPERATION XYZ' and stop until 'OPERATION EFG'. Please note that the record details (ie, Record 2: Failed, etc) for one particular operation (ie, OPERATION XYZ, etc) may contains multiple record details instead of 3 record details shown by below example. It might be 2 records, 10 records, or whatever.

myfile.csv
------------------------------------------------------------------------------

OPERATION OPQ

Record 5: Successful


OPERATION XYZ

Record 1: Succesful

Record 2: Failed

Record 2: Successful


OPERATION EFG

Record 4: Failed

Record 6: Failed

Record 1: Successful

Record 9: Failed

Record 5: Succesfull


OPERATION ABC

Record 1: Succesful

Record 2: Failed

Record 2: Successful


--------------------------------------------------------------------------------


How I can get the following output and save it to another file?

Output:

OPERATION XYZ

Record 1: Succesful

Record 2: Failed

Record 2: Successful


See More: extracting few lines from file

Report •

#1
March 30, 2011 at 23:11:12
A way using awk: if field 1 equals OPERATION and field 2 equals XYZ, drop into a while loop and get a line at a time and print it until finding a line with field 1 equaling OPERATION. getline > 0 prevents the script going into an infinite loop if no OPERATION is found. That would happen if you are looking for the last OPERATION in the file with no OPERATION keyword to terminate it, but the end of the data file:

#!/bin/ksh

awk ' {
if($1 == "OPERATION" && $2 == "XYZ")
   {
   print $0
   while(getline > 0)
      {
      if($1 == "OPERATION")
         exit

      print $0
      }
   }
} ' mydatafile.txt


Report •

#2
March 31, 2011 at 18:21:35
Hi Nails,

Thank you very much. the script work fine.


Report •

#3
April 4, 2011 at 02:04:37
Hi Nails,

What if 'XYZ' is a variable value?


Report •

Related Solutions

#4
April 4, 2011 at 02:16:48
below script does not work. I'm trying to put variable on it, but no result produced.

FILES=`cat filelist.txt`
for f in $FILES
do
awk ' {
if($1 == "OPERATION" && $2 == "$f")
{
print $0
while(getline > 0)
{
if($1 == "OPERATION")
exit

print $0
}
}
} ' mydatafile.txt > $f.LOG
done

-----------------------------------------------------------

filelist.txt contain as below:

XYZ
ABC

------------------------------------------------------------
the question is how to get result for each 'OPERATION XYZ' and 'OPERATION ABC' and save to each file?


Report •

#5
April 5, 2011 at 19:20:45
There is more than one way of passing shell variables to awk. If you are going to embed a variable in awk, try this:

"'"$f"'"

Check out this link for more information:

http://www.tek-tips.com/faqs.cfm?fi...


Report •

#6
April 5, 2011 at 20:48:44
Yes Nails,

it's work! great. tq so much


Report •

Ask Question