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...:-)
15 6251 - 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)
Thank you very much...
But when it is executed following error message is seen.Please can u tell me where i am doing wrong. - #!/usr/bin/perl -w
-
$value = `perl -naF/\s/ -e '$.==2 && print $F[1]' input.txt`;
-
print "$value";
Error message : - Unrecognized escape \s passed through at Untitled2 line 2.
-
Name "main::F" used only once: possible typo at Untitled2 line 2.
-
Use of uninitialized value $. in concatenation (.) or string at Untitled2 line 2.
-
Use of uninitialized value in concatenation (.) or string at Untitled2 line 2.
-
Can't find string terminator "'" anywhere before EOF at -e line 1.
.
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'.
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: -
#!/usr/bin/perl -w
-
$command='perl -naF/\s/ -e "$.==2 && print $F[1]" input.txt';
-
$value = `$command`;
-
print "$value";
-
However, if you want to use it inside script: -
#!/usr/bin/perl -w
-
$command='perl -naF/\s/ -e "$.==2 && print $F[1]" input.txt';
-
$value = `$command`;
-
print "$value";
-
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
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: -
perl -naF/\s+/ -e "$.==2 && print $F[1]" input.txt
-
\s+ will look for one or more spaces.
-Nithin
use: -
perl -naF/\s+/ -e "$.==2 && print $F[1]" input.txt
-
\s+ will look for one or more spaces.
-Nithin
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
I am stuck up with this please anyone help me
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: -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
print +(split/\s+/)[1],"\n";
-
}
-
close IN;
-
If you wanted all the second column values you should have said that in your first post.
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: -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
print +(split/\s+/)[1],"\n";
-
}
-
close IN;
-
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
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.
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
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.
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 $.
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? -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
print +(split/\s+/)[1],"\n";
-
}
-
close IN;
-
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: -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
my $col2 = (split/\s+/)[1];
-
do_something($col2);
-
}
-
close IN;
-
-
sub do_something {
-
my $col2 = $_[0];
-
# do something useful with $col2
-
}
-
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? -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
print +(split/\s+/)[1],"\n";
-
}
-
close IN;
-
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: -
open(IN,'input.txt') or die "$!";
-
while (<IN>) {
-
my $col2 = (split/\s+/)[1];
-
do_something($col2);
-
}
-
close IN;
-
-
sub do_something {
-
my $col2 = $_[0];
-
# do something useful with $col2
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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:...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |