Ribs Ribs - 7 months ago 15
Bash Question

Extract string under a string from a file in linux

This site have helped me a lot in the past but signed up only now as I couldn't find an exact answer for what am trying now. I hope you would be able to help.

I'm trying to extract or cut the values under a string in a file. My file looks like:

Xmqqstab v1.0 - Developed by Oliver Fisse (ISSW)

****
**** Tuesday, April 26, 2016 10:49:21 AM BST ****
****
UNIX
Report for queue(s): 'Q1' on queue manager 'QMGR'...

CQD PQF MxQD OIC OUC UNC LGETDATE LGETTIME LPUTDATE LPUTTIME QOM G P Local Queue Name
----------------------------------------------------------------------------------------------------------------------------------------------------
5 0.10% 5000 0 0 0 2016-04-26 10.48.46 2016-04-26 10.49.01 26s E E - Q1

1 queue(s) matching.


The file actually gives MQ queue statistics and am trying to get the values under say, CQD=5 (this could be any digit number), LGETTIME=10.48.46.

I could get the line with the values using : grep -A3 'CQD' file.txt | sed -n '3p'. And from the line i will have to cut the values by column but I thought it wont be accurate to do so as the digits could vary like 5 in this case could be a four digit number in which case the column numbers would change.

I hope am clear with the question.

Would appreciate any help.

Thanks.

Answer

Assuming the data you're interested in is the 1st line that starts with a digit:

read my_CQD my_LGETDATE <<< $(awk '$1 ~ /^[0-9]/ {print $1, $8; exit}' file.txt)

Output:

echo -e "\$my_CQD: $my_CQD\n\$my_LGETDATE: $my_LGETDATE"
$my_CQD: 5
$my_LGETDATE: 10.48.46
Comments