1753687 Members
5366 Online
108799 Solutions
New Discussion юеВ

awk for lvcreate

 
SOLVED
Go to solution
Rasheed Tamton
Honored Contributor

awk for lvcreate

I have a file like below:
I want to create a text file for creating LVs on the system using this file. It has two lines. The Record Separator starting with vg (e.g., vgidx). So each record is two lines.

What I am looking for is that the output txt file should produce like this; using the first two lines of the file:

lvcreate -L 4080 -n idx01_4g vgidx

--------------
vgidx
idx01_4g 4080
vgidx
idx02_4g 4080
vgidx
idx03_4g 4080
vgidx
idx04_4g 4080
vgidx
idx05_4g 4080
vgidx
idx06_4g 4080
vgidx
idx07_4g 4080
---
Thanks
Rasheed.
11 REPLIES 11
Rodney Hills
Honored Contributor

Re: awk for lvcreate

If perl is ok then-

perl -ne 'chomp; /^vg/ && do { $vg=$0l } ; /^id/ && do { ($id,$sz)=split(" ",$_); print "lvcreate -L $sz -n $id $vg\n"; }' inputfile

HTH

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

Re: awk for lvcreate

Or use awk:

cat input-file | awk '/^vg/ {VG=$1} /^id/ {print "lvcreate -L 4080 -n " $1" "$2" "VG}' >output-file

HTH,
Peter
Hoefnix
Honored Contributor

Re: awk for lvcreate

Sorry mist an item:
this will be the way:
cat input-file | awk '/^vg/ {VG=$1} /^id/ {print "lvcreate -L "$2" -n " $1" "VG}' >output-file

Sorry for the mistake,
Peter
Kent Ostby
Honored Contributor

Re: awk for lvcreate

Hmmm.. I think there's a minor error in Pete's script.

I think it should be:


cat input-file | awk '/^vg/ {VG=$1} /^id/ {print "lvcreate -L " $2 " -n " $1" "
VG}' >output-file
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Hoefnix
Honored Contributor

Re: awk for lvcreate

Kent,

You are right, I saw it myself and posted the correction just before you ;-)

Regards,
Peter
Rasheed Tamton
Honored Contributor

Re: awk for lvcreate

I think I was not too clear. There are around 22 VGs other than vgidx and I do not get the expected answer. Please be more clear.

Thanks
Rasheed Tamton
Honored Contributor

Re: awk for lvcreate

Sorry Rodney, I got some compilation errors with the Perl stuff. I could not test it on my system. Also I am still not comforatble with Perl.
Jim Mallett
Honored Contributor
Solution

Re: awk for lvcreate

Rasheed,

The awk examples above work with your given sample data. If you have VGs and LVs that don't use the above naming convention you can try this:

awk '{vg=$0; getline; print "lvcreate -L " $2 " -n " $1 " " vg}' test.txt

I created a sample file with the following data:
1vgidx1
idx01_4g 4080
2vgidx2
idx02_4g 6120
3vgidx3
idx03_4g 8160

And the output of the command above was:
$ awk '{vg=$0; getline; print "lvcreate -L " $2 " -n " $1 " " vg}' test.txt
lvcreate -L 4080 -n idx01_4g 1vgidx1
lvcreate -L 6120 -n idx02_4g 2vgidx2
lvcreate -L 8160 -n idx03_4g 3vgidx3

This should work without worrying about the VG or LV starting characters.

Jim
Hindsight is 20/20
Volker Borowski
Honored Contributor

Re: awk for lvcreate

Hi,

no syntaxcheck, and no empty lines required !


awk '{ VG=$1
getline
print "lvcreate -L " $2 " -n " $1 " " VG
}' infile

lvcreate -L 4080 -n idx01_4g vgidx
lvcreate -L 4080 -n idx02_4g vgidx
lvcreate -L 4080 -n idx03_4g vgidx
lvcreate -L 4080 -n idx04_4g vgidx
lvcreate -L 4080 -n idx05_4g vgidx
lvcreate -L 4080 -n idx06_4g vgidx
lvcreate -L 4080 -n idx07_4g vgidx


Trick is to store the VG-Name in VG and to read the next line and after that generate the output line.

Hope this helps
Volker