michapdm
asked on
Perl How I filter Value of File and write them ordered in a new file
I have a textfile. its look similar like this
#------------------------- ---------- --------
cd /home/merkur/Stationsnetz- Config-Web interface
mkdir St.Margrethen
cd St.Margrethen
wget -cmkr http://172.16.x.a
#------------------------- ---------- --------
cd /home/merkur/Stationsnetz- Config-Web interface
mkdir Hoechst
cd Hoechst
wget -cmkr http://172.16.x.b
#------------------------- ---------- --------
cd /home/merkur/Stationsnetz- Config-Web interface
mkdir Buriet
cd Buriet
wget -cmkr http://172.16.x.c
#------------------------- ---------- --------
cd /home/merkur/Stationsnetz- Config-Web interface
mkdir Seeriet
cd Seeriet
wget -cmkr http://172.16.x.d
#------------------------- ---------- --------
now I will use perl what write me a new file
what look like this
St.Margrethen 172.16.x.a
Hoechst 172.16.x.b
Buriet 172.16.x.c
Seeriet 172.16.x.d
I get Errors because of the slashes, can somebody help me
#-------------------------
cd /home/merkur/Stationsnetz-
mkdir St.Margrethen
cd St.Margrethen
wget -cmkr http://172.16.x.a
#-------------------------
cd /home/merkur/Stationsnetz-
mkdir Hoechst
cd Hoechst
wget -cmkr http://172.16.x.b
#-------------------------
cd /home/merkur/Stationsnetz-
mkdir Buriet
cd Buriet
wget -cmkr http://172.16.x.c
#-------------------------
cd /home/merkur/Stationsnetz-
mkdir Seeriet
cd Seeriet
wget -cmkr http://172.16.x.d
#-------------------------
now I will use perl what write me a new file
what look like this
St.Margrethen 172.16.x.a
Hoechst 172.16.x.b
Buriet 172.16.x.c
Seeriet 172.16.x.d
I get Errors because of the slashes, can somebody help me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
perl -ne 'print for m{mkdir\s*(.*)},m{wget -cmkr(\s+)http://(.*)}s' datafile
ASKER
this Command
perl -ne 'print for m{mkdir\s*(.*)},m{wget -cmkr(\s+)http://(.*)}s' datafile > newFile
is working thanks
but the example above is not working, plaese can you correct my comments.
#!/usr/bin/perl
my $file = save-config-script.sh; #File gets a real File
my $line = $ARGV[0]; #variable line becomes an array
local $/; # all local variables become change abilitiy
open (IN, $file); #opens file for read
my $line = <IN>; #????
@matches = $line =~ m {cd\s+ ([\w.]+) [\n\r]wget.*?http://(.+)}g; #????
while( ($x,$y, @matches) = @matches) #Funktion for all Values
{
print "$x $y\n"; #writing all values
}
and how I put the values inthe new file
perl -ne 'print for m{mkdir\s*(.*)},m{wget -cmkr(\s+)http://(.*)}s' datafile > newFile
is working thanks
but the example above is not working, plaese can you correct my comments.
#!/usr/bin/perl
my $file = save-config-script.sh; #File gets a real File
my $line = $ARGV[0]; #variable line becomes an array
local $/; # all local variables become change abilitiy
open (IN, $file); #opens file for read
my $line = <IN>; #????
@matches = $line =~ m {cd\s+ ([\w.]+) [\n\r]wget.*?http://(.+)}g; #????
while( ($x,$y, @matches) = @matches) #Funktion for all Values
{
print "$x $y\n"; #writing all values
}
and how I put the values inthe new file
You have changed the code. You had to use it as per the usage I told you above.
Just put that code into a file. Don't put a filename in the second version, pass it as a command line
When you save it, you should run it with filename passed at command line as I showed above.
Just put that code into a file. Don't put a filename in the second version, pass it as a command line
When you save it, you should run it with filename passed at command line as I showed above.
ASKER
Works excellent
Now that we know it works, here is the explanation.
It reads the entire file into a single variable. Then applies regular expressions to find patterns. It stores those patterns in a list and finally prints the list in pairs of two.
It reads the entire file into a single variable. Then applies regular expressions to find patterns. It stores those patterns in a list and finally prints the list in pairs of two.
#!/usr/bin/perl
#Getting filename from command line
my $file = $ARGV[0];
#Undefine the input record seperator -- so it would read the entire file into a single variable
local $/;
#Getting file descriptor
open (IN, $file);
#Reading the descriptor into a single varible
my $line = <IN>;
#Matching from two consecutive lines
@matches = $line =~ m{cd\s+([\w.]+)[\n\r]wget.*?http://(.+)}g;
while( ($x,$y, @matches) = @matches)
{
print "$x $y\n";
}
Please modify the filename
Open in new window