473,752 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

perl syntax for using awk

12 New Member
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 6312
KevinADC
4,059 Recognized Expert Specialist
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 New Member
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Specialist
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 New Member
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

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

Similar topics

13
2708
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 not that different. I did like the indent-as-group idea, which was different. Ruby looked very cool. But it was impossible to get good documentation. It seemed like a Japanese cult with a few western initiates. Well, MacOS X ships with Perl,...
77
4057
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 developing serious application, e.g web application as it has not many features in it yet. I've given up on Perl for its ugly syntax and it is not the easiest language to learn. How about PHP? Thanks
4
1470
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 help() >From there one can type any keyword or module name to find out the
31
4801
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 great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
25
2442
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 syntax and ease of use, etc. But on some things (PDF for example) as barbaric as Perl's syntax is, it does outshine Python... I hate having to use Perl just to deal with PDF files. What do others do???
20
4072
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: li=;
6
4858
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: Can't locate DBD/pg.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.3 /usr/local/share/perl/5.8.3 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at
9
1788
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 the way to go for compiled langs, now days so I will learn that. At one time, I did a bit of perl but now I see php alot. I was wondering what you guys thinks of the pros/cons of perl and php.
1
7189
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
10
2248
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 either save that value in some kind of variable or array or potentially write it out to a file. For the simple case, writing out to a file, I think the syntax is very close to the sed syntax. I would like to get a few recommendations, first, on a...
0
9011
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9597
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9414
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8270
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6090
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4722
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4900
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2232
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.