Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Text File Manipulation

by sabas (Novice)
on Jan 14, 2020 at 08:28 UTC ( #11111397=note: print w/replies, xml ) Need Help??

in reply to Re: Text File Manipulation
in thread Text File Manipulation

I apologize for my elementary coding in Perl. I am a beginner and I been debugging this for a over 3 days and i cannot figure out....the idea i am making is this: file1.txt aaa bbb ccc file2.txt ddd eee fff ggg hhh ggg file3.txt should looks like these: aaa bbb ccc ggg hhh ggg the length of my rows are over 381 and when i wrote it to file3.txt the length becomes < 200 and the padding (space) for each column became one space only per column.

# CREATING UPDATE FILE FEATURE my $tmp = "$workingDir/CR/EGIS_table.txt"; open(my $fh, '<:encoding(UTF-8)', $tmp) or die "Could not open fil +e '$tmp' $!"; my @rows = <$fh>; chomp @rows; if ($updatefile) { # logical and operator if the update=1 and the + filename exist then go down my $tmp = "$workingDir/CR/EGIS_table.txt"; open(my $fh, '<:encoding(UTF-8)', $tmp) or die "Could not open + file '$tmp' $!"; my @rows = <$fh>; chomp @rows; #FORMAT AND PAD THE FIELDS WITH SPACES IN LEFT JUSTIFIED BEFOR +E WRITING TO FILE $file_requesterName = sprintf('%-30s',$file_requesterName); $file_requesterEmail = sprintf('%-35s',$file_requesterEmail); $file_reqtype = sprintf('%-15s', $file_reqtype); $file_region = sprintf('%-15s',$file_region); $file_Serial_Number = sprintf('%-15s', $file_Serial_Number); $file_aiws_OSS_IP_pri = sprintf('%-15s', $file_aiws_OSS_IP_pri +); $file_aiws_OSS_IP_sec = sprintf('%-15s', $file_aiws_OSS_IP_sec +); $file_temp_SGW_IP_pri = sprintf('%-15s', $file_temp_SGW_IP_pri +); $file_temp_SGW_IP_sec = sprintf('%-15s', $file_temp_SGW_IP_sec +); $file_bts_site_id = sprintf('%-15s', $file_bts_site_id); $file_req_date = sprintf('%-15s', $file_req_date); $file_NOC_Update_req = sprintf('%-19s', $file_NOC_Update_req); $file_OSS_enm = sprintf('%-11s', $file_OSS_enm); $file_category = sprintf('%-15s', $file_category); $file_RNC_Name = sprintf('%-10s', $file_RNC_Name); $file_submission_status = sprintf('%-15s',$file_submission_sta +tus); $file_additionalnotes = sprintf('%-100s',$file_additionalnotes +); # WRITING TEMPORARY UPDATED ROW INTO TEMPORARY FILE TO BE USED + LATER IN UPDATING THE EGIS_table.txt (MASTER TABLE) my $temporary_fileName = "$workingDir/CR/EGIS_table_oneline.tx +t"; open ($fh, '>', $temporary_fileName) or die $!; `echo "$file_requesterName $file_requesterEmail $file_reqtype +$file_region $file_Serial_Number $file_aiws_OSS_IP_pri $file_aiws_OSS +_IP_sec $file_temp_SGW_IP_pri $file_temp_SGW_IP_sec $file_bts_site_id + $file_req_date $file_NOC_Update_req $file_OSS_enm $file_category $fi +le_RNC_Name $file_submission_status $file_additionalnotes \n" > $temp +orary_fileName`; close ($fh); # END OF WRITING TEMPORARY UPDATED ROW INTO TEMPORARY FILE TO +BE USED LATER IN UPDATING THE EGIS_table.txt (MASTER TABLE) my $index=1; # UPDATE THE ARRAY INDEX THAT WAS EDITED BY THE USER foreach my $row(@rows) { next if $row =~ /REQUESTER_NAME/; # SKIP THE HEADER OF TH +E TABLE if ($index == $submit_button) { # IF ARRAY INDEX = SUBMI +T_BUTTON THEN UPDATE THE CONTENT OF AN ARRAY. Example index=13 and yo +u change the requester name to Alvin and hit the submit button, only +index 13 will be updated. print "The Submit Button was Pressed and the array ind +ex=" . $submit_button; @rows[$submit_button] = $file_requesterName . $file_re +questerEmail . $file_reqtype . $file_region . $file_Serial_Number . $ +file_aiws_OSS_IP_pri . $file_aiws_OSS_IP_sec . $file_temp_SGW_IP_pri +. $file_temp_SGW_IP_sec . $file_bts_site_id . $file_req_date . $file_ +NOC_Update_req . $file_OSS_enm . $file_category . $file_RNC_Name . $ +file_submission_status . $file_additionalnotes; print "ROWS=@rows[$submit_button]"; } $index++; } $index--; # print "Array Index Total Row=" . $index; # STORE TEMPORARILY THE UPDATED DATA HERE my $EGIS_table_fileName_tmp = "$workingDir/CR/EGIS_table_tmp.t +xt"; open(EGIS_table_fileName_tmp_fh, '>:encoding(UTF-8)', $EGIS_ta +ble_fileName_tmp) or die "Could not open file '$EGIS_table_fileName_t +mp' $!"; # ORIGINAL FORMATTED FILE WITH PADDED with space COLUMNS my $EGIS_table_fileName = "$workingDir/CR/EGIS_table.txt"; + open(EGIS_table_fileName_fh, '<:encoding(UTF-8)', $EGIS_table_ +fileName) or die "Could not open file '$EGIS_table_fileName' $!"; # FILE THAT HOLDS THE USER CHANGED ROW my $EGIS_table_fileName_oneline = "$workingDir/CR/EGIS_table_o +neline.txt"; open(EGIS_table_fileName_oneline_fh, '<:encoding(UTF-8)', $EGI +S_table_fileName_oneline) or die "Could not open file '$EGIS_table_fi +leName_oneline' $!"; # STORE THE CONTENT OF EGIS_TABLE_ONELINE.TXT into variable my $oneline_content; open(my $fh, '<', $EGIS_table_fileName_oneline) or die "cannot + open file $EGIS_table_fileName_oneline"; { local $/; $oneline_content = <$fh>; } close($fh); print "One line txt file content=$oneline_content"; my $j=0; print "*SUBMIT_BUTTON=$submit_button*"; while (my $row = <EGIS_table_fileName_fh>) { chomp $row; print "Hello"; my $row_length = length($row); print "THE LENGTH OF ROW=$row_length"; $j++; if ($j==$submit_button) { # IF THE ROW IS THE DATA TO BE CH +ANGED then OPEN THE FILE AND REPLACE THE ROW WITH UPDATED ONE print "***** Matching Row Found ******"; print "From File Row=$row*"; $row = "$file_requesterName $file_requesterEmail $file_r +eqtype $file_region $file_Serial_Number $file_aiws_OSS_IP_pri $file_a +iws_OSS_IP_sec $file_temp_SGW_IP_pri $file_temp_SGW_IP_sec $file_bts_ +site_id $file_req_date $file_NOC_Update_req $file_OSS_enm $file_categ +ory $file_RNC_Name $file_submission_status $file_additionalnotes"; print "New Row=$row*"; `echo $row >> $EGIS_table_fileName_tmp`; # THIS IS NOT + WORKINGGGGGGGGGGGGGGGGGGGGGGGGG! } else { # IF THE ROW IS NOT TO BE CHANGED JUST WRITE IT DO +WN AS IS print "***** NotMatching Row Found Writing straight to E +GIS_table_tmp.txt******"; print $row; `echo $row >> $EGIS_table_fileName_tmp`; # THIS IS NO +T WORKINGGGGGGGGGGGGGGGGGGGGGGGGG! } } close (EGIS_table_fileName_tmp_fh); close (EGIS_table_fileName_fh); copy $EGIS_table_fileName_tmp, $EGIS_table_fileName; } # END OF CREATING UPDATE FILE FEATURE

Replies are listed 'Best First'.
Re^3: Text File Manipulation
by hippo (Chancellor) on Jan 14, 2020 at 10:12 UTC
    and the padding (space) for each column became one space only per column.
    `echo $row >> $EGIS_table_fileName_tmp`;     # THIS IS NOT WORKINGGGGGGGGGGGGGGGGGGGGGGGGG!

    The problem is that instead of opening a file and printing to it you are instead shelling out and running echo from the shell which by default collapses multiple spaces into just one.

    Have a good read through perlintro, especially the part about how to write to files from within Perl in the section Files and I/O. Good luck.

Re^3: Text File Manipulation
by soonix (Canon) on Jan 14, 2020 at 11:54 UTC
    Seconding hippo's comment, perhaps it helps to understand that
    print "something";
    (which you are already using), is just an abbreviation for
    print STDOUT "something";
    and instead of
    open ($fh, '>', $temporary_fileName) or die $!; `echo "... long string ..." > $temporary_fileName`; close ($fh);
    where open and close are effectively useless, you should use
    open ($fh, '>', $temporary_fileName) or die $!; print$fh "... long string ..."; close ($fh);
    (I prefer to put the file handle after print without a space to help remembering there's no comma after the file handle)

    If you feel adventurous, you might to look over Text::Table Tutorial, but that's an advanced topic, just to see what's possible.

Re^3: Text File Manipulation
by roboticus (Chancellor) on Jan 14, 2020 at 14:56 UTC


    Your code is frightening. You need to learn how to read code and understand what's happening inside it. Your code has three different places where you're creating the desired row of data (one which looks just fine) and then you're playing with temporary files in desperate attempts at trying to somehow get the data into the file you want.

    It looks like it would work if you'd remove all the junk you don't really need. I was able to cut your code down into something that looks reasonable in less than 40 lines. I'd ordinarily make a long post describing some ways to improve your code, but I don't have the time to do it today. Instead I'll show you the line that looks like it would work (I changed @rows to $rows to eliminate a warning):

    $rows[$submit_button] = $file_requesterName . $file_requesterEmail . $ +file_reqtype . $file_region . $file_Serial_Number . $file_aiws_OSS_IP +_pri . $file_aiws_OSS_IP_sec . $file_temp_SGW_IP_pri . $file_temp_SGW +_IP_sec . $file_bts_site_id . $file_req_date . $file_NOC_Update_req . + $file_OSS_enm . $file_category . $file_RNC_Name . $file_submission_ +status . $file_additionalnotes;

    Since that line puts the data in the correct format into @rows, you can delete the code dealing with temporary files and just write the @rows data to the output file.


    When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111397]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-11-27 01:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found