Mani Kandan
Mani Kandan

Reputation: 11

Not getting expected file result using awk

#!/bin/bash
delete_file () {
    for file in processor_list.txt currnet_username.txt unique_username.txt
    do
        if [ -e  $file  ] ;then
            rm $file
        fi
    done
}
delete_file
ps -elf > processor_list.txt ; chmod 755 processor_list.txt
awk '{print $3}' processor_list.txt > currnet_username.txt ; chmod 755 currnet_username.txt
sort -u currnet_username.txt  > unique_username.txt ;chmod 755 unique_username.txt
while read line ; do
    if [ -e  $line.txt ]  ;then
        rm $line.txt
    fi 
    grep $line processor_list.txt >$line.sh ;chmod 755 $line.sh
    awk '{if($4 == "$line") print $0;}' $line.sh > ${line}1.txt ; #mv ${line}1.txt $line.txt;chmod 755 $line.txt
done < unique_username.txt

I'm a beginner of unix shell scripting. please suggested, i am not getting expected results in ${line}1.txt.

For example, I have two UID like kplus , kplustp. what is my requirement is find "kplus" string from ps -elf command and create a file as same name like kplus.txt and redirect or move the data whatever found data using grep command.

But I am getting kplus and kplustp data in kplus.txt file. I need only kplus value based on UID column from ps –elf in kplus.txt file.

Upvotes: 0

Views: 78

Answers (1)

Jotne
Jotne

Reputation: 41446

This is wrong way to read variable using awk

awk '{if($4 == "$line") print $0;}' $line.sh

Use:

awk '{if($4 == var) print $0;}' var="$line" $line.sh

Or shorten to

awk '$4==var' var="$line" $line.sh

default action is {print $0} if no action is specified.


If you need to search for the text $line escape the $ in regex

awk '$4==/\$line/' $line.sh

or in text it should work directly

awk '$4=="$line"' $line.sh

Upvotes: 4

Related Questions