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.:b:

$ fpm=`awk '$2=="174"  { print $3 }' tempfind.txt` | sed -n /$fpm/p tempfind.txt
t2589vg 8888 11234 -ksh
mqsiadm 174 8888 -ksh
$
awk 'BEGIN {
  while (getline < "file")
     if ($2 == 174)
        ppid = $3
} {
  if ($0 ~ ppid) 
     print
}' file

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" \` &gt;&gt; temp.txt.$$
    echo " $file_dir is  being accessed" &gt;&gt; temp.txt.$$

done

sed -n '/[1][a-z]*/p' temp.txt.$$ >> tempmail.txt
echo "Accessed By: " >>tempmail.txt
sed -n '/[2]/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.:b:


  1. ↩︎

  2. 0-9 ↩︎

Put the sed command inside double ticks as the single ones prevent variable expansion...

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:b:

Another method of solving the original question using getline

$cat awkfile
BEGIN { pid="" }

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

END {
   if (pid != "") {
      tmp="sed -n /"pid"/p file"
      while ((tmp | getline) > 0)
         print
      close(tmp)
   }
}
$
$ awk -f awkfile tempfile.txt
t2589vg 8888 11234 -ksh
mqsiadm 174 8888 -ksh
$

thanks murphy,

Could you please tell or clarify me one thing, is there any problem when we store the value in a variable using awk ?
as:

fpm=`awk '$2=="174" { print $3 }' tempfind.txt` | sed -n /$fpm/p tempfind.txt

and

fpm=`awk '$2=="966906" {print $3}' tempfind.txt` | echo "$fpm"

let me know.
thnks.
Varun.:b:

AFAIK the problem lies in using single quotes in the second awk command. You are running a nested awk, so using single quotes again will confuse the initial outer awk script, (I ran into the same problem before).
So I'd suggest try to avoid the single quotes and use the other methods shown here to assign the variables.

Hope it helps.

Hey,

Could you please tell me, how to get a particular cell value using awk.
As far as I know, awk works on records.,

As :
--------------------------------------
mqsiaaa 123 888
mqsiddd 456 999
t2589gg 789 555
-------------------------------------

In the above table, say i search using $2 column and i want to get the value of $3 of that searched record.

awk '$2~/456/ { printf $3 > "filefilter"}' temp.txt

Means I want the 999 only corrosponding to the search, but as awk works on records and $3 represent whole third column, it used to give me
888
999
555
all the three values in filefilter., BUT i want only 999 (respective to the matched record and its 3rd cell value.)

How to achieve that ??
Do let me know.

Thanks in advance.
Varun.:b:

Sure, but I was refering to the issues using single quotes in a nested awk. Back to your awk command, I see no issues using it as is. From the command line the output is correct:

cat temp.txt

mqsiaaa 123 888
mqsiddd 456 999
t2589gg 789 555

and


awk '$2~/456/ { printf $3 > "filefilter"}' temp.txt

   #The expected output is correct:

cat filefilter
999

Same can be said if you use a variable:

var=`awk '$2~/456/ { printf $3 }' temp.txt`
echo $var
999

Thanks a lot man !!

Could you please check following way of writing awk is correct or not ???


pattern= "mqsi[admtspr]"

  cat tempgrep.txt | \\
   while read ppid
                    do
                            awk '\{
                                     recurcive_fun\(ppid, pattern\)
                                     function recurcive_fun\(n, pattern\)
                                     \{
                                            awk '\{ if \($2~n && $1~pattern\)\{
                                                            n=$3
                                                            recurcive_fun\(n, pattern\)\}
                                                    else\{
                                                            if \($2~n && $1!~pettern\)\{
                                                                    print $1 &gt;&gt; "tempmail.txt"
                                                                    exit
                                                                    \}
                                                            else \{next\}
                                                        \}
                                                    \}' tempfind.txt
                                     \}
                                 \}'

                    done

Its recursion being called.

tempgrep.txt has :
462948
1311040
880922

tempfind.txt has :
t2589vg 880922 462948 09:57:07 -ksh
mqsiadm 1298856 1311040 09:57:56 -ksh
mqsiadm 1311040 880922 09:57:19 -ksh

Please suggust !!
Thanks
Varun:b: