array and awk match function in SunOS 5.10


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array and awk match function in SunOS 5.10
# 1  
Old 04-24-2011
array and awk match function in SunOS 5.10

Hi Experts,

Need help in writing a shell script in SunOS 5.10. I want to use array but it is not running in SunOs where as it is running in unix. pls help
Want to print the alue store in array as below but it is giving error.

Code:
p[1]=1
p[2]=6
p[3]=15
p[4]=20
for i in 1 2 3 4
do
echo ${p[i]}
done

also awk match function in SunOS where as it is working fine in unix.

Code:
a= "this is an example" 
 
d[1]=`echo $a | awk '{ print match($0, "this")}'`
d[2]=`echo $a | awk '{ print match($0, "an")}'`
d[3]=`echo $a | awk '{ print match($0, "is")}'`
d[4]=`echo $a | awk '{ print match($0, "example")}'`
 
for i in 1 2 3 4
do
echo ${d[i]}
done

why it is not running in SunOS and how to run it, pls help

Last edited by DukeNuke2; 04-24-2011 at 06:32 AM..
# 2  
Old 04-24-2011
try this... in solaris use /usr/xpg4/bin/awk instead of awk

Code:
#!/bin/ksh
a="this is an example"

d[1]=`echo $a | /usr/xpg4/bin/awk '{ print match($0, "this")}'`
d[2]=`echo $a | /usr/xpg4/bin/awk '{ print match($0, "an")}'`
d[3]=`echo $a | /usr/xpg4/bin/awk '{ print match($0, "is")}'`
d[4]=`echo $a | /usr/xpg4/bin/awk '{ print match($0, "example")}'`

for i in 1 2 3 4
do
echo ${d[i]}
done

And the follwoing code works for me... may be try putting the interpreter
Code:
#!/bin/ksh

p[1]=1 
p[2]=6 
p[3]=15 
p[4]=20 
for i in 1 2 3 4 
do 
echo ${p[i]} 
done

Both the code tested in Solaris

regards,
Ahamed
# 3  
Old 04-24-2011
Hi Ahamed,
many thnks for the reply
yes , I missed to put "#!/bin/ksh".
Now it is working fine. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

5. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

6. Shell Programming and Scripting

awk match function

Hello, I tried an example with 'match' and print out RSTART and RLENGTH. Sadly it doesn't work. test.txt > yesterday was tuesday today is wednesday tomorrow is thursdayawk -F\\n '{ match( '/day/', $1 ); printf RSTART "," RLENGTH "\n" }' test.txt output:0,-1 (3 Replies)
Discussion started by: daWonderer
3 Replies

7. Shell Programming and Scripting

Match elements in an AWK multi-dimensional array

Hello, I have two files in the following format; file1: A B C D E F G H I J K L file2: 1 2 3 4 5 6 7 8 9 10 11 12 I have read them both in to multi-dimensional arrays. I need a file that has column 2 of the first file printed out for each column 3 of the second file ie... ... (3 Replies)
Discussion started by: cold_Que
3 Replies

8. Shell Programming and Scripting

Awk Array doesnt match for substring

Awk Array doesnt match for substring nawk -F"," 'FNR==NR{a=$2 OFS $3;next} a{print $1,$2,a}' OFS="," file1 file2 I want cluster3 in file1 to match with cluster3int in file2 output getting: Output required: Help is appreciated (8 Replies)
Discussion started by: pinnacle
8 Replies

9. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

10. Shell Programming and Scripting

Unix Korn Shell Array Issue (SunOS)

Hello, I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total... (5 Replies)
Discussion started by: Janus
5 Replies
Login or Register to Ask a Question