By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

perl syntax for using awk

P: 12
Hi everyone,
I have a file input.txt which has contents like this:
1 1132 aaaa vvvv
2 1123 bbbb 222
3 1121 ssad 323
4 2222 asda 232

When i was scripting in shell i used awk to pick 2nd row 2column value i.e 1123
awk 'NR=='$2' {print $2}' input

Now i need same thing to be done using perl...please anyone can suggest how to write this is perl..

thanks in advance...:-)
Aug 6 '08 #1
Share this Question
Share on Google+
15 Replies


KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. perl -naF/\s/ -e '$.==2 && print $F[1]' input
options:

n - only prints the lines you tell it to
a - autosplit mode into @F
F - pattern to split on in this case a regexp ("/\s/" spilt on a space)
e - evaluate whats in quotes as code

code:

$. is the input record line number
$F[1] is the second field (or column) of @F (perl uses zero based arrays)
Aug 6 '08 #2

P: 12
Thank you very much...
But when it is executed following error message is seen.Please can u tell me where i am doing wrong.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. $value = `perl -naF/\s/ -e '$.==2 && print $F[1]' input.txt`;
  3. print "$value";
Error message :
Expand|Select|Wrap|Line Numbers
  1. Unrecognized escape \s passed through at Untitled2 line 2.
  2. Name "main::F" used only once: possible typo at Untitled2 line 2.
  3. Use of uninitialized value $. in concatenation (.) or string at Untitled2 line 2.
  4. Use of uninitialized value in concatenation (.) or string at Untitled2 line 2.
  5. Can't find string terminator "'" anywhere before EOF at -e line 1.
.
Aug 6 '08 #3

nithinpes
Expert 100+
P: 410
What Kevin has posted is a perl one-liner which you can run directly in the terminal as you do with your awk command. No need of the script file.
All you need to do is type the entire command in terminal and press 'ENTER'.
Aug 6 '08 #4

nithinpes
Expert 100+
P: 410
What Kevin has posted is a perl one-liner which you can run directly in the terminal as you do with your awk command. No need of the script file.
All you need to do is type the entire command in terminal and press 'ENTER'.

However, if you want to use it inside script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. $command='perl -naF/\s/ -e "$.==2 && print $F[1]" input.txt';
  3. $value = `$command`;
  4. print "$value";
  5.  
Aug 6 '08 #5

P: 12
However, if you want to use it inside script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. $command='perl -naF/\s/ -e "$.==2 && print $F[1]" input.txt';
  3. $value = `$command`;
  4. print "$value";
  5.  
Is there any bound on spaces like there should be only one space between each field? because when input file has value in same row but little placed apart like
1 1132 aaaa vvvv
2 1123 bbbb 222 etert
3 1121 ssad 323 hfgh
4 2222 asda 232

i am not getting values when it is placed after few spaces
Aug 6 '08 #6

nithinpes
Expert 100+
P: 410
Is there any bound on spaces like there should be only one space between each field? because when input file has value in same row but little placed apart like
1 1132 aaaa vvvv
2 1123 bbbb 222 etert
3 1121 ssad 323 hfgh
4 2222 asda 232

i am not getting values when it is placed after few spaces
use:
Expand|Select|Wrap|Line Numbers
  1. perl -naF/\s+/ -e "$.==2 && print $F[1]" input.txt
  2.  
\s+ will look for one or more spaces.

-Nithin
Aug 6 '08 #7

P: 12
use:
Expand|Select|Wrap|Line Numbers
  1. perl -naF/\s+/ -e "$.==2 && print $F[1]" input.txt
  2.  
\s+ will look for one or more spaces.

-Nithin
Is there any method to increment the input record line number automatically.
Expand|Select|Wrap|Line Numbers
  1. for($i=0;i<=5;i++)
  2. {
  3. $command='perl -naF/\s+/ -e "$.=='$i' && print $F[4]" input.txt';
  4. $value = `$command`;
  5. print "$value";
  6. }
Expand|Select|Wrap|Line Numbers
  1. 1 1132 aaaa vvvv
  2. 2 1123 bbbb 222  etert
  3. 3 1121 ssad 323 hfgh
  4. 4 2222 asda 232
I require all the values in 2nd field
Expand|Select|Wrap|Line Numbers
  1. 1132
  2. 1123
  3. 1121
  4. 2222
I am stuck up with this please anyone help me
Aug 6 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
Is there any method to increment the input record line number automatically.
for($i=0;i<=5;i++)
{
$command='perl -naF/\s+/ -e "$.=='$i' && print $F[4]" input.txt';
$value = `$command`;
print "$value";
}

1 1132 aaaa vvvv
2 1123 bbbb 222 etert
3 1121 ssad 323 hfgh
4 2222 asda 232

