Get index value by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get index value by awk
# 1  
Old 04-27-2012
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:
Code:
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:
Code:
4
48
4

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 04-27-2012 at 07:52 AM..
# 2  
Old 04-27-2012
Code:
awk -F"," -vIndex=4 '{print $Index}' filename

# 3  
Old 04-27-2012
Code:
# awk -F, -vi=4 '{print $i}'

# 4  
Old 04-27-2012
result print the original file, but not what I expect
# 5  
Old 04-27-2012
use nawk

Code:
 
nawk -F, -v i="4" '{print $i}' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 04-27-2012
It work. Thanks everyone. :>
# 7  
Old 04-27-2012
@mimilaw,

Next time, when you are asking question, let us know the OS and the shell you are using.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

awk : search last index in specific column

I am trying to search a given text in a file and find its last occurrence index. The task is to append the searched index in the same file but in a separate column. I am able to accomplish the task partially and looking for a solution. Following is the detailed description: names_file.txt ... (17 Replies)
Discussion started by: tarun.trehan
17 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

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

5. 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

6. Shell Programming and Scripting

awk: reading into an array and then print the value corresponding to index

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (19 Replies)
Discussion started by: akshaykr2
19 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

Index problem with awk

Hello, I have this code: #!/bin/sh awk -v val="........." 'BEGIN{FS=OFS=" ";c=0} NR==FNR&&d==0{a=$0; c++;next} FNR==(NR-c){b=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) ... (2 Replies)
Discussion started by: jolecanard
2 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