1834178 Members
2386 Online
110064 Solutions
New Discussion

script help

 
Thi Vu
Frequent Advisor

script help

Hello everyone,

I have a report that have 10 different kinds of data (i.e: data: CMTP
info about the data ....
....
....

data: MDCA
info about the data....
....
....)
and each data has various info that some can be 1 page or up to 3 pages long. So instead of have over 100 pages of report to see only 2 of the data info that is require - I'm writing a script to parse the info for the 2 types of data that are needed and send it to another file so that the user can print it if he/she wants. I had tried grep (but it only show the line where it matches not the info after that) I'd read that sed can do the trick. Can someone help me with this. Thank you.

Thi
8 REPLIES 8
Justo Exposito
Esteemed Contributor

Re: script help

Hi,

You can do something like this:
num=`grep -n "CMDAS" coco1| cut -d":" -f1`
echo $num
echo "$num,\$ w temp" |ed coco1
num2=`grep -n "DATA" temp| head -2 | tail -1 | cut -d":" -f1`
echo $num2
(( num2 = num2 -1 ))
echo "1,$num2 w temp" |ed temp

Regards,

Justo.
Help is a Beatiful word
Rodney Hills
Honored Contributor

Re: script help

It depends a lot on what defines the start of the info and the end. If it is like you show, then the following will fetch that block of info.

sed -n -e '/^data: /,/.*)$/p' newfile

-- Rod Hills
There be dragons...
David Lodge
Trusted Contributor

Re: script help

you can also use awk to rip between two patterns:

awk '/data: DATA/,/end/ { print $0 }' file.dat
harry d brown jr
Honored Contributor

Re: script help

#!/usr/bin/perl -d
#
$ReportFile = "/tmp/4";
#
open(REPORTLINES, "<".$ReportFile) || die "can not open $ReportFile";
#
LINE: while(){
$lineofdata = $_;
if (substr($lineofdata,0,5) eq "Data:") {
$reportname = substr($lineofdata,6,length($lineofdata)-6);
$ReportFileName=$ReportFile . $reportname;
close(NEWREPORT);
open(NEWREPORT, ">".$ReportFileName) || die "can not open $ReportFile$repo
rtname";
next LINE;
}
#
printf NEWREPORT $lineofdata;
}
close(REPORTFIXED);
Live Free or Die
Thi Vu
Frequent Advisor

Re: script help

Thank you for all your help. I can only use one suggestion and need more help, this is what my script look like:

list="CTMT GRPP MDDA"

for file in $list
do
awk '/DATA: $file/,/Report/ {print $0}' >>
done

This will allow me to go through loop and print 3 types of data and append it to a newfile. However, when I ran this sript the variable $file did not get pass to the awk command line at all. The error report from awk is:

awk: There is a regular expression error.
Invalid pattern.
The input line number is 1.
The source line number is 1.

Is there a way to pass a variable to the awk command in a script ? Again thank you for your help.

Thi
Rodney Hills
Honored Contributor

Re: script help

Passing args to an awk script requires using the -F option, Change your-
awk '/DATA: $file/,/Report/ {print $0}' >>

to

awk -Ffile="DATA: $file" 'file,/Report/ {print $0}' ...

The -F option sets awk variable "file" to "DATA: $file" and then you can use that awk variable in your awk script.

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: script help

Sorry, that is the -V option (not -F).

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: script help

Hi:

Try this:

# file=/tmp/myfile
# echo awk '$0~DATA: file,/Report/ {print $0}' file=$file >>

Regards!

...JRF...