LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2018, 03:08 PM   #16
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled

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.
 
Old 09-14-2018, 03:14 PM   #17
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.


#!/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;




I did not understand very well the syntax of regex.
Thanks.
 
Old 09-14-2018, 04:08 PM   #18
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,764
Blog Entries: 6

Rep: Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585Reputation: 2585
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
 
Old 09-14-2018, 04:20 PM   #19
individual
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 96

Rep: Reputation: 68
Quote:
Originally Posted by andres_fever View Post
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.
 
Old 09-14-2018, 04:40 PM   #20
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by individual View Post
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;

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
 
Old 09-14-2018, 04:42 PM   #21
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,763

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Quote:
Originally Posted by andres_fever View Post
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?
 
Old 09-14-2018, 04:44 PM   #22
individual
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 96

Rep: Reputation: 68
Quote:
Originally Posted by andres_fever View Post
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;
 
2 members found this post helpful.
Old 09-14-2018, 04:55 PM   #23
andres_fever
LQ Newbie
 
Registered: Sep 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by individual View Post
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;

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.
 
Old 09-14-2018, 04:59 PM   #24
individual
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 96

Rep: Reputation: 68
Quote:
Originally Posted by andres_fever View Post
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.
 
Old 09-14-2018, 11:55 PM   #25
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,437
Blog Entries: 3

Rep: Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517
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.

I do see lack of simple, to the point examples in the available tutorials however.
Try this one: http://modernperlbooks.com/books/mod...mberedcaptures
That will give you a two-line solution.

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.
 
Old 09-15-2018, 08:25 AM   #26
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,345

Rep: Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414
you might want to check this: https://perlmaven.com/perl-split
 
Old 09-16-2018, 02:49 PM   #27
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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;

Last edited by keefaz; 09-16-2018 at 03:12 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to take the value of certain column of a string andres_fever Linux - Newbie 2 09-12-2018 02:30 PM
If the 1th column of file f1 and file f2 is the same, then export those line with maximum string of 2nd column weichanghe2000 Programming 1 04-26-2018 12:42 PM
print column with a string using awk or grep bison72 Linux - Newbie 2 01-21-2015 10:31 PM
[SOLVED] How to split single column string into two columns Colorinb Linux - Newbie 2 10-07-2011 09:06 PM
[SOLVED] search a string and print column vikas027 Programming 6 03-14-2010 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration