roborat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I am trying to create a report but I need to update one column based on the string value from the next column

Here's sample data from text file.

SWITCH FastEthernet1_0_4
SWITCH FastEthernet1_0_4
SWITCH FastEthernet2_0_4
SWITCH FastEthernet2_0_4

Output:

SWITCH1 FastEthernet0_4
SWITCH1 FastEthernet0_4
SWITCH2 FastEthernet0_4
SWITCH2 FastEthernet0_4

I need to loop into the file and come up with the output as above. Any suggestions / tips is highly appreciated. Thanks a lot!
  • Comment on Get the string value from one column and append to another column

Replies are listed 'Best First'.
Re: Get the string value from one column and append to another column
by Ratazong (Monsignor) on Jul 20, 2010 at 08:54 UTC

    Try the following approach

    • open file 1 for reading and file 2 for writing
    • for each line in file 1 do
      • parse it by columns (e.g.  $lineIn =~ /(SWITCH)( FastEthernet)(\d)(.*)/;)
      • create the output format (re-ordering the columns) (e.g.  $lineOut = "$1$3$2$4";)
      • write the output to file 2

    Probably you need some fine-tuning with the regular expressions ;-)

    HTH, Rata