Operating System - HP-UX
1833839 Members
2192 Online
110063 Solutions
New Discussion

Re: get a string in a line

 
idi
Occasional Contributor

get a string in a line

hi, anybody help me

how to get a string in a line hp ux?

'i want to solve this problem' and i want to get 'solve' but how?
15 REPLIES 15
Robert-Jan Goossens
Honored Contributor

Re: get a string in a line

could you post an example ?
Muthukumar_5
Honored Contributor

Re: get a string in a line

You can use awk, sed for getting string in a line. grep can used to search it. post example input and output you want.

hth.
Easy to suggest when don't know about the problem!
Giacomo TOTTI
Advisor

Re: get a string in a line

Hi,
if you simply want to get a line in a file, use a grep command, it's a simplier way!!

i.e.

grep test file

(if you want to search a string with a test word)

enjoy

Giacomo

Alessandro Pilati
Esteemed Contributor

Re: get a string in a line

if you know the position of the string use:
awk '{ print $4 }'
if you don't try, you'll never know if you are able to
idi
Occasional Contributor

Re: get a string in a line

LINE IS: 'i want to solve this problem'
and STRING IS: 'solve'
so how can i get these string?

note: i just want to get the string 'solve'
i dont want to get line (grep command is not suitable for this question)
Muthukumar_5
Honored Contributor

Re: get a string in a line

You can try as,

echo "i want to solve this problem" | sed '/solve/!d;s/^.*solve.*/solve/'

or

echo "i want to solve this problem" | grep 'solve' | sed '/solve/!d;s/^.*solve.*/solve/'

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: get a string in a line

It is easy that. You have to search the line with solve string and change into 'solve' that is all. Do it with awk as,

echo "i want to solve this problem" | awk '/solve/ { print "solve";}'

hth.
Easy to suggest when don't know about the problem!
Leif Halvarsson_2
Honored Contributor

Re: get a string in a line

Hi,
If you want to search for strings in plain text files you can use "grep -q".


-q (Quiet) Do not write anything to the standard
output, regardless of matching lines. Exit
with zero status upon finding the first
matching line. Overrides any options that
would produce output.

Then you can use an "if" condition to print out anything you want depending on hit/non hit.
Giacomo TOTTI
Advisor

Re: get a string in a line

OK,
if the word "solve" is always on the fourth position, than:

grep solve file| awk '{print $4}'

regards,

Giacomo
Alessandro Pilati
Esteemed Contributor

Re: get a string in a line

Idi,
echo "i want to solve this problem" | awk '{ print $4 }'
if you don't try, you'll never know if you are able to
idi
Occasional Contributor

Re: get a string in a line

Thank you so much all you, But actually my real problem is, I have a text which i gave 'attachment'

and firstly i want to find 'May 13 2003' string in this text and after i want to clear the lines which, dates are before 'May 13 2003'

Thank you again so much
john korterman
Honored Contributor

Re: get a string in a line

Hi,
try this slightly reused awk statement:

# awk '/May 13/,/\$/' orgfile > newfile

and then
# diff orgfile newfile
to check if it is ok.

regards,
John K.
it would be nice if you always got a second chance
Muthukumar_5
Honored Contributor

Re: get a string in a line

You can try as,

## CODE ###
FILENAME=testfile
LINE=$(grep -n 'May 13.*2003' $FILENAME | cut -d':' -f1)
tail -f +${LINE} $FILENAME
##

hth.
Easy to suggest when don't know about the problem!
David Child_1
Honored Contributor

Re: get a string in a line

This isn't going to be an easy one-liner. You need to pull the time stamps, then compare. First off, the lines do not all have the same number of columns so try the following to parse the timestamps;

awk '{ print $(NF-3),$(NF-2),$(NF-1),$NF }' file

Once you have the timestamps you will need to do some coding where you compare the year, then month, then day.

David
H.Merijn Brand (procura
Honored Contributor

Re: get a string in a line

Use grep -w -c

lt09:/home/merijn 102 > grep -c -w merijn /etc/passwd
1
lt09:/home/merijn 103 > grep -c -w merlijn /etc/passwd
0
lt09:/home/merijn 104 >

Since you want returned something you know, it doesn't matter if you get that word or "1"

If you *do* mind, try this:

sh-3.00$ var=merijn
sh-3.00$ grep -q -w $var /etc/passwd && echo $var
merijn
sh-3.00$ var=merlijn
sh-3.00$ grep -q -w $var /etc/passwd && echo $var
sh-3.00$

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn