How ti Grep for a word and print the next word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How ti Grep for a word and print the next word
# 1  
Old 01-13-2012
How ti Grep for a word and print the next word

Hi
can we grep for a word and print the next word of the greped word?

ex:- create or replace function function_name
create function function_name

we should search for word "function" and output next word "function_name" from both lines.
# 2  
Old 01-13-2012
Code:
 
$ nawk '{for(i=1;i<NF;i++){if($i~/^function$/){print $(i+1)}}}' test.txt
function_name
function_name

$ cat test.txt
create or replace function function_name
create function function_name

# 3  
Old 01-13-2012
It works!!

Thanks Smilie
it worked.
i changes your code to
Code:
awk '{for(i=1;i<NF;i++){if(tolower($i)~/^function$/){print $(i+1)}}}' testsql

to make the search case insensitive

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-13-2012 at 04:34 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 01-13-2012
Next time onwards, use the code tag to post the code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursively grep for a pattern and print that whole word

Hello Forum Members, I am trying to write a script for a requirement where i have to recursively search for a pattern and replace it with the new string in run time from user inputs grep -ohr "]*.xyz.com]*" $HOME/source/group/ | sort | uniq > $HOME/output.txt while read -r -u9 line; ... (4 Replies)
Discussion started by: raokl
4 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Shell Programming and Scripting

[Solved] Search for a word and print the next word

Hi, I am trying to search for a word and print the next word. For example: My text is "<TRANSFORMATION TYPE ="Lookup Procedure">" I am searching for "TYPE" and trying to print ="Lookup Procedure" I have written a code like following: echo $line | nawk... (4 Replies)
Discussion started by: sampoorna
4 Replies

5. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

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

7. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

8. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

9. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question