- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Perl question - cut or awk function to get fir...
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
03-23-2006 08:45 AM
03-23-2006 08:45 AM
Contains "a b c"
I want to put the first value in $var2.
What's the syntax similar to :
var2=`echo $var1 | awk ' { print $1 } '
OR
var2='echo $var1 | cut -d -f 1`
I think the split function will do it but can't remember how.
jack...
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 08:53 AM
03-23-2006 08:53 AM
Re: Perl question - cut or awk function to get first field
How about:
echo "${var1}" | read var2 dummy1 dummy2
echo "var2 = \"${var2}\""
It's all internal to the shell.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 08:59 AM
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 09:00 AM
03-23-2006 09:00 AM
Re: Perl question - cut or awk function to get first field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 09:04 AM
03-23-2006 09:04 AM
Re: Perl question - cut or awk function to get first field
my $var1 = "a b c";
my @var2 = split /\s+/,$var1
print "var2 = ",$var2[0],"\n";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 09:36 AM
03-23-2006 09:36 AM
Re: Perl question - cut or awk function to get first field
Just wondering why your own awk contruct will not work? It works for me :)
# var1="a b c"
# var2=`echo $var1 | awk '{print $1}`
# echo $var2
a
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 12:33 PM
03-23-2006 12:33 PM
Re: Perl question - cut or awk function to get first field
It's worth mentioning, since you asked about Perl in comparison to 'awk', that its easy to extract fields from files thusly:
# perl -nalF: -e 'print $F[0] if /^j/i' /etc/passwd
The above reads '/etc/passwd' and prints all logins that begin with "j" in lower or upper case.
The '-n' generates a read-loop to read the datafile. The '-a' arms automatic splitting of the records read using a field seperator defined by '-F' --- here the colon (:"). The '-l' adds a newline to every print statement. Each element split from the record is placed in the @F array which is automatically provided. Again, the first element is zero-relative.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 01:10 PM
03-23-2006 01:10 PM
Re: Perl question - cut or awk function to get first field
>> Got a variable named $var1
>> Contains "a b c"
If you are truly interested in using Perl, then you should probably take step back and see how the value got into that variable, and what will be done with $var2 once you get that. Print it? Use it as a part of a command?
Maybe using perl whilest getting $var1 tru using $var2 can solve multiple problems at once.
To put it differently...
What problem are you really trying to solve?
Surely gettting a field from a variable is just a minor sub task. What is the next level up task, and perhaps the next level up from there? Maybe there a neat all-perl solution instead of shell + perl/awk.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2006 01:27 PM
03-23-2006 01:27 PM
Re: Perl question - cut or awk function to get first field
I thought $var1 was a shell variable, but now I see it may have been a perl variable already.
The shell scripting threw me of.
Just for grins...
If it was a shell variable, the you can do something like:
$ var1="aap noot mies"
$ echo $var1 | perl -ple '$_ = (split)[0]'
aap
That works because -p implies a loop reading data into $_ and printing whatever is left in $_ at each loop itteration.
The 'split' produces an array splitting by default on whitespace and taking input from $_
And using the -a autosplit into @F gives:
$ echo $var1 | perl -pale '$_ = @F[0]'
aap
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2006 01:39 AM
03-24-2006 01:39 AM
Re: Perl question - cut or awk function to get first field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2006 01:54 AM
03-24-2006 01:54 AM
Re: Perl question - cut or awk function to get first field
I just needed the corresponding perl sytax to capture the first value and then place that value into a variable that I can use later in the perl script.
I used the shell syntax in the original post in an attempt to show what I was wanting to do in perl. Looking for perl equivalent syntax.
Jack...