Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with AWK...

Status
Not open for further replies.

psychosb

Technical User
Oct 19, 2011
3
Hello to all!

I would like some help...

I'm trying to use AWK to filter the results of the file /proc/mdstat in linux.

The content of the file is:

[root@hostname~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdq1[0] sdr1[1]
264960 blocks [2/2] [UU]
bitmap: 0/33 pages [0KB], 4KB chunk

md1 : active raid1 sdq2[0] sdr2[1]
142793664 blocks [2/2] [UU]
bitmap: 2/137 pages [8KB], 512KB chunk

I'm trying to create a script that runs the following command:

/sbin/mdadm -Q --detail /dev/mdX

Where the "X" is the number of devices listed on the mdstat file.

I wrote the following script, and it's working fine...
But I'm wondering if there's any way that i can do the same thing, without using the pipe to run the second awk (awk 'NR=='$a -> to get the line number).

for ((a=1; a <= 10 ; a++))
do
mdx=`awk '$1 ~ /^[md]/ {print substr($0,0,3)}' /proc/mdstat | awk 'NR=='$a`
if [[ -n "$mdx" ]] ; then
/sbin/mdadm -Q --detail /dev/$mdx
else
echo
fi
done

I've made a lot of tests trying to use the awk to invoke the system command but without any success.

Does anyone have a idea?

Thanks!
 
Hi

$1 ~ /^[md]/
Do you realize there you wrote if $1's first character is either "m" or "d" ?

As far as I understand it, this would be enough :
Code:
awk '$1~/^md/{print"/sbin/mdadm -Q --detail /dev/"$1}' /proc/mdstat | sh
Do you have to process /sbin/mdadm output too ?


Feherke.
 
system() should also do the trick, although a fraction less efficient:

Code:
awk '[blue]$1[/blue]~[green]/^md/[/green]{[b]print[/b][red]"[/red][purple]/sbin/mdadm -Q --detail /dev/[/purple][red]"[/red][blue]$1[/blue]}' /proc/mdstat


Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Hi

Annihilannic, are you sure you intended to post that code ?


Feherke.
 
Hehe... err... oops!

I was thinking more along these lines:

Code:
awk '[green]/^md/[/green]{[b]system[/b]([red]"[/red][purple]/sbin/mdadm -Q --detail /dev/[/purple][red]"[/red][blue]$1[/blue])}' /proc/mdstat

Thanks feherke... moral of the story, preview before you post! *sigh*


Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Both codes worked perfectly!
Resumed 8 lines in one! =)

I'll try to find some awk library, so I can read more about it...

Thank you very much, feherke and Annihilannic!

Best regards!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top