Loop through awk results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through awk results
# 1  
Old 12-12-2011
Loop through awk results

I have files structured in stanzas, whose title is '[something]', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title.
The following works:
Code:
awk 'BEGIN{RS="";IGNORECASE=1}/^\[.*'"$id"'.*\]/' myfile

I would need to count all of the occurences, though, AND add each one to a bash array.
The problem is that the above code, with multiple results, returns a whole bunch of lines, as if they were one 'big' result, so I can't have each occurence as an element of the array.
Is there a way to treat each search result as a single element, so that I can assign it to a bash variable?
Hope I've been clear enough.
Thanks for any help.
# 2  
Old 12-12-2011
Please provide a sample input and required output.
# 3  
Old 12-12-2011
try this
Code:
i=0;awk 'BEGIN{RS="";IGNORECASE=1}/^\[.*'"$id"'.*\]/' myfile|while read -r r;do a[i]="$r";echo "${a[++i]}";done

# 4  
Old 12-12-2011
Sample input file:
Code:
[stanza1]
id1: value1
id2: value2
id3: value3

[stanza2]
id1: value1
id2: value2
id3: value3

[stanza12]
id1: value1
id2: value2
id3: value3

Searching for 'stanza1' will return
Code:
[stanza1]
id1: value1
id2: value2
id3: value3
[stanza12]
id1: value1
id2: value2
id3: value3

as a single result.
I would like to access to the content of each stanza, so that I could assign
Code:
[stanza1]
id1: value1
id2: value2
id3: value3

to, e.g., variable i, and
Code:
[stanza12]
id1: value1
id2: value2
id3: value3

to variable j.

---------- Post updated at 02:41 PM ---------- Previous update was at 02:33 PM ----------

Quote:
Originally Posted by ygemici
try this
Code:
i=0;awk 'BEGIN{RS="";IGNORECASE=1}/^\[.*'"$id"'.*\]/' myfile|while read -r r;do a[i]="$r";echo "${a[++i]}";done

Thanks for the reply ygemici, but this will cycle throug each line of the result, that's not what I need.
# 5  
Old 12-12-2011
Maybe something like this?
Code:
# All of them:
$> awk '{_[NR]=$0} END{for(x=1; x<=NR; x++){print _[x]}}' RS= infile
[stanza1]
id1: value1
id2: value2
id3: value3
[stanza2]
id1: value1
id2: value2
id3: value3
[stanza12]
id1: value1
id2: value2
id3: value3

# Element 2:
$> awk '{_[NR]=$0} END{print _[2]}' RS= infile
[stanza2]
id1: value1
id2: value2
id3: value3

Edit:
Just noticed you want to enter a search pattern to get the result; will update this.
# 6  
Old 12-12-2011
'ere we go:
Code:
awk -v pat="stanza1" '{_[NR]=$0} END{pat=pat"]"; for(x=1; x<=NR; x++){if(_[x] ~ pat)print _[x]}}' RS= infile
[stanza1]
id1: value1
id2: value2
id3: value3


Last edited by zaxxon; 12-12-2011 at 10:31 AM.. Reason: corrected typo and change added ] to pat
# 7  
Old 12-12-2011
Code:
perl -ne '$x="stanza1";if(/$x]/../^\n/){print if($_!~/^\n/)}' inputfile.txt


Last edited by balajesuri; 12-12-2011 at 11:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Df -h results in a loop

Hello everyone, I am doing a check of the disk space using df -h, I want to combine the result in break line; but the result after while/done is empty: # df -h Filesystem Size Used Avail Use% Mounted on rootfs 20G 14G 4.6G 75% / /dev/root 20G 14G 4.6G 75% /... (15 Replies)
Discussion started by: Abu Rayane
15 Replies

2. Shell Programming and Scripting

Substitute from awk results

Hi, The following awk command : asmcmd lsdg | awk '{print $13;}' | grep -i ${SID} return the following output . An Empty line + two lines contain "/" at the end of the line INDEVDATA/ INDEVFRA/ I need to remove the "/" as well as the empty line. Please advise Thanks (3 Replies)
Discussion started by: Yoav
3 Replies

3. Shell Programming and Scripting

Narrowing sed Results in While Loop

find $SRC -type f -name *.emlx | while read FILE do if : then sed -n '/From/p' $FILE fi done > $DEST-output.txt The loop above spits out a .txt file with several lines that look like this: From: John Smith <jsmith@company.com> How can I narrow that sed result to spit out the email... (5 Replies)
Discussion started by: sudo
5 Replies

4. Shell Programming and Scripting

Concatenate Loop Results

Hi, I have the following situation: Param1Values = AAAA,BBBB Param1=$(echo $Param1Values| tr "," "\n") for x in $Param1 do db2 select X from Y where Z IN ('$x') done Obviously the above will perform the select 'x' amount of times. Is there a way in which i can... (13 Replies)
Discussion started by: RichZR
13 Replies

5. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

6. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

7. Shell Programming and Scripting

2 CMD results on the same line while rexing in a loop

Folks, I have a 3 problems. In a sh script, I call a server name from a list and rex to a distant machine to get the boot date. for i in `cat list` do (echo "$i|"; /bin/rexsh $i -l bozo -t10 who -b | cut -d" " -f14-16) >>getBootTimes.out sleep 1 done The results are on 2 lines instead... (8 Replies)
Discussion started by: linux_lou
8 Replies

8. Shell Programming and Scripting

need a little help with results from awk

Hi there all, I am using a line to get some replys from my PS I do ps -ef |awk '{printf $9}' But my result is 1 big line. No spaces between the lines or someting for example:... (2 Replies)
Discussion started by: draco
2 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question