Index problem with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Index problem with awk
# 1  
Old 10-04-2008
Index problem with awk

Hello,

I have this code:
Code:
#!/bin/sh
awk -v val="........." 'BEGIN{FS=OFS=" ";c=0}
NR==FNR&&d==0{a[$0]=$0; c++;next}
FNR==(NR-c){b[FNR]=val;next}
{if(1234 in a){print "okay"}}
{print $1}' listi fpr.11 grn

that is working (awk find the value in the table "a" and return "okay" followed by 1234)

and when I am using:
Code:
#!/bin/sh
awk -v val="........." 'BEGIN{FS=OFS=" ";c=0}
NR==FNR&&d==0{a[FNR]=$0; c++;next}
FNR==(NR-c){b[FNR]=val;next}
{if(1234 in a){print "okay"}}
{print $1}' listi fpr.11 grn

awk returns me "1234" but not "okay", meaning that it did not find 1234 in my list (but of course 1234 is in the list!!).

Does someone has any idea why I have this problem?

Thanks
# 2  
Old 10-04-2008

for ss in array searches the subscripts, not the values in the array. In the second script, you have 1234 as a value, not a subscript.

# 3  
Old 10-05-2008
Good to know...thanks!!

How can I change that?
I would like to search among the values in the array and not among the subscripts...I know I can do it with loop:
Code:
 
for (j in array){if(1234==array[j]){print "okay"}}

but is there a specific command to test the content of an array?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Index to get position matches pattern

Input data as below (filetest.txt): 1|22 JAN Minimum Bal 20.00 | SAT 2|09 FEB Extract bal 168.00BR | REM 3|MIN BAL | LEX Output should be: ( If there is Date & Month in 2nd field of Input file, It should be seperated else blank. If There is Decimal OR Decimal & Currency in last of the 2nd... (7 Replies)
Discussion started by: JSKOBS
7 Replies

2. Shell Programming and Scripting

Index problem in associate array in awk

I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script. infile: S01 36407 53706 88540 S02 69343 87098 87316 S03 50133 59721 107923... (4 Replies)
Discussion started by: yifangt
4 Replies

3. Shell Programming and Scripting

awk - index and wildcards

When I search for the string capId=... using awk '/capId=.../' file I get successful results. However when I feed the string as a variable, like this: str="capId=..." awk -v str="$str" 'index($0, str)' file I get no results. What can I do if I need to generate a string that contains... (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

Get index value by awk

HI All, I would like to pass a integer and get all values under this index the by using awk. Could anyone help? Thanks :> input: 1,2,3,4,5,6,7 1,2,3,48,5,6,7 1,2,3,4,5,6,7 e.g. i pass 4 to awk command output: 4 48 4 Video tutorial on how to use code tags in The UNIX and Linux... (8 Replies)
Discussion started by: mimilaw
8 Replies

5. Shell Programming and Scripting

awk index

1 2 000060000 How do i return the point in the string where the 6 is? i.e what I want on output is 1 2 5 something like awk '{print $1 $2 index($3,6) }' but I can't get it to work Thanks in advance (3 Replies)
Discussion started by: garethsays
3 Replies

6. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

7. Shell Programming and Scripting

awk array index help

$ cat file.txt A|X|20 A|Y|20 A|X|30 A|Z|20 B|X|10 A|Y|40 Summing up $NF based on first 2 fields, $ awk -F "|" 'BEGIN {OFS="|"} { sum += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

8. Shell Programming and Scripting

Problem when assign the array with the string index

I come across the problems when assigning the array in the script below . How to use the array with the 'string index' correctly ? When I assign a new string index , the array elements that are previously assigned are all changed .:eek::eek::eek: $ array=211 $ echo ${array} 211 $... (4 Replies)
Discussion started by: youareapkman
4 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question