LinuxQuestions.org
Review your favorite Linux distribution.
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 06-08-2017, 09:44 AM   #1
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Rep: Reputation: 15
Trying to store the output from awk


Hi All,

I have a file containing

abc xyz pqr 300
asd zyz sdf 400

I am using awk to get the output 300
- awk '/abc/ && /xyz/ && /pqr/ {print $NF} /home/file.txt

I got the output as 300 now I have to store the 300 value into a integer to comapre

int1=$(awk '/abc/ && /xyz/ && /pqr/ {print $NF} /home/file.txt)
I have even tried redirecting the output ">> int1"

but nothing seems to work

Thanks in advance.
 
Old 06-08-2017, 10:13 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,372
Blog Entries: 3

Rep: Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049
You seem to be missing the closing quotation mark for that:

Code:
int1=$(awk '/abc/ && /xyz/ && /pqr/ {print $NF}' /home/file.txt);
 
3 members found this post helpful.
Old 06-08-2017, 10:35 AM   #3
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
my bad its works now thank you
 
Old 06-08-2017, 10:36 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,574

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
Turbocapitalist gave good advice in post #2.

Consider that the order of the character strings may be important and cause unexpected results.

With this InFile ...
Code:
abc xyz pqr 300
asd zyz sdf 400
abc pqr xyz 500
... this awk ..
Code:
result=$(awk '/abc/ && /xyz/ && /pqr/ {print $NF}' $InFile)
echo "result="$result
... and this awk ...
Code:
result=$(awk '/abc xyz pqr/ {print $NF}' $InFile)
echo "result="$result
... are not equivalent.

Daniel B. Martin
 
1 members found this post helpful.
Old 06-08-2017, 10:38 AM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,372
Blog Entries: 3

Rep: Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049
No problem.

Depending on what you are trying to do, you might associate patterns with specific fields.

Code:
awk '$1 ~ /abc/ && $2 ~ /xyz/ && $3 ~ /pqr/ {print $NF}' /home/file.txt
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Awk: How to pass/store a command output in variable? shivaa Linux - Newbie 8 01-05-2013 12:30 PM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
Can not capture no at end of file store it and print it using sed and awk amuthiga Programming 10 11-26-2008 07:37 AM
store output of iwconfig ppb Linux - Wireless Networking 1 04-13-2005 12:59 AM
using > to store output student04 Linux - General 2 01-21-2005 07:51 PM

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

All times are GMT -5. The time now is 11:48 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration