sed in awk ? or nested awk ? | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

sed in awk ? or nested awk ?

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-29-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
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.
Sponsored Links
    #2  
Old 01-30-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 24 April 2014, 7:17 AM EDT
Location: /dev/ph
Posts: 4,717
Thanks: 54
Thanked 411 Times in 382 Posts

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

Sponsored Links
    #3  
Old 01-30-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 12 April 2014, 1:48 PM EDT
Location: USA
Posts: 1,436
Thanks: 19
Thanked 134 Times in 126 Posts

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

    #4  
Old 01-30-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
PHP Issue still lies ...

Quote:
Originally Posted by fpmurphy View Post
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.
Sponsored Links
    #5  
Old 01-30-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 12 April 2014, 1:48 PM EDT
Location: USA
Posts: 1,436
Thanks: 19
Thanked 134 Times in 126 Posts
Put the sed command inside double ticks as the single ones prevent variable expansion...


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

Sponsored Links
    #6  
Old 01-30-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by shamrock View Post
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 !!
Varun

Last edited by varungupta; 01-30-2008 at 04:07 PM..
Sponsored Links
    #7  
Old 01-30-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 24 April 2014, 7:17 AM EDT
Location: /dev/ph
Posts: 4,717
Thanks: 54
Thanked 411 Times in 382 Posts
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
$

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Nested if in KSH Calbrenar Shell Programming and Scripting 4 10-19-2011 03:29 PM
KSH nested loops? mrice Shell Programming and Scripting 9 07-26-2011 08:17 PM
Help nested sql joje47 Programming 1 05-31-2011 11:25 AM
Nested If statement within Do / Done dalgibbard UNIX for Dummies Questions & Answers 2 12-22-2009 10:10 AM
awk 2 delimiter nested onlyroshni Shell Programming and Scripting 5 10-10-2007 05:01 AM



All times are GMT -4. The time now is 07:50 PM.