LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2010, 06:45 AM   #1
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
How to get search results to array by awk


I have this multiline variable:

Code:
block_new="title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img"
In one thread here grail written this code to get array with results of search of uuids:

Code:
uuid=($(echo "$block_new" | awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");print}'))
This returns array with uuids.

Now I tried several ways how to get similar array but with values like hd(0,2) and similar.

These are the tested codes
Code:
#1
hd=($(echo "$block_new" | awk '/-/{ x=gensub(/.*(\(hd[[:digit:]],[[:digit:]]+\)).*/,"\\1","g");  print x }'))
#2
hd=($(echo "$block_new" | awk 'BEGIN{RS="hd"}/-/{ x=gensub(/([[:digit:]],[[:digit:]]+)).*/,"\\1)","");print $0="(hd"x}'))
#3
hd=($(echo "$block_new" | awk 'BEGIN{RS="hd"}/-/{ x=gensub(/([[:digit:]],[[:digit:]]+)).*/,"(hd\\1)","");print x}')) 
#5
hd=($(echo "$block_new" | awk 'BEGIN{RS="hd"}/-/{ gsub(/([[:digit:]],[[:digit:]]+\)).*/,"(hd\1)"); print}'))
echo ${hd[@]};
The codes #1 to #4 should return result (hd0,2). Problem is that it is not a array.

Last try #5 more similar to grail's code. #5 returns "(hd)" without substitution. I run out of ideas. Some ideas?
 
Old 05-05-2010, 08:27 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Using the sample you gave, all of your awk statements only produce a single text string.

The pattern "array=( )" will set one array element for each separate* string inside the parentheses. For example, "array=( abc def ghi )" will set three elements. Since awk is only outputting one string, there's only one entry.

I duplicated your text string 3 times, with slight modifications. When I did, your expressions 2-5 work (#1 doesn't match properly).
Code:
$ block_new='title Sata Mandriva
kernel (hd0,1)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,1)/boot/initrd.img
title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img
title Sata Mandriva
kernel (hd0,3)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c
resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,3)/boot/initrd.img'


$ hd=( $(echo "$block_new" | awk 'BEGIN{RS="hd"}/-/{ x=gensub(/([[:digit:]],[[:digit:]]+)).*/,"\\1)","");print $0="(hd"x}') )

$ echo "${hd[0]}"
(hd0,1)
$ echo "${hd[1]}"
(hd0,2)
$ echo "${hd[2]}"
(hd0,3)
*What's considered a separate string depends on the shell' IFS (internal field seperator) setting. This is space/tab/newline by default.

Last edited by David the H.; 05-05-2010 at 08:33 AM. Reason: correction and addition for clarification
 
1 members found this post helpful.
Old 05-05-2010, 08:41 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
block_new="title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c  resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img"

hd=$(echo "$block_new" | awk 'BEGIN{FS="\n";RS=""}{print gensub(/.*(\(.*\)).*/,"\\1","g")}')

echo $hd
Alternate "hd=" line:
Code:
hd=$(echo "$block_new" | awk 'gsub(/^k[^(]+|\/.*vm.*/,"")')

Last edited by grail; 05-05-2010 at 08:59 AM.
 
1 members found this post helpful.
Old 05-05-2010, 11:38 AM   #4
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Smile

Quote:
Originally Posted by David the H. View Post
I duplicated your text string 3 times, with slight modifications. When I did, your expressions 2-5 work (#1 doesn't match properly).
Oops! I thought that the result of my code should be:
$ echo "${hd[@]}"
(hd0,2) (hd0,2)

But you surprised me. I hadn't idea to change the number. That is exactly what I need. Because I don't want duplicate numbers/item/records in the array.

Last edited by webhope; 05-05-2010 at 11:40 AM.
 
Old 05-05-2010, 11:54 AM   #5
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
Alternate "hd=" line:
Code:
hd=$(echo "$block_new" | awk 'gsub(/^k[^(]+|\/.*vm.*/,"")')
Also interesting. It looks that this is coming out from presumption that the source string is not dynamical. The substring to remove begins at "k" character (kernel). The second part to remove which is after pipeline specifies the word "vmlinuz". Right?

Last edited by webhope; 05-05-2010 at 11:55 AM.
 
Old 05-05-2010, 11:59 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Also interesting. It looks that this is coming out from presumption that the source string is not dynamical. The substring to remove begins at "k" character (kernel). The second part to remove which is after pipeline specifies the word "vmlinuz". Right?
Correct
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Elvis search results Josh000 Slackware 9 04-04-2010 09:16 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Search results in RSS Rotwang LQ Suggestions & Feedback 24 01-18-2007 11:44 AM
php array sort inconsistent results rblampain Programming 2 04-03-2006 12:34 AM
search within results toastermaker LQ Suggestions & Feedback 1 11-20-2003 01:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration