Operating System - HP-UX
1831131 Members
2629 Online
110020 Solutions
New Discussion

Search for a particular string using SED

 
SOLVED
Go to solution

Search for a particular string using SED

Hi All,

IS there a way out to search for strings in a file using sed.

Thanks in adv,
Raja.B
17 REPLIES 17
Sundar_7
Honored Contributor

Re: Search for a particular string using SED

Is there any reason why you dont want to do string search using grep ?

sed -n '/string/p' inputfile

The above sed will only display the lines that contains "string" (like grep)
Learn What to do ,How to do and more importantly When to do ?

Re: Search for a particular string using SED

Hi,

I am actually trying to capture the time / date a particular script has executed via cron.teh log file for cron (/var/adm/cron/log) is a very huge file and grep is slow and also the script has a absolute path to it

eg: /opt/lbin/..../scriptname and grep "/opt/lbin/..../scriptname" is not giving me any output.I even tried the grep -x option also.

Hope this answaer your question.

Thanks,
Raja.B
Rodney Hills
Honored Contributor

Re: Search for a particular string using SED

Have you visually verified that that path actually exist in the log file as you specified?

Could you show a snippet of the log file that shows that path and the precise grep command you tried to use...

-- Rod Hills
There be dragons...
Geoff Wild
Honored Contributor

Re: Search for a particular string using SED

Here's the sed one liners page - might be usefullfor you:

http://www.student.northpark.edu/pemente/sed/sed1line.txt

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.
Hein van den Heuvel
Honored Contributor

Re: Search for a particular string using SED


It is hard to believe that sed woudl be any faster than grep. Also, do you not want the line before the cmd to get the date? In that case an 'awk' or 'perl' solution is probably called for. Do a 'search'-'more options' here for several examples.
If you still can not figure it out, then please re-reply with a text file containing the excact string you look for and a paragraph or two from the cron log that contain an examle that should match.

hth,
Hein.
Muthukumar_5
Honored Contributor

Re: Search for a particular string using SED

eg: /opt/lbin/..../scriptname and grep "/opt/lbin/..../scriptname" is not giving me any output.I even tried the grep -x option also. says,

Are you trying with grep filename there.?

Can you post some lines of log files and what do you want to get it from there?

Grep will do the requirement, but performance will be differed with sed / perl / awk there.

Do post your log file and informations to be extracted from that file.

HTH.
Easy to suggest when don't know about the problem!

Re: Search for a particular string using SED

Hi All,

SAMPLE LOG FILE

CMD: /opt/omni/sbin/omnitrig
> root 2571 c Thu Jun 3 12:15:00 MDT 2004
> CMD: /opt/osit/xxxx/sysdowntime/bin/start.sh >/dev/null 2>&1
> root 2572 c Thu Jun 3 12:15:01 MDT 2004
< root 2572 c Thu Jun 3 12:15:01 MDT 2004

I need to grep both the line starting with CMD: and the next line too.

Thanks,
Raja.B
Bharat Katkar
Honored Contributor

Re: Search for a particular string using SED

HI Raja,
Is this what u r looking for:bash-2.05b

# cat abc
CMD: /opt/omni/sbin/omnitrig
> root 2571 c Thu Jun 3 12:15:00 MDT 2004
> CMD: /opt/osit/xxxx/sysdowntime/bin/start.sh >/dev/null 2>&1
> root 2572 c Thu Jun 3 12:15:01 MDT 2004
< root 2572 c Thu Jun 3 12:15:01 MDT 2004

# cat abc | egrep "CMD|root"
CMD: /opt/omni/sbin/omnitrig
> root 2571 c Thu Jun 3 12:15:00 MDT 2004
> CMD: /opt/osit/xxxx/sysdowntime/bin/start.sh >/dev/null 2>&1
> root 2572 c Thu Jun 3 12:15:01 MDT 2004
< root 2572 c Thu Jun 3 12:15:01 MDT 2004

#

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Sundar_7
Honored Contributor

Re: Search for a particular string using SED

Raja,

You can try something like this.

PID=2571
awk -v CID=$PID '$3==CID && $1==">" {print prevline;} {prevline=$0} $3==CID {print}' /var/adm/cron/log

