- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Search for a particular string using SED
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 12:08 PM
10-19-2004 12:08 PM
IS there a way out to search for strings in a file using sed.
Thanks in adv,
Raja.B
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 12:17 PM
10-19-2004 12:17 PM
Re: Search for a particular string using SED
sed -n '/string/p' inputfile
The above sed will only display the lines that contains "string" (like grep)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 12:21 PM
10-19-2004 12:21 PM
Re: Search for a particular string using SED
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 01:12 PM
10-19-2004 01:12 PM
Re: Search for a particular string using SED
Could you show a snippet of the log file that shows that path and the precise grep command you tried to use...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 02:33 PM
10-19-2004 02:33 PM
Re: Search for a particular string using SED
http://www.student.northpark.edu/pemente/sed/sed1line.txt
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 02:49 PM
10-19-2004 02:49 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2004 05:36 PM
10-19-2004 05:36 PM
Re: Search for a particular string using SED
Are you trying with grep
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 04:29 AM
10-20-2004 04:29 AM
Re: Search for a particular string using SED
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 04:37 AM
10-20-2004 04:37 AM
Re: Search for a particular string using SED
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 04:43 AM
10-20-2004 04:43 AM
Re: Search for a particular string using SED
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 05:17 AM
10-20-2004 05:17 AM
Re: Search for a particular string using SED
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 09:25 AM
10-20-2004 09:25 AM
Re: Search for a particular string using SED
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 :
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 10:23 AM
10-20-2004 10:23 AM
Re: Search for a particular string using SED
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 09:22 PM
10-20-2004 09:22 PM
Re: Search for a particular string using SED
How about this one.
# cat abc | egrep "CMD|root" | grep -v "^<"
I know it's funny but hope that should work. :))
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2004 11:02 PM
10-20-2004 11:02 PM
Re: Search for a particular string using SED
/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2004 05:15 AM
10-21-2004 05:15 AM
Re: Search for a particular string using SED
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2004 05:27 AM
10-21-2004 05:27 AM
SolutionWith '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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2004 07:50 AM
10-21-2004 07:50 AM
Re: Search for a particular string using SED
Thanks a lot for all your help.
I could get the output that I was looking for.
Thanks,
Raja.B