Operating System - Linux
1827859 Members
2819 Online
109969 Solutions
New Discussion

Re: Perl question - cut or awk function to get first field

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

Perl question - cut or awk function to get first field

Got a variable named $var1
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...




10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

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.


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl question - cut or awk function to get first field

Hi Jack:

# perl -le '$x="a b c";@fields=split(/\s+/,$x);print $fields[0]'

...splits on whitespace, for instance. Fields in erl are 0-relative, so the first one is [0].

Regards!

...JRF...
Jack C. Mahaffey
Super Advisor

Re: Perl question - cut or awk function to get first field

Thanks...
A. Clay Stephenson
Acclaimed Contributor

Re: Perl question - cut or awk function to get first field

You asked in Perl but your syntax is so bad for Perl that I assumed that this was a shell question but if it's Perl then something like this:

my $var1 = "a b c";
my @var2 = split /\s+/,$var1
print "var2 = ",$var2[0],"\n";
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Perl question - cut or awk function to get first field

Jack,

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!
James R. Ferguson
Acclaimed Contributor

Re: Perl question - cut or awk function to get first field

Hi (again) Jack:

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...

Hein van den Heuvel
Honored Contributor

Re: Perl question - cut or awk function to get first field

JRF... I was going to write that :-). Thanks.

>> 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.

Hein van den Heuvel
Honored Contributor

Re: Perl question - cut or awk function to get first field

Oops, ignore my prior reply.
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.
Jack C. Mahaffey
Super Advisor

Re: Perl question - cut or awk function to get first field

Clay, you are correct. I provided shell syntax and wanted the equivalent syntax for perl.
Jack C. Mahaffey
Super Advisor

Re: Perl question - cut or awk function to get first field

Thanks all for your feedback. My bad for not being very clear in my post.

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...