The above awk will only print the command that forked the process with PID 2571.

- Sundar.
Learn What to do ,How to do and more importantly When to do ?
Muthukumar_5
Honored Contributor

Re: Search for a particular string using SED

To get CMD: line and next line from the log file then,

# cat > testfile
CMD: /opt/omni/sbin/omnitrig
> root 2571 c Thu Jun 3 12:15:00 MDT 2004
> CMD: /opt/osit/xxxx/sysdowntime/bin/start.sh >/dev/null 2>&1
> root 2572 c Thu Jun 3 12:15:01 MDT 2004
< root 2572 c Thu Jun 3 12:15:01 MDT 2004

# awk '/CMD:/ { print $0; getline; print $0 }' testfile
CMD: /opt/omni/sbin/omnitrig
> root 2571 c Thu Jun 3 12:15:00 MDT 2004
> CMD: /opt/osit/xxxx/sysdowntime/bin/start.sh >/dev/null 2>&1
> root 2572 c Thu Jun 3 12:15:01 MDT 2004

If you want to get appropriate then post your requirement more.

HTH.
Easy to suggest when don't know about the problem!

Re: Search for a particular string using SED

Hi Bharat,

Thans for the command , but
cat filename | egrep "CMD:root" is giving me all the lines in the file that contain the string CMD and root.I am actually looking for some specifiic lines from the file .As the file is a cronlog , a lot of other scripts are scheduled via cron .I need to get the output of :
CMD : root:
This is basically to find out when all that particular script has got triggered via cron from the cron log.

Hope I have not confused you :-)

Thanks,
Raja.B
Sridhar Bhaskarla
Honored Contributor

Re: Search for a particular string using SED

Hi Raja,

Try Muthukumar's solution.

awk '/CMD: \/opt\/osit\/xxxx\/sysdowntime\/bin\/start.sh/ {print $0;getline;print $0}' /var/adm/cron/log

Note that all /s are to be escape(\)d.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bharat Katkar
Honored Contributor

Re: Search for a particular string using SED

Hi there,
How about this one.

# cat abc | egrep "CMD|root" | grep -v "^<"

I know it's funny but hope that should work. :))

Regards,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: Search for a particular string using SED

Don't try to use as,

/CMD: \/opt\/osit\/xxxx\/sysdowntime\/bin\/start.sh/

It will need to check full pattern there.

If you want to get CMD: and it's next line then try as,

awk '/CMD:/ { print $0; getline; print $0 }'


You can use another way as,

sed '/CMD:/!d;N' testfile

It will give the same requirement again using sed.

HTH.


Easy to suggest when don't know about the problem!

Re: Search for a particular string using SED

Hi Sridhar,

Muthukumar's solution worked for me .

awk '/CMD: \/opt\/omni\/lbin\/GetMetrics.pl/ {print $0;getline;print $0}' /var/adm/cron/log


But, when i try to run teh same command across servers by using remsh , awk is throwing this error

syntax error The source line is 1.
The error context is
>>> /CMD: <<<
awk: Quitting
The source line is 1.
sh} /var/adm/cron/log
sh: getline: not found.

To be honest I have no much knowledge of awk :-)

Any advice.

Thanks,
Raja.B

Sridhar Bhaskarla
Honored Contributor
Solution

Re: Search for a particular string using SED

Hi Raja,

With 'remsh' it can get little trickier because shell will try to interfere before remsh actually passes the command. So, one way is to escape the escape charater. For ex.,

remsh remote_host -n "awk '/CMD: \\/opt\\/omni\\/lbin\\/GetMetrics.pl/ {print $0;getline;print \$0}' /var/adm/cron/log"

Or put the awk command in a oneline script and push the script using 'rcp' and then execute that script using 'remsh' in the next step.

Muthukumar - I believe he is actually trying to get details for only selective cronjobs. With simple 'CMD' string, he will get all the jobs.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try

Re: Search for a particular string using SED

Hi Sridhar / Muthukumar,

Thanks a lot for all your help.

I could get the output that I was looking for.

Thanks,
Raja.B