sed in awk ? or nested awk ?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed in awk ? or nested awk ?
# 1  
Old 01-29-2008
Error sed in awk ? or nested awk ?

Hey all,

Can I put sed command inside the awk action ?? If not then can i do grep in the awk action ??

For ex:

awk '$1=="174" { ppid=($2) ; sed -n '/$ppid/p' tempfind.txt ; }' tempfind.txt

Assume: 174 is string.
Assume: tempfind.txt is used for awk and sed both.

tempfind.txt contains :

t2589vg 8888 11234 -ksh
mqsiadm 128 134 -ksh
mqsiadm 174 8888 -ksh



Please let me know, its an urgent.
Thanks to all brains.

You can give your opinion/examples/suggustions regarding the same (to use: sed in awk or grep in awk or nested awk).

Varun.Smilie
# 2  
Old 01-30-2008
Code:
$ fpm=`awk '$2=="174"  { print $3 }' tempfind.txt` | sed -n /$fpm/p tempfind.txt
t2589vg 8888 11234 -ksh
mqsiadm 174 8888 -ksh
$

# 3  
Old 01-30-2008
Code:
awk 'BEGIN {
  while (getline < "file")
     if ($2 == 174)
        ppid = $3
} {
  if ($0 ~ ppid) 
     print
}' file

# 4  
Old 01-30-2008
PHP Issue still lies ...

Quote:
Originally Posted by fpmurphy
Code:
$ fpm=`awk '$2=="174"  { print $3 }' tempfind.txt` | sed -n /$fpm/p tempfind.txt
t2589vg 8888 11234 -ksh
mqsiadm 174 8888 -ksh
$

Hey murphy,

Thnks for the reply firstof all.

Below is my script :
where i am using your given code..and somehow its not giving desired o/p.
can you please tell me what is wrong in this...

-----------------------------------------------------------------------

#SCRIPT TO CHECK WHO HAS ACCESSED THE LOG/FILE IN PAST 'N' MINUTES, AND MAIL ACCORDINGLY.

MYPATH="/clocal/mqbrkrs/user/mqsiadm/sanjay/"
MAIL_RECIPIENTS="vg517@dcx.com"
Subject=":: File(s) accessed/touched in last few minutes ::"
>tempmail.txt
>tempfind.txt
>filterfile.txt
>tempgrep.txt
#*************************************************************************************************** ****************************************************
## List all the files which one accessed since last 1 min #####
#*************************************************************************************************** *****************************************************


ps -ef | grep "\-ksh" | awk '$8 !~ /grep/ { printf "%s %s %s %s %s\n", $1, $2, $3, $5, $8 ; }' >> ./tempfind.txt

for file_dir in `find $MYPATH -amin -1`
do

echo `fuser -uf "$file_dir" ` >> temp.txt.$$
echo " $file_dir is being accessed" >> temp.txt.$$
done

sed -n '/^[ ][a-z]*/p' temp.txt.$$ >> tempmail.txt
echo "Accessed By: " >>tempmail.txt
sed -n '/^[0-9]/p' temp.txt.$$ > filterfile.txt

for pid_var in `awk '{ print $2 }' filterfile.txt`
do
fpm=`awk '$2=="$pid_var" {print $3}' tempfind.txt` | sed -n '/$fpm/p' tempfind.txt >> tempmail.txt

done

cat tempmail.txt | mailx -s "$Subject" "$MAIL_RECIPIENTS"


-----------------------------------------------------------------------

Its not giving anyting, I tried on commands prompt aswell.
Could you guide me, please.

Thanks
Varun.Smilie
# 5  
Old 01-30-2008
Put the sed command inside double ticks as the single ones prevent variable expansion...

Code:
sed -n "/$fpm/p"

# 6  
Old 01-30-2008
Quote:
Originally Posted by shamrock
Put the sed command inside double ticks as the single ones prevent variable expansion...

Code:
sed -n "/$fpm/p"

hey,

I tried using :
fpm=`awk '$2=="966906" {print $3}' tempfind.txt` | sed -n "/$fpm/p" tempfind.txt
but still its not working. Whenever I do only awk '$2=="966906" {print $3}' tempfind.txt it works and gives the value of $3, but when we assign $3 to fpm variable, its not going ahead.

Even i have tried..
fpm=`awk '$2=="966906" {print $3}' tempfind.txt` | echo "$fpm"
It does not show anything.

Where is the matter ?? Please suggust me, this is the last thing in my script and am waiting to complete it.

Thanks !!
VarunSmilie

Last edited by varungupta; 01-30-2008 at 05:07 PM..
# 7  
Old 01-30-2008
Another method of solving the original question using getline

Code:
$cat awkfile
BEGIN { pid="" }

$2=="174" { pid=$3 }

END {
   if (pid != "") {
      tmp="sed -n /"pid"/p file"
      while ((tmp | getline) > 0)
         print
      close(tmp)
   }
}
$

Code:
$ awk -f awkfile tempfile.txt
t2589vg 8888 11234 -ksh
mqsiadm 174 8888 -ksh
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - multiple and nested if-then-else

Hello. I would like to convert the following piece of code from bash to awk. Here are bash variables in a bash script. CUR_ROW_ID and ROW_ID_TO_SEARCH contains a string which represent a row id. The string contain a valid row id. CUR_ROW_ID sometimes may be null. CUR_VALUE... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies

3. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

4. Shell Programming and Scripting

Nested if not working with /usr/xpg4/bin/awk

Hi, I am trying to do if inside the If in /usr/xpg4/bin/awk. But I am getting below error : /usr/xpg4/bin/awk -v a="${THREADIDARR }" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c]=1;} length($3) == 0{ftag == 1{{print}}; length($3) != 0{$3 in c{ftag=1;print;} !$3 in c{ftag=0;}}'... (4 Replies)
Discussion started by: nes
4 Replies

5. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 Replies

6. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

7. Shell Programming and Scripting

Nested case inside awk

please let me know if the below code could be written efficiently inside single awk case "$INP" in ksh) cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}' ;; pset) cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}' ;; dml) cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

8. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

9. Shell Programming and Scripting

Awk Script: Nested search using different patter

Hi Friends. Please have a look at dummy file. I need to extract from this file: 1. Counts of event= 2. the 2nd coulmn is unique call id of this transaction. Based on that, i have to search for txstatus= . Note: Values of event, calltype and txstatus can be anything. I want to print... (1 Reply)
Discussion started by: itsmesanju
1 Replies

10. Shell Programming and Scripting

awk 2 delimiter nested

Hello All, This work could be very easy for you guys. I would really appreciate help. input file: output file: (Desired) What I am capable of doing: Command: cat inputfile | awk -F\| '{print "num="$1" value="$2" digits="$3" name1="$4" file="$5" code="$6}' > outputfile Result what I am... (5 Replies)
Discussion started by: onlyroshni
5 Replies
Login or Register to Ask a Question