Operating System - HP-UX
1819899 Members
2456 Online
109607 Solutions
New Discussion юеВ

Re: Finding a string in a file

 
JUAN_17
Occasional Advisor

Finding a string in a file

Hello everybody!!

How i can find a string in a file?

Sorry, but i'm a "freshman" in HPUX-11
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: Finding a string in a file

Hi:

# grep thestring filename

For example, if you want to find the string "host" in the file '/etc/hosts' you would do:

# grep hosts /etc/hosts

See the man pages for 'grep'.

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: Finding a string in a file

You can use grep, or cat and pipe to awk.

RGds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
John Poff
Honored Contributor

Re: Finding a string in a file

Hi,

One way to find a string in a file is with the 'grep' command:

grep somestring filename

Do a 'man grep' and take a look at the docs. There are lots of regular expressions you can use for finding strings.

JP
V. Nyga
Honored Contributor

Re: Finding a string in a file

Hi,

two possibilities:

grep -i 'string' *
gives you the filename and the line with the string.

If you edit a file with vi:
/string 'enter'
get the curser to the string.

More questions?

Volkmar
*** Say 'Thanks' with Kudos ***
Jon Mattatall
Esteemed Contributor

Re: Finding a string in a file

Depends...

If you're at a prompt -
grep "string" /path/to/filename

If in "vi" -
/string will take you to the first occurence of the string, "n" takes you to the next.
A little knowledge is dangerous - none is absolutely terrifying!!!
Domenico_5
Respected Contributor

Re: Finding a string in a file

grep file
JUAN_17
Occasional Advisor

Re: Finding a string in a file

Gracias a todos!!

thanks to all of you!!

Every day, i learn more...
Donny Jekels
Respected Contributor

Re: Finding a string in a file

freshman huh? welcome to the free world.

grep is the tool you need to study.

grep -v something *

will print all the lines in all the files in the present directory, EXCEPT the lines that contain "something" - cool right?

grep -E "name && lastname" filename

will get name and lastname out of a file.

grep -E "name || lastname" filename

will get either name or lastname out of the file.

enough from the peanut galary -

hack away - peace
Donny
"Vision, is the art of seeing the invisible"
Kelli Ward
Trusted Contributor

Re: Finding a string in a file

Here's a fun one.

Want to find a text string in a file, but do not know which file or where to look?

cd to root - cd /
and type:

find . -type f -exec grep -l '' {} \;

Has been invaluable for me at times.

Note: Can be a resource hog and take a long time if you have a substantial filesystem.

Enjoy HP-UX!

Kel
The more I learn, the more I realize how much more I have to learn. Isn't it GREAT!
Carl_35
Occasional Advisor

Re: Finding a string in a file

You can also use the command "strings" to find text strings in a binary file.

This is useful when looking for error messages, or other messages, when you don't have source code.

Again, this usually gets piped through grep...

strings | grep

By the way, none of these answers is specific to hp-ux, they'll work with any unix/linux system.

Enjoy!