LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk (https://www.linuxquestions.org/questions/programming-9/awk-529856/)

kalyanofb 02-17-2007 04:52 AM

awk
 
hai,

i am searching a word in a file using awk. when it finds it will write $2 in other file.

It is working perfectly.

My question is if it does find that word i want to write that work in another file. How i will check whether the word is found or not?

pls. let me know

matthewg42 02-17-2007 06:10 AM

I'm not clear what you mean?

cfaj 02-18-2007 09:25 PM

Quote:

Originally Posted by kalyanofb
hai,

i am searching a word in a file using awk. when it finds it will write $2 in other file.

It is working perfectly.

My question is if it does find that word i want to write that work in another file. How i will check whether the word is found or not?

pls. let me know


Look in the other file. If it was found, it will be there; it is wasn't, it won't be.

kalyanofb 02-18-2007 11:20 PM

awk
 
Mr. Matthew Gates,

getSrc()
{
awk -F '|' -v abc=$1 '$1==abc {printf "%s\n",$2}' $2 >> $3
}

The above code will run whenever getSrc function from shell script which will be called alongwith 3 parameters.

$1 is a search string
$2 is the input file
$3 is the output file

My question is if it finds match it will write into $3. If it doesn't find $1 in $2, i want to write $1 in some other file.

How i will do this?

KALYANA RAMAN. M

matthewg42 02-19-2007 01:55 AM

awk supports similar re-direction facilities as the shell. So inside your awk program, you can write to named files, not just standard output:

Code:

# usage: getSrc pattern in_file found_out_file notfound_out_file
getSrc () {
  awk -F'|' -v pattern="$1" -v in_file="$2" -v of_found="$3" -v of_notfound="$4" \
    '{  if ( $1 == pattern ) {
                print "found " pattern " in " in_file > of_found;
        } else {
                print "did not find " pattern " in " in_file > of_notfound;
        }
    }' "$2"
}



All times are GMT -5. The time now is 07:12 PM.