473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

perl syntax for using awk

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
15 6278
KevinADC
4,059 Expert 2GB
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
jyo123
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
410 Expert 256MB
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
410 Expert 256MB
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
jyo123
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
410 Expert 256MB
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
jyo123
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
4,059 Expert 2GB
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
jyo123
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
4,059 Expert 2GB
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
jyo123
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
4,059 Expert 2GB
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
jyo123
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
4,059 Expert 2GB
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
jyo123
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

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

Similar topics

13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
4
by: Xah Lee | last post by:
while programing in Python, one can lookup syntax or info for keywords or modules within Python. In the command line, type python to get into the python interactive program. then type...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
25
by: rbt | last post by:
Are there any plans in the near future to support PDF files in Python as thoroughly and completely as Perl does? http://cpan.uwinnipeg.ca/search?query=pdf&mode=dist I love Python's clean...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
6
by: Jay | last post by:
hi people i am trying to connect to ppstgresql on a debian system using perl . i am getting the following error .can any one please suggest what has to be done install_driver(pg) failed:...
9
by: peter | last post by:
Hi, this is not stinky bait. If you take it that way, please dont respond.. I have been away from UNIX software for quite awhile and want to get back into it. I liked "C" but Java seems like...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
10
by: masinick | last post by:
I am aware that Perl has a lot of features that originally came from sed and awk. I have a pattern that I am using like this: sed -n '/|Y|/p' I want to do the same thing in Perl and be able to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.