Need help to grep/awk into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to grep/awk into array
# 1  
Old 05-15-2009
Need help to grep/awk into array

Hey guys,

First post, I'm not a UNIX script guru and am trying to hack together something as a requirement ASAP.

I have scanned the forums and Googled for this and have found similar issues but no usable solutions so I am hoping one of you guys can help me out.

Basically I have a batch file filled with data -- a mock up example is below:

Code:
IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000024
IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000008
IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000234

Amongst some other things, I need to extract all instance of lines that are "trailer" records which are indicated with "IMATRL".

I can do that quite easily with grepping. However, in my eventual processings script, I need to get all trailer records out into an array so that I can then use them as a basis of analysing the file for statistical purposes (matching trade data passed in vs what is processed out, etc)

My problem is that no matter what I do, I simply cannot get the "whole line" extracted out into an array. I've tried various playings with 'awk' but it hasn't worked:

`grep ${TRAILER_ID} ${file}|awk 'BEGIN {FS="IMATRL"} {print $1 }'`
or
`grep ${TRAILER_ID} ${file}|awk 'BEGIN {FS="\n"} {print $1 }'`


So far, this is my current test code:

Code:
#!/bin/ksh

TRAILER_ID=IMATRL
file=xxx.txt

set -A y `grep ${TRAILER_ID} ${file}`

i=0
while [[ $i -lt ${#y[*]} ]] ; do
    echo "y[$i] = ${y[$i]}"
    ((i=$i+1))
done

But it dumps out everything to invidual elements :

Code:
y[0] = IMATRL
y[1] = FOO
y[2] = BAR
y[3] = FWEEP
y[4] = 000024
y[5] = IMATRL
y[6] = FOO
y[7] = BAR
y[8] = FWEEP
y[9] = 000008
y[10] = IMATRL
y[11] = FOO
y[12] = BAR
y[13] = FWEEP
y[14] = 000234

Running the basic grep on the command line prints out the 3 lines with neat little "\n" so it looks like what I'd want. But in script, it obviously resolves the grep result to a single string which is then using a single " " whitespace as the tokeniser for the array creation.

Can anyone help me get my array output to look like this :

Code:
y[0]=IMATRL FOO BAR FWEEP 000024
y[1]=IMATRL FOO BAR FWEEP 000008
y[2]=IMATRL FOO BAR FWEEP 000234

And without using a temp file to dump results to before re-parsing?

Thanks guys.
# 2  
Old 05-15-2009
Code:
#!/bin/ksh
typeset -i i=0
awk '/^IMATRL/' inputfilename | while read value
do
  arr[i]="$value"
  i=$(( $i + 1) )
done

The limit is 1023 elements per array in ksh.
# 3  
Old 05-15-2009
Thanks a lot Jim - that works a treat!

As for the 1023 limit - that should be fine -- the batch files I have to process should at *most* have maybe 5 to 6 entries that need to be extracted for array purposes and the lines in production are 80chars in length so that's fine.

Smilie
# 4  
Old 05-15-2009
might be another way to work around

Code:
 
awk '/^IMATRL/ { arr[++i]=$0 } END {for (j in arr) print arr[j] }' rem.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

array and string with for-loop and grep

Hello together, first of all, iam really a beginner in Shellskripting and i need some help please. Following Task i try to finished: vmtoolsd --cmd 'info-get guestinfo.ovfEnv' > vmt In the file vmt are some strings that iam searching for. For that i try to create a array like: ... (2 Replies)
Discussion started by: dreipapier
2 Replies

4. Shell Programming and Scripting

!!EMERGENCY!! - GREP/CUT to array

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa 41aaa 42000 43aaa... (5 Replies)
Discussion started by: gc_sw
5 Replies

5. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 array files using grep

Using the bash shell I am trying to read in 2 files. The first file (file1) is a list of names to search for in (file2). If a name in file1 is found in file2 I want the entire line in file2 to be printed to an output file. File1 consists of a single column of names. File2 consists of several... (2 Replies)
Discussion started by: lanna001
2 Replies

7. Shell Programming and Scripting

How to use variables/array in grep command

Hi, I have a reqmt as i have some values in array and I want to search each value in a file by grep command. Here goes my scripting: #!/bin/ksh set -A ArrayA CENTER LEFT RIGHT echo "ArrayA contains: ${ArrayAİ*¨}" grep -e "${ArrayAİ*¨}" filename.txt The above grep is working for... (4 Replies)
Discussion started by: prashant43
4 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

Perl array grep

Hi, I have the following array. @a=( ,); and want to push @a,() if the 6 is not in the $a. This is simplified out of a longer script but im trying for days now and get sick of it. Can any of you help me on this. Cheers Markus (1 Reply)
Discussion started by: elvis00
1 Replies

10. Shell Programming and Scripting

grep in array -perl

hi all i have 2 array one is refernce array where as other array is generated from text file. array A has all letters from A-z and a-z and i want to find out which letters are missing in generated array and push them in another array to display them. i have following code .... for my $ref... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question