Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Incorrectly adding a newline character

by pimperator (Acolyte)
on Oct 08, 2013 at 04:14 UTC ( [id://1057351]=perlquestion: print w/replies, xml ) Need Help??

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

This is my tab delimited input file

Name \t Street \t Address

This is how I want my output file to look like

Street \t Address \t Address

(yes duplicate the next two columns) My output file looks like this instead

Street \t Address \n \t Address

What is going on with perl? This is my code.

open (IN, $ARGV[0]); open (OUT, ">output.txt"); while ($line = <IN>){ chomp $line; @line=split/\t/,$line; $line[2]=~s/\n//g; print OUT $line[1]."\t".$line[2]."\t".$line[2]."\n"; } close( OUT);

Replies are listed 'Best First'.
Re: Incorrectly adding a newline character
by McA (Priest) on Oct 08, 2013 at 05:00 UTC

    Hi,

    if you're on UNIX and have a text file which lines ends with \r\n the chomp only throws the \n away, but not \r. Have a look at your file with hexdump -C.

    Regards
    McA

Re: Incorrectly adding a newline character
by vinoth.ree (Monsignor) on Oct 08, 2013 at 04:38 UTC

    Try Chomping an array If you chomp an array, it will remove a newline from the end of every element in the array

    So, try the blow code and tell what happens!

    open (IN, $ARGV[0]); open (OUT, ">output.txt"); while ($line = <IN>){ chomp $line; @line=split/\t/,$line; chomp(@line); print OUT $line[1]."\t".$line[2]."\t".$line[2]."\n"; } close( OUT);

    All is well
Re: Incorrectly adding a newline character
by Genis200 (Initiate) on Oct 08, 2013 at 04:34 UTC
    The code seems to be fine...Creating a test tab delimited input file and using the code you have here, there is no issue...I get
    Street \t Address \t Address
    Perhaps check your file and Perl version, maybe there's something you're missing.
Re: Incorrectly adding a newline character
by Laurent_R (Canon) on Oct 08, 2013 at 06:02 UTC

    Your address field has a trailing new line character (the end of your input line), you need to remove it with the chomp function. It would be actually be two characters, new line and carriage return, if your file was built under Windows, but chomp will take care of that (the exception being with a mix of 2 OSes, for example if wou created your file under Windows and use it under Unix, in which case a regular expression will be needed).

Re: Incorrectly adding a newline character
by Lennotoecom (Pilgrim) on Oct 08, 2013 at 07:58 UTC
    while(<DATA>){ print "$1\t$2\t$2\n" if /\t(.+)\t(.+)$/; } __DATA__ a first street 1A b second street 2B c third street 3C d fourth street 4D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1057351]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found