Link to home
Start Free TrialLog in
Avatar of michapdm
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-Webinterface
mkdir St.Margrethen
cd St.Margrethen
wget -cmkr http://172.16.x.a

#-------------------------------------------
cd /home/merkur/Stationsnetz-Config-Webinterface
mkdir Hoechst
cd Hoechst
wget -cmkr http://172.16.x.b

#-------------------------------------------
cd /home/merkur/Stationsnetz-Config-Webinterface
mkdir Buriet
cd  Buriet
wget -cmkr http://172.16.x.c

#-------------------------------------------
cd /home/merkur/Stationsnetz-Config-Webinterface
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
Avatar of farzanj
farzanj
Flag of Canada image

Here is a crude code to do what you want
Please modify the filename
my $file = Filename;
local $/;
open (IN, $file);
my $line = <IN>;
@matches = $line =~ m{cd\s+([\w.]+)[\n\r]wget.*?http://(.+)}g;
$" = " ";
my $sol = "@matches";
$sol =~ s/(\S+ \S+) /$1\n/g;
print $sol, "\n";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perl -ne 'print for m{mkdir\s*(.*)},m{wget -cmkr(\s+)http://(.*)}s' datafile
Avatar of michapdm
michapdm

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

Open in new window