1745901 Members
4215 Online
108723 Solutions
New Discussion юеВ

Re: script help...

 
SOLVED
Go to solution
Keith Floyd
Advisor

script help...

Hi

I want to parse a text file for a particular line - then when found read the
following lines until another line found

ie

find this line PV Name LE on PV PE on PV

push these to a new file /dev/dsk/c2t4d0 2048 2048

finsh here --- Logical extents ---

Hope someone can help

Thanks

Keith
10 REPLIES 10
Dan Hetzel
Honored Contributor

Re: script help...

Hi Keith,

AGREP is the best tool for you.
The main difference with grep, is that agrep has definabla record delimiters (could be a string) while grep only has the End-Of-line.

You'll find it at the porting center:
http://hpux.cae.wisc.edu/hppd/hpux/Misc/agrep-2.04/

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: script help...

Hi again,

Here is the small abstract for agrep:
Searches text for strings or approximations thereof. Agrep is similar to the standard UNIX grep utility, but is more general, and according to its authors, usually faster. Its most notable feature is the approximate pattern matching facility. Agrep has other features not found in the grep family: It is record rather than line orientated; pattern matches can be output in a larger context than a single line. Multiple search patterns can be specified with AND/OR logic.


Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Andreas Voss
Honored Contributor
Solution

Re: script help...

Hi,

looks like you want to get the devices for a log. volume.
Here my code:
lvdisplay -v /dev/vgXX/lvolX |awk 'BEGIN{f=0}
{
if($0 ~ "PV Name")
{
f=1
continue;
}
if($0 ~ "Logical extents")
{
f=2;
exit;
}
if(f==1)
{
print $0
}
}'

Regards
Curtis Larson
Trusted Contributor

Re: script help...

cat your_file|
awk ' /PV Name/ {
getline;
while ( $0 !~ "Logical extends" ) {
print $0;
getline;
}
exit;
}'
Carlos Fernandez Riera
Honored Contributor

Re: script help...

Tommy Palo
Trusted Contributor

Re: script help...

Try:
cat FILE | awk '/PV Name LE/,/Logical extents/' > NEW_FILE
Keep it simple
Curtis Larson
Trusted Contributor

Re: script help...

and if you want something more generic put this into an executable file, such as print_from_s1_to_s2.

#!/usr/bin/ksh

string1=$1
string2=$2

awk ' {
if ( $0 ~ "'"$string1"'" ) {
getline;
while ( $0 !~ "'"$string2"'" ) {
# remove blanklines
if ( $0 !~ /^$/ ) print $0;
getline;
}
exit;
}
}'

then use it where ever you like

lvdisplay | print_from_s1_to_s2 "PV Name" "Logical extents" > yourfile
Keith Floyd
Advisor

Re: script help...

Thanks
At least one works - I will check the various methods - and assign points -

Again thanks for all your help
David Totsch
Valued Contributor

Re: script help...

Keith:

lvdisplay -v /dev/vg00/lvol1 | sed -n "/-- Distribution/,/^$/p"

is close to what you want.

This is a sed(1) "ranging". The first /RE/ instructs sed(1) to find the line with the RE (where RE is a regular expression). The second /RE/, /^$/ is looking for a blank line that follows the first RE. 'p' prints lines (that is why sed(1) was called with -n, don't print unless I tell you to).

Now, using sed(1), lets strip out what you don't want:

lvdisplay...| sed -n "/--Distribution/,/^$/{
/--Distribution/d
/PV Name/d
/^$/d
p
}"

The stuff between the curly braces is a subroutine. The statements delete what you don't want (including the trailing blank line) and explicity print what remains.

Enjoy,
-dlt-