LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-17-2007, 04:52 AM   #1
kalyanofb
LQ Newbie
 
Registered: Feb 2007
Posts: 21

Rep: Reputation: 15
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
 
Old 02-17-2007, 06:10 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I'm not clear what you mean?
 
Old 02-18-2007, 09:25 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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.
 
Old 02-18-2007, 11:20 PM   #4
kalyanofb
LQ Newbie
 
Registered: Feb 2007
Posts: 21

Original Poster
Rep: Reputation: 15
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
 
Old 02-19-2007, 01:55 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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"
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with awk kshkid Programming 1 12-22-2006 05:59 AM
Do you know awk? Vitalka Linux - General 2 12-01-2006 11:53 PM
Do you know awk? Vitalka Programming 5 12-01-2006 10:42 PM
using awk meniscus Linux - Newbie 6 10-05-2006 12:39 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration