- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Extracting field using perl
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
тАО12-14-2006 01:04 PM
тАО12-14-2006 01:04 PM
I will like to get the field from a line of string, using perl.
say the string is this:
"Process_name core kernel " and i will like to extract "core kernel from it" what is the best way to do this? Thanks in advance.
Best regards
Henry
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 01:11 PM
тАО12-14-2006 01:11 PM
Re: Extracting field using perl
# echo "Process_name core kernel"|perl -nle 'print $1 if m/(core kernel)/'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 01:18 PM
тАО12-14-2006 01:18 PM
Re: Extracting field using perl
But what if I want to print the field after the first field i.e. for:
Process_name data 1
This will print "data 1"
Thanks.
Best regards
Henry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 01:30 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 04:03 PM
тАО12-14-2006 04:03 PM
Re: Extracting field using perl
If 'Process_name' has to be the first field on the line, then I would recommend indicating that with the BOL 'anchor' : "^"
That make the match quicker, and prevents false matched in case the string appears somewhere else. No need to save 'process_name' in $1 through () either.
So that would give:
echo "Process_name data 1"|perl -ne 'print $1 if m/^Process_name\s+(.+)/'
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 05:00 PM
тАО12-14-2006 05:00 PM
Re: Extracting field using perl
If I am reading lines from a file using perl.. how does the above applies to this?
Regards
Henry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-16-2006 01:31 AM
тАО12-16-2006 01:31 AM
Re: Extracting field using perl
We used the 'echo' for easy one-line demo.
With a file you probably also need to
- loop
- remove the new-line
- decide whether you want a first occurence, a last or all.
For example:
perl -ne 'chomp; if (/^Process_name\s+(.+)/) {print $1; last}'
The -n requests perl to create a loop, Not printing each line. -p will loop and Print.
The chomp is not needed for a simple display, but probably is if you are sticking the result in a variable.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-16-2006 03:29 AM
тАО12-16-2006 03:29 AM
Re: Extracting field using perl
To add to Hein's explanation a bit, consider this.
Using the '-n' or '-p' switch tells Perl to create a loop around your code, here, the argument of the '-e' switch enclosed in single quotes. Thus, instead of:
# perl -nle 'chomp;print $1 if m/^Process_name\s+(.+)/' filename
I could have written:
# cat .findpat
#/usr/bin/perl
while (<>) {
chomp;
print "$1\n" if if m/^Process_name\s+(.+)/'
}
# ./findpat filename
I used the '-l' switch on the commandline to cause print statements to have a newline character appended automatically to the output.
The read using "<>" is Perl's way of saying open the filename found in $ARGV and read it. If no @ARGV arguments are supplied, STDIN is used. Thus, using 'echo" piped to our Perl program is merely our Perl program reading STDIN. In every case, the read loop runs until there is no more input. A one-line 'echo' piped to our program is nothing more than a read of a file with one line.
If you want to read three (3) lines with a piped 'echo' you could do something as degenerate as:
# echo "line1\nline2\nline3"| perl -ple 'chomp'
Regards!
...JRF...