0

I have a File under Linux /tmp/volume.log and the content is:

[id:1091666, name:root, deviceName:/dev/sda, maxStorage:107374182400, unitNumber:0, displayOrder:0, rootVolume:true],[id:1091851, name:/var/log/devops|40GB;/home/devops|150GB, deviceName:/dev/sdb, maxStorage:289910292480, unitNumber:1, displayOrder:1, rootVolume:false]],

It will be in single line. This file is generated by some other script.

My Aim is to find the non-root volume disk and parse it to LVM Commands as Variable to extend or create new volumes, at the time of OS Build.

In the above Example.

There are two disks, 1 - sda which is root disk, (if rootVolume is true it is root disk) 2 - sdb which is non root disk (if rootVolume is false it is non root disk)

I have to grep only

rootVolume=false

and grep then

deviceName:/dev/sdb

followed by:

name:/var/log/devops|40GB;/home/devops|150GB

Once I parse all the above in to Variables, I can call those variables in the Commands like

#!/bin/bash
Disk_name=/dev/sdb
Size_Value=40G
LV_name=var_log_devops
Mount_Point=/var/log/devops

sudo pvcreate $Disk_name
sudo vgcreate vgrp01 $Disk_name 

sudo lvcreate -L $Size_Value -n $var_log_devops vgrp01
sudo mkfs.xfs $var_log_devops
sudo mkdir -p $/var/log/devops
sudo mount $var_log_devops  $/var/log/devops

Right now I am struck with, Extracting the Values from the Text file, becasue they are in single line and multiple Delimiters are present.

2 Answers 2

0

How far would this get you:

awk -vRS="]" '/rootVolume:false/ {for (i=1; i<=NF; i++) if ($i ~ /name|deviceName/) print $i}' file
name:/var/log/devops|40GB;/home/devops|150GB,
deviceName:/dev/sdb,

It splits the single line at ], then scans through all fields to find the target ones, and prints them. You could read the result into shell variables like so, then:

read VARA VARB REST <<< $(awk -vRS="],*" -vORS=" " '/rootVolume:false/ {for (i=1; i<=NF; i++) if ($i ~ /name|deviceName/) print $i}' file)
echo $VARA
name:/var/log/devops|40GB;/home/devops|150GB,
echo $VARB
deviceName:/dev/sdb,
2
  • Hey, this is near perfect. How can i get only the below details ? example : #awk -vRS="]" '/rootVolume:false/ {for (i=1; i<=NF; i++) if ($i ~ /name|deviceName/) print $i}' /tmp/a | grep deviceName: deviceName:/dev/sdb, I would like to have only /dev/sdb and make that as variable. Oct 1, 2018 at 12:59
  • With bash's "process substitution", you could try like . <(awk -vRS="],*" '/rootVolume:false/ {for (i=1; i<=NF; i++) if ($i ~ /name|deviceName/) {sub (/:/, "=\"", $i); sub (/,$/,"\"", $i); print $i}}' file) to define the "name" and "deviceName" variables. Watch the leading dot (for "source")!
    – RudiC
    Oct 1, 2018 at 13:27
0

You can use the command cut something like:

cat /tmp/volume.log | cut -d, -f14 | tr -d "]["
rootVolume=false

make it a variable:
rootvolume=$(cat /tmp/volume.log | cut -d, -f14 | tr -d "][")

cat /tmp/volume.log | cut -d, -f3
deviceName:/dev/sda

cat /tmp/volume.log | cut -d, -f9
name:/var/log/devops|40GB;/home/devops|150GB

Or with grep

cat /tmp/volume.log | grep -o "rootVolume:false]]," | tr "][,"

make it a variable:
rootvolume=$(cat /tmp/volume.log | grep -o "rootVolume:false]]," | tr "][,")

cat /tmp/volume.log | grep -o "deviceName:/dev/sda"

cat /tmp/volume.log | grep -o "name:/var/log/devops|40GB;/home/devops|150GB" | tr -d "\n"

-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Use tr after grep to remove [], ....etc from your output.

6
  • I gave you an example of the text file, but in real we might have more disk's. So I have to grep only the rootvolume=false and once the match is found. Then I need to get the false boot disk name. Then the other partition. Oct 1, 2018 at 11:34
  • Hi @user3179298. You can change -f14 to match the information you want in the file. Does this make sense?
    – user88036
    Oct 1, 2018 at 11:35
  • Hello Gora, the Field will change server to server. Some times the field 14 will have root volume disk. Oct 1, 2018 at 12:05
  • No worries, I will tweak it for you. I thought you have standard files as provided in your example!
    – user88036
    Oct 1, 2018 at 12:06
  • I have a logic, but not sure how to get that done. i see every disk are start and end with [ ..... ],[.....],[.....] So, i need to grep for root volume=false, then need to take that [......rootvolume=false ] as variable and use sed or cut to get the disk and other volume information's. then i will parse it to the script for LVM creation. But not sure how to :( Oct 1, 2018 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .