- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: grep for a string and redirect all lines after...
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
Discussions
Discussions
Forums
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
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
тАО11-25-2009 04:16 AM
тАО11-25-2009 04:16 AM
grep for a string and redirect all lines after that occurence to another file
I have a problem. I need to find a particular string in a file and display all lines coming after the occurence of the string.
Is that possible and how?
Thanks in advance,
Goutham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-25-2009 04:43 AM
тАО11-25-2009 04:43 AM
Re: grep for a string and redirect all lines after that occurence to another file
This can easily be done in 'awk' or Perl. By example we could look for the string "local" in an '/etc/hosts':
# awk '/local/ {X=1};{if (X) {print}}' /etc/hosts
In Perl we can do the same and add case-insensitive matching like:
# perl -ne '/LoCaL/i and $X=1;print if $X' /etc/hosts
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-25-2009 02:53 PM
тАО11-25-2009 02:53 PM
Re: grep for a string and redirect all lines after that occurence to another file
sed -n -e '/string/,$p' file > file2
(This includes the line with "string".)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-25-2009 04:59 PM
тАО11-25-2009 04:59 PM
Re: grep for a string and redirect all lines after that occurence to another file
If you want to _exclude_ the line with the matching string, but print every line beyond to the file's end, you could do:
# perl -ne '/LoCaL/i and {$x=1,next};print if $x' /etc/hosts
This case-insensitively matches the string "local"; skips printing that line; and prints every line to the file's end.
As always, if you want to redirect the output from your terminal to a file, simply do:
# perl -ne '/LoCaL/i and {$x=1,next};print if $x' /etc/hosts > /tmp/myoutput
Analogous to 'sed' or 'awk', Perl offers another way to match a pattern and print from the line with the pattern to the end of a file:
# perl -ne 'print if /LoCaL/i..eof' /etc/hosts > /tmp/myoutput
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-25-2009 08:47 PM
тАО11-25-2009 08:47 PM
Re: grep for a string and redirect all lines after that occurence to another file
Regards,
Goutham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-25-2009 08:48 PM
тАО11-25-2009 08:48 PM