LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 14

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: 14

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<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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: 14

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
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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: 14

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
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
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 Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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



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 01:19 AM.

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
Open Source Consulting | Domain Registration