• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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
0
michapdm
Asked:
michapdm
  • 4
  • 2
1 Solution
 
farzanjCommented:
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

0
 
farzanjCommented:
Or use this one:

Usage: perl script datafile


#!/usr/bin/perl
my $file = $ARGV[0];
local $/;
open (IN, $file);
my $line = <IN>;
@matches = $line =~ m{cd\s+([\w.]+)[\n\r]wget.*?http://(.+)}g;
while( ($x,$y, @matches) = @matches)
{
        print "$x $y\n";
}

Open in new window

0
 
ozoCommented:
perl -ne 'print for m{mkdir\s*(.*)},m{wget -cmkr(\s+)http://(.*)}s' datafile
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
michapdmAuthor Commented:
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
0
 
farzanjCommented:
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.
0
 
michapdmAuthor Commented:
Works excellent
0
 
farzanjCommented:
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

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now