I require all the values in 2nd field
1132
1123
1121
2222
I am stuck up with this please anyone help me

Well, I thought you want a command line like with AWK, there is no sense in writing a one liner you run with backtiks, just do it all in the same script:

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    print +(split/\s+/)[1],"\n";
  4. }
  5. close IN;
  6.  
If you wanted all the second column values you should have said that in your first post.
Aug 6 '08 #9

P: 12
Well, I thought you want a command line like with AWK, there is no sense in writing a one liner you run with backtiks, just do it all in the same script:

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    print +(split/\s+/)[1],"\n";
  4. }
  5. close IN;
  6.  
If you wanted all the second column values you should have said that in your first post.
No actually what i needed is script has to go 1st line 1std filed, take that value ,need to do some operations like pass that value into netsnmp command then increment by itself to 2nd line and take the value in 2nd field substitute in the netsnmp command ....please this is my main intention i thought i give example of simple file and try on my own applying it to what i want..
my input file looks like this...

11713000 1.3.6.1.4.1.253.8.63.11.1100.1.2 5
11610000 1.3.6.1.4.1.253.8.63.11.1100.2.1 5
11620000 1.3.6.1.4.1.253.8.63.11.1100.2.2 5
11630000 1.3.6.1.4.1.253.8.63.11.1100.2.3 8
Aug 6 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
No actually what i needed is script has to go 1st line 1std filed, take that value ,need to do some operations like pass that value into netsnmp command then increment by itself to 2nd line and take the value in 2nd field substitute in the netsnmp command ....please this is my main intention i thought i give example of simple file and try on my own applying it to what i want..
my input file looks like this...

11713000 1.3.6.1.4.1.253.8.63.11.1100.1.2 5
11610000 1.3.6.1.4.1.253.8.63.11.1100.2.1 5
11620000 1.3.6.1.4.1.253.8.63.11.1100.2.2 5
11630000 1.3.6.1.4.1.253.8.63.11.1100.2.3 8
OK, well, try applying what you have been shown so far to whatever it is you want to do with the file data.
Aug 6 '08 #11

P: 12
OK, well, try applying what you have been shown so far to whatever it is you want to do with the file data.
Ya i tried but i am stuck up with iterating the line number please help me
Aug 6 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
You don't need to iterate the line numbers of you want to read all the lines. Look at the last code I posted, that reads all the lines of the file. If you ever need to get to one or some specific line, then $. (dollar-sign dot) holds that value.
Aug 6 '08 #13

P: 12
You don't need to iterate the line numbers of you want to read all the lines. Look at the last code I posted, that reads all the lines of the file. If you ever need to get to one or some specific line, then $. (dollar-sign dot) holds that value.
ya i got it but $.==2 is given in your code i am trying to put it in the loop i.e value given 2 ,for next iteration it should automatically increment to 3.
for($i=0;$i<=10;$i++)
{
$command='perl -naF/\s+/ -e "$.=$i && print $F[4]" comms_input';
$value = `$command`;
...
...
....
}

Is there anyway to assign variable into $.
Aug 7 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
Look at the last code I posted, you will not see any '$.' in the code. Do you want to loop through all the lines in the file?

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    print +(split/\s+/)[1],"\n";
  4. }
  5. close IN;
  6.  
The code you are trying to use is terrible because you are basing it from the one-liner I posted for you which is what I thought you wanted, but you don't. Another example:

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    my $col2 = (split/\s+/)[1];
  4.    do_something($col2);
  5. }
  6. close IN;
  7.  
  8. sub do_something {
  9.    my $col2 = $_[0];
  10.    # do something useful with $col2
  11. }
  12.  
Aug 7 '08 #15

P: 12
Look at the last code I posted, you will not see any '$.' in the code. Do you want to loop through all the lines in the file?

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    print +(split/\s+/)[1],"\n";
  4. }
  5. close IN;
  6.  
The code you are trying to use is terrible because you are basing it from the one-liner I posted for you which is what I thought you wanted, but you don't. Another example:

Expand|Select|Wrap|Line Numbers
  1. open(IN,'input.txt') or die "$!";
  2. while (<IN>) {
  3.    my $col2 = (split/\s+/)[1];
  4.    do_something($col2);
  5. }
  6. close IN;
  7.  
  8. sub do_something {
  9.    my $col2 = $_[0];
  10.    # do something useful with $col2
  11. }
  12.  
Thanks,
I tried this but i did not get what i wanted.
If my logic i requested is terrible then how it is possible to be success using awk
when i tried in awk i am getting what i wanted but i am not able to do in perl

In awk i used following code:
for((i=0;i<=5;i++))
do
Syn=`awk 'NR=='$i' {print $6}' comms_input`
echo "$Syn"
.............................................
....<opertaions using $Syn>.....
done
Aug 13 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.