Visit Our UNIX and Linux User Community


Grep output to awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep output to awk command
# 1  
Old 09-24-2014
Grep output to awk command

Hi Team(Solaris 5.8/Ksh),

How can we save grep output to awk variable when grep returns more than one line or word.
abc.log
Code:
[Makarand] # more abc.log
Hi Makarand
How r u
bye Makarand
Hello

when grep returns only 1 word below command works
Code:
nawk -v var=`cat abc.log |grep "Hello"` 'BEGIN { if (var != "") {print var;exit;}}'

output Hello
When grep command returns more than one word or more than one line it fails
Code:
nawk -v var=`cat abc.log |grep "Hi"` 'BEGIN { if (var != "") {print var;exit;}}'

output
Code:
nawk: can't open file BEGIN { if (var != "") {print var;exit;}}
source line number 1

or
Code:
nawk -v var=`cat abc.log |grep "Makarand"` 'BEGIN { if (var != "") {print var;exit;}}'

output:
Code:
nawk: can't open file BEGIN { if ("var" != "") {print var;exit;}}
source line number 1

how to store & compare multiline or multi word grep output in awk variable???

Last edited by vbe; 09-24-2014 at 12:14 PM..
# 2  
Old 09-24-2014
There is no need to use cat or grep with awk.

What are you exactly trying to accomplish?

Maybe using arrays would help you. For example to store all lines containing the word "Makarand" into an array, you can use:

Code:
awk '/Makarand/{array[$0]};END{for (x in array) print x}' abc.log

# 3  
Old 09-24-2014
Just enclose the var in quotes:
Code:
awk -v var="`cat kk2 |grep "Makarand"`" 'BEGIN { if (var != "") {print var;exit;}}'

As @pilnet101 points , your code is getting messy. Smilie
# 4  
Old 09-24-2014
Note: I moved the double quotes...

Code:
nawk -v var="`cat abc.log |grep Hi`" 'BEGIN { if (var != "") {print var;exit;}}'

# 5  
Old 09-24-2014
thanks mates for responses
i want to grep word Makarand if found i want to mail the grep output to my email....if doest not found .. do nothing...
I tried in below way but not working
Code:
nawk -v var="`cat abc.log |grep Makarand`" 'BEGIN { if (var != "")  {system "print var | mailx -s "Found something" abc@abcsys.com" |getline users;print users}}'

Note: 1) Grep out put can be multiline
2) I have to grep only once...
# 6  
Old 09-24-2014
Is there any need to use awk here? Can't you just use grep and the mailx command alone?

---------- Post updated at 10:51 AM ---------- Previous update was at 10:46 AM ----------

How about something like:

Code:
var=$(grep Makarand abc.log)  
[[ ! -z "${var}" ]] && {
        echo "${var}"|mailx -s "Found something" abc@abcsys.com"|echo "${var}"|head -1        
}

This User Gave Thanks to pilnet101 For This Post:
# 7  
Old 09-24-2014
yes...working...thanks
actualy it is some other user's post which i was trying to solve...i will reply to him..anyways thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #946
Difficulty: Medium
A ping flood attack is a simple DOS attack where the attacker overwhelms the victim with TCP echo request packets.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - grep particular word from output

Hi Experts, - Getting error while using it through a variable to get the PID, PID=42 # UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk '{if ($2~"^42$") print $0}' 0.00 42 0 Feb 10 600-17:21:29 nfs_async_io - But when using with a variable it is not working . #... (6 Replies)
Discussion started by: rveri
6 Replies

2. Shell Programming and Scripting

Grep command output in tabular format

I have a grep command script which works fine and give the correct results but i wanted the output to be displayed in tabular format ? Is it possible to display the output in tabular format and as well direct them to some file. main script : #!/usr/bin/bash Start_Time=`date '+%m%d%y... (1 Reply)
Discussion started by: Optimus81
1 Replies

3. Shell Programming and Scripting

awk or grep to search one column and output the other

Hello, it would be great if someone can help me with the following: I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched? cat fileA... (7 Replies)
Discussion started by: Manyaka
7 Replies

4. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

5. Shell Programming and Scripting

grep only a string on command output

How can I grep exactly a string that has .,/ characters using grep? Example: I want to grep ONLY string1 and not string1.more or string1.more.evenmore #lsauth ALL|grep 'string1' All output: string1 <--- This is the only I want. string1.more string1.evenmore. more.string1... (4 Replies)
Discussion started by: iga3725
4 Replies

6. UNIX and Linux Applications

How to redirect grep command output to same file

Hi Everyone, Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command. $grep pattern file1 > file1 Kind Regards, Eswar (5 Replies)
Discussion started by: picheswa
5 Replies

7. UNIX for Dummies Questions & Answers

Using grep output as input for sed command

Hi, I would like to know if this is possible, and if so what can i do to make this work. I would like to grep a line X from fileA and then use the output to replace a word Y in fileB. grep "line X" fileA | sed -e 's/Y/X/g' > outfile this statement does not work, as i do not know how to... (7 Replies)
Discussion started by: cavanac2
7 Replies

8. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

9. Shell Programming and Scripting

pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not. #!/bin/sh if netstat -a | grep `grep ${1}: /tomcat/bases | awk -F: '{print $3}'` > /dev/null then echo Tomcat for $1 running else echo Tomcat for $1 NOT running fi the /tomcat/bases is a file that... (2 Replies)
Discussion started by: ziggy25
2 Replies

Featured Tech Videos