LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2015, 09:34 AM   #16
doni
LQ Newbie
 
Registered: Mar 2006
Posts: 27

Original Poster
Rep: Reputation: 1

Hi Guys
One more question:

awk '/ORA/{print RS,$1;for(i=1;i<=NF;i++)if($i ~ /ORA/)print $i}' FS='\n' RS='SQL>' logfile


Does anyone know how can I pass variable to awk - instead of ORA I would like to have variable from bash:

grep ORA logfile.txt | while read err; do

awk '/$err/{print RS,$1;for(i=1;i<=NF;i++)if($i ~ /$err/)print $i}' FS='\n' RS='SQL>' logfile



done

Tried with awk -v but didn't work
 
Old 03-20-2015, 09:41 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well you could do all of that in an awk script instead, but to pass variables you can look at the -v option
 
Old 03-20-2015, 10:06 AM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by doni View Post
... Does anyone know how can I pass variable to awk ...
Tried with awk -v but didn't work
awk -v does work. Maybe you made a keying error. Once again, you are asking a question without giving enough background. Show us what you tried. Tell us what you mean by "didn't work." That vague wording could mean ...
- you got a syntax error
- you got no output
- you got incorrect output
- your computer "hung"
- you smelled smoke and the monitor went blank

Daniel B. Martin
 
Old 03-20-2015, 10:11 AM   #19
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You can't use awk variables in regexp literals, you have to use Computed Regexps instead.

Code:
awk -vvar=foo '/var/'     # this searches for the string "var"
awk -vvar=foo '$0 ~ var'  # this searches for the string "foo"
# if you're just searching for a simple string, you don't need a regexp as such:
awk -vvar=foo '$0 == var' # this searches for the string "foo"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Inquiry on a bash script using sed and grep -c Leanux Linux - Newbie 4 01-21-2015 07:20 AM
[SOLVED] bash- sed, awk, grep schneidz Programming 10 01-31-2011 08:56 PM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
extracting part of a string (sed, bash, ...) rama76 Programming 3 01-11-2008 08:14 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration