AWK Position Variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Position Variables
# 1  
Old 10-22-2010
AWK Position Variables


Hello guys,
I'm stuck with AWK and probably stupid stuff. Smilie
I've a file with some values like:
Code:
123 456 10 11 90 39 20 ...........

Then, extracting these vaues from that file is quite simple:
Code:
tail -1 "filename"|awk '{printf $2}'

This will show: 456

Now, what I don't realize is how to do a sequentially extraction using the for cycle:

Code:
for position in 1 2 3 4 5
do
     tail -1 "filename"|awk '{printf ${position}}'
done



The above string is not correct since I receive an error (awk: line 1: syntax error at or near {)

Could someone help me to understand how extracting all values using a for and a position variable ??

Thanks!


Last edited by Scott; 10-23-2010 at 11:46 PM.. Reason: Please use code tags
# 2  
Old 10-22-2010
------

Last edited by ctsgnb; 10-22-2010 at 08:45 PM.. Reason: didn't understood the question
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-22-2010
OmG! Searching for the third time on forum give me a solution! Smilie
Sorry, first to post I searched without find anythig! Smilie

Well, the solution is:

Code:
for position in 1 2 3 4 5
do
            tail -1 "filename"|awk -v x=$position '{printf $x }'
done







---------- Post updated at 06:48 PM ---------- Previous update was at 06:40 PM ----------




Thanks for reply ctsgnb, I already know that $0 mean the whole row, but I need to analyze values one by one, and for sure I remove some code from "for cycle"! Smilie

Btw, this is another approach:
Code:
tail -1 "filename" | read a b c d e f

And it was apreciated! Smilie
But if I want only some positions (e.g. 1, 3 and 5) I think the better way will be the cycle (ok here we have only 5 values, but in the real row I have 48 values!!!)

Last edited by Scott; 10-23-2010 at 11:46 PM.. Reason: Code tags
# 4  
Old 10-23-2010
Solution without tail -1

Code:
for position in 1 2 3 4 5
do
    awk -v x=$position 'END {printf $x }' filename
done

# 5  
Old 10-23-2010
awk can do all of that for you....

If i understand correctly your input file is:
Code:
123 456 10 11 90 39 20

your desired output is:

Code:
123
456
10 
11
90
39
20

Code:
echo "123 456 10 11 90 39 20" | awk '{split($0,blah," ");{for(i=1;i<=NF;i++)print blah[i]}}'
123
456
10
11
90
39
20

# 6  
Old 10-23-2010
Code:
# cat file
123 456 10 11 90 39 20
# tr \  \\n <file
123
456
10
11
90
39
20

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

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

awk usage for position matching

i have a requirement like this if the line contains from position 294 to 299 is equal to "prabhu" ,then print entire line . i want to use awk awk '{if(substr(294-299) == 'prabhu') print "line" }' filename (1 Reply)
Discussion started by: ptappeta
1 Replies

4. Shell Programming and Scripting

awk pattern search with inconsistent position

Hi, Anybody knows how to get the value after the regexp and test it on pattern? The if the pattern matches, it will print the entire line on a separate file. Here's my raw file: ^_Name^_string^_Apple ^_Color^_string^_Red ^_Code^_string^_121 ^_Name^_string^_Banana ^_Code^_string^_123 ... (7 Replies)
Discussion started by: Jin_
7 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Find position of character with awk

Hi Guys! Could anyone help me with?.. I have a line which says BCVGF%6$#900 .....How can we know which position is for % or say $ by command or script?There is any way to get a prompt by any script? Thanks a lot (6 Replies)
Discussion started by: Indra2011
6 Replies

6. Shell Programming and Scripting

Help with awk script and update position

Hi, I'm using AIX 5.3 and am trying to get this to work as I need: echo 6078: 6072 6073 6074 6075 6076 6077 6078 6079 | awk '{n=$8} n{sub(n,n+1000000)}1' 1006078: 6072 6073 6074 6075 6076 6077 6078 6079I only want it to change second occurance of 6078 (ie $8) and not the first ($1). I... (4 Replies)
Discussion started by: say170
4 Replies

7. Shell Programming and Scripting

Awk Line/Row Position

Hi guys. I'd just like to know if its possible to change the actual line/row position in awk while its busy processing a file. In other words, is it possible to jump from line 10, back up to line 5 and continue processing line-by-line from then onwards? or is the way around this to add all lines... (3 Replies)
Discussion started by: going_grey
3 Replies

8. Shell Programming and Scripting

Position of a string using AWK

I'm plowing through a bunch of text (and I have to use awk) and need to identify which position a certain string is in. Consider the two lines... UP BROADCAST RUNNING SLAVE MULTICAST MTU:1372 Metric:1 UP LOOPBACK RUNNING MTU:16436 Metric:1 I can identify the line by searching for MTU:.... (3 Replies)
Discussion started by: scottwevans
3 Replies

9. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

using awk removing newline and specific position

Hello Friends, Input File looks as follows: >FASTA Header1 line1 line2 line3 linen >FASTA Header2 Line1 Line2 linen >FASTA Header3 and so on ....... Output: Want something as: >FASTA Header1 line1line2line3linen >FASTA Header2 (5 Replies)
Discussion started by: Deep9000
5 Replies
Login or Register to Ask a Question