I want to take the value of certain column of a string in perl
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I take the value of a row from a txt file and save this in a variable var. I want to take a row of this variable.
#!/usr/bin/perl
use strict;
use warnings;
my $search="/dev/sda1";
my $read_file="Salidaprueba.txt";
my $write_file = "/home/alvaro.andradec/Alvaro.txt";
open (my $WRITE,">> $write_file") or die "can not open the file $write_file";
open(my $READ, $read_file);
while(my $line = <$READ>) {
if ($line =~ /$search/) {
my $var="$line";
chomp($var);
print ("$var\n");
}
}
close $WRITE;
exit;
what the var variable saves is /dev/sda1 4.7G 4.3G 202M 96% /
It has been difficult for me to understand how I take the value and not reach to test the code of a person who left it in this post and then the hidden.
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
When adding a comment as in your previous posts, please use the Edit button at bottom your posts to make the addition rather than re-posting the entire previous content. This will make it easier for others to follow the thread and understand what you are asking more clearly.
I have read this thread several times and am still not sure that I understand what your question actually is.
What do you mean when you say you want to take the value of a row, and what exactly do you want to do with that value.
If you would post a clearly identified example of the expected input, and a similar clearly stated example of what you expect the output to look like, perhaps we can see clearly what you are trying to accomplish.
My perl skills are rusty so I will defer comment on your code syntax and structure to others.
Last edited by astrogeek; 09-14-2018 at 04:13 PM.
Reason: new typos for old
I take the value of a row from a txt file and save this in the variable var. I want to take a row of this string, where is the number 96.
SNIPPED
I did not understand very well the syntax of regex.
Thanks.
Ok, here is an example for you.
Code:
#!/usr/bin/perl
use strict;
use warnings;
# you can group multiple constants together, since constants are represented
# as a hash.
use constant {
IN_FILE => 'your_file.txt',
OUT_FILE => 'your_out_file.txt' # you don't seem to be using this.
};
my $search_term = '/dev/sda1';
# please use the three part open.
open my $in_file, '<', IN_FILE or die $!;
chomp(my @lines = <$in_file>);
for my $line (@lines) {
# using index to find a fixed substring in a string.
if ( index( $line, $search_term ) >= 0 ) {
print "$line\n";
last;
}
# using a regular expression. note: please don't use this for fixed patterns.
if ( $line =~ /$search_term/ ) {
print "$line\n";
last;
}
}
close $in_file;
Last edited by individual; 09-14-2018 at 04:27 PM.
Reason: Updated to show how to use a regular expression.
#!/usr/bin/perl
use strict;
use warnings;
# you can group multiple constants together, since constants are represented
# as a hash.
use constant {
IN_FILE => 'your_file.txt',
OUT_FILE => 'your_out_file.txt' # you don't seem to be using this.
};
my $search_term = '/dev/sda1';
# please use the three part open.
open my $in_file, '<', IN_FILE or die $!;
chomp(my @lines = <$in_file>);
for my $line (@lines) {
# using index to find a fixed substring in a string.
if ( index( $line, $search_term ) >= 0 ) {
print "$line\n";
last;
}
# using a regular expression. note: please don't use this for fixed patterns.
if ( $line =~ /$search_term/ ) {
print "$line\n";
last;
}
}
close $in_file;
your script does the same as mine, take the row where the expression is / dev / sda1 4.7G 4.3G 202M 96% /
Now I need to take the number that always accompanies the% sign, since with this is that I can make some calculations of the percentage of free space on a disk
your script does the same as mine, take the row where the expression is / dev / sda1 4.7G 4.3G 202M 96% /
Now I need to take the number that always accompanies the% sign, since with this is that I can make some calculations of the percentage of free space on a disk
How do you think you might accomplish that? Any ideas?
your script does the same as mine, take the row where the expression is / dev / sda1 4.7G 4.3G 202M 96% /
Now I need to take the number that always accompanies the% sign, since with this is that I can make some calculations of the percentage of free space on a disk
You weren't clear about what you wanted.
Code:
#!/usr/bin/perl
use strict;
use warnings;
# you can group multiple constants together, since constants are represented
# as a hash.
use constant {
IN_FILE => 'your_file.txt',
OUT_FILE => 'your_out_file.txt' # you don't seem to be using this.
};
my $search_term = '/dev/sda1';
# please use the three part open.
open my $in_file, '<', IN_FILE or die $!;
chomp(my @lines = <$in_file>);
for my $line (@lines) {
# using index to find a fixed substring in a string.
if (index($line, $search_term) >= 0) {
for my $col (split ' ', $line) {
if (rindex($col, '%') >= 0) {
print $col;
last;
}
}
last;
}
# using a regular expression. note: please don't use this for fixed patterns.
if ($line =~ /$search_term/) {
print $line =~ /(\d+%)/;
last;
}
}
close $in_file;
#!/usr/bin/perl
use strict;
use warnings;
# you can group multiple constants together, since constants are represented
# as a hash.
use constant {
IN_FILE => 'your_file.txt',
OUT_FILE => 'your_out_file.txt' # you don't seem to be using this.
};
my $search_term = '/dev/sda1';
# please use the three part open.
open my $in_file, '<', IN_FILE or die $!;
chomp(my @lines = <$in_file>);
for my $line (@lines) {
# using index to find a fixed substring in a string.
if (index($line, $search_term) >= 0) {
for my $col (split ' ', $line) {
if (rindex($col, '%') >= 0) {
print $col;
last;
}
}
last;
}
# using a regular expression. note: please don't use this for fixed patterns.
if ($line =~ /$search_term/) {
print $line =~ /(\d+%)/;
last;
}
}
close $in_file;
thank you very much, working with bash I got this easily but with perl it has been difficult for me. If you have books or tutorials that you can provide I would appreciate it. the script worked perfect.
thank you very much, working with bash I got this easily but with perl it has been difficult for me. If you have books or tutorials that you can provide I would appreciate it. the script worked perfect.
You're welcome! Yes it can be confusing, but like anything else practice makes it easier. I'd recommend first typing
Code:
perldoc perlintro
in your terminal. If you want to use Perl on the regular, I'd recommened investing in Programming Perl.
EDIT: I forgot to mention, you don't need both versions of selecting the substring in your code. Choose one you like (and that works for you) and remove the other.
Last edited by individual; 09-14-2018 at 05:02 PM.
You're realy close with $line =~ /$search/ and you can do something like what you want by replacing my $var="$line"; with a similar pattern that is also using a capturing group within it. The specific manual page would be perlre Although that is a reference and not a tutorial it will help to keep checking it, perl patterns in the form of PCRE are used everywhere. Eventually it will become familiar.
Code is read many more times than it is written so the investement needs to be in clear, concise writting. If no one else you are writing to your future self.
Maybe store % values in an array if you want to process data later
Code:
#!/usr/bin/perl
my ($file, @found, $item);
open $file, '<', 'file.txt' or die $!;
@found = grep { /(\d+%)/ and $_ = $1 } <$file>;
close $file;
# @found contains all % values
print 'item '.(++$item).": $_\n" for @found;
Edit: for dealing with df style lines, I think I would rather use split to keep what is associated with the value
Code:
#!/usr/bin/perl
my ($file, %found);
open $file, '<', 'file.txt' or die $!;
while (<$file>) {
next unless /\d+%/;
my ($disk, $used) = (split ' ')[0,4];
$found{$disk} = $used;
}
close $file;
# %found hash contains all disk / used values pair
print "$_: $found{$_}\n" for sort keys %found;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.