Computing.Net > Forums > Unix > Shell script:find word in big file

Shell script:find word in big file

Reply to Message Icon

Original Message
Name: Terry
Date: January 14, 2003 at 03:41:45 Pacific
Subject: Shell script:find word in big file
OS: unix
CPU/Ram: -
Comment:

how do I write shell script that find my word in file.
That's easy for using grep .
But i want find number of line that i found first(with my word) and exit (don't search anymore). (Just only want the first line that i serach)
beacuse my file is very large and i don't want to waste time to find.
thank all


Report Offensive Message For Removal

Response Number 1
Name: James Boothe
Date: January 14, 2003 at 07:18:28 Pacific
Subject: Shell script:find word in big file
Reply: (edit)

A one-liner solution would be:

awk '/just testing/ {print;exit}' myfile

And a script follows. If the passed grep string has embedded spaces, enclose entire string in single or double quotes. If the grep string includes a single quote, you can enclose the grep string in double quotes. If grep string includes a double quote, you can enclose grep string in single quotes.

#!/bin/ksh

if [ $# -eq 1 ] ; then
string=$1
else
echo 'Need one grep string.'
echo 'Surround in single or double quotes if needed.'
exit
fi

echo '======================================='
echo 'Searching for first occurrence only of:'
echo "\"$string\""
echo '======================================='

awk '/'"$string"'/ {print;exit}' myfile

exit 0


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Shell script:find  word in big file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software