awk - why this is not working? trying next word!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - why this is not working? trying next word!
# 1  
Old 05-13-2014
awk - why this is not working? trying next word!

Hi Experts,
Can you please advise , why I am not able to make it work, or why this is not working: I spent quite a lot of time on this figuring out , but not working,




file :
Code:
This is a test file thanks for your reply
This is another file again 
Have a nice day this is a small file thanks again.

I am trying:
Code:
cat file|awk '{for(i=i;i<=NF;i++)if($i~"file") print $(i+1)}'


Output:
Code:
cat file|awk '{for(i=i;i<=NF;i++)if($i~"file") print $(i+1)}'
awk: Field $() is not correct.
 The input line number is 1.
 The source line number is 1.


The output should be :
Code:
thanks 
again
thanks


Last edited by rveri; 05-13-2014 at 08:41 PM..
# 2  
Old 05-13-2014
i in $i is undefined because you incorrectly initialize it to itself.

Regards,
Alister
# 3  
Old 05-13-2014
So how do I get the above output, do I have to store in an array first. Thank you ..
# 4  
Old 05-14-2014
i=1 not i=i
Code:
< file awk '{for(i=1;i<NF;i++) if($i~"file") print $(i+1)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

3. Shell Programming and Scripting

awk word boundaries not working

Hi, I am trying below code but the word boundaries not seem to be working. What am I doing incorrectly? echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\bq\b/) print "HELLO" ; }' OR echo " ECHO " | awk '{ q="ECHO" ; if ( $0 ~ /\b'$q'\b/) print "HELLO" ; }' Or echo " ECHO " | awk... (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

4. Shell Programming and Scripting

Awk: System command not working in awk

Hi, I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help Following is the code awk -F"\t" '{print... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

How to get the exact word in awk?

Hi, i have a file that contains the following: ARTPRD01_app = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.222.3.4)(PORT = 1540)) (CONNECT_DATA = (SERVICE_NAME = artprd01.com) ARTPRD01 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.223.3.1)(PORT =... (2 Replies)
Discussion started by: reignangel2003
2 Replies

6. Shell Programming and Scripting

sed working as uniq 1st word only

Hello, everyone. I am having trouble figuring out sed command which emulates uniq. The task I want to do is that 2 consecutive lines in file should be considered the same using the first word only. Example: cat tmp.txt ddd eee aaa bbb ccc ddd eee fff asd fdd asd fdd bbb aaa bbb asd fgh... (4 Replies)
Discussion started by: motorcek
4 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. Shell Programming and Scripting

Awk for last word

Hi I am writing a script using nano and I'm asking the user for their full name, I then store this in a variable but I only want to display the last word that they input. Any idea on how I would do this using awk. I am not using sed, just using a script. So far I have got this... echo "What... (1 Reply)
Discussion started by: Addman1991
1 Replies

9. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

10. UNIX for Dummies Questions & Answers

grep for word not working

Hi All..I need a help i am trying to find a word using below script whereas the word exists in my file nitin.txt as a directory but still i am getting "word not found" output..Your suggestions welcomed.: #to check for existence of nitin #!/bin/bash cd /apps/uat1/deploy/app ls -lrt >... (4 Replies)
Discussion started by: nattynitin
4 Replies
Login or Register to Ask a Question