Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: very basic, just beginning

by fletcher_the_dog (Friar)
on Aug 27, 2003 at 17:31 UTC ( [id://287100]=note: print w/replies, xml ) Need Help??


in reply to very basic, just beginning

If each file really is "straight text, no carriage returns, no spaces, etc. " then all you have to do is the following from the command line:
perl -pi -e '$_=">$ARGV\n$_\n"' *.txt
This makes the assumption that all ".txt" files in your directory fit your criteria. "$ARGV" represents the current file being used if you are using the "<>" operator. When you use -p, perl assumes that there is a
while(<>){}
loop around your code and then prints $_ at the end of each iteration through the loop. The -i causes perl to be in "In-place edit" mode, which will cause each "print" statement without a filehandle to print to whatever file $ARGV currently is. CHANGE: As jmanning2k pointed out, I forgot to put ">" in front of the file name

Replies are listed 'Best First'.
Re: Re: very basic, just beginning
by jmanning2k (Pilgrim) on Aug 27, 2003 at 18:17 UTC
    Update: I read the question better & turned down the flame-o-meter on this comment...

    This code might get you in trouble if there are newlines in the file. If that is the case then...

    It puts "filename.txt" as every other line in the file. The while loop is what gets you in trouble here. Perhaps if you do a undef $/ somewhere in there.

    Or, even better, start with your version and test for the first line.

    perl -pi -e '$ARGV =~ s/\.txt//; $_=">$ARGV\n$_\n if($.==1)"' *.txt
    This adds three things - it removes the .txt from the filename first. Second, it adds the initial '>' character. Finally, it only rewrites the first line. ($. is line number).

    Not the most elegant, but it works.

      Umm, I said "If each file really is "straight text, no carriage returns, no spaces, etc. " If there are no carriage returns there is only one line. If you are getting the file name every other line than your file has one more than one line and doesn't fit the requirement straight text, no carriage returns, no spaces, etc. Also, no where is it specified to remove the ".txt". In fact the example specifically shows the ".txt" being there
      filename: a36.txt initial filecontents : AATGACGTACGTAGTCGTAGCGT after script filecontents : >a36.txt AATGACGTACGTAGTCGTAGCGT [newline]
        Fair enough... I gave your orig comment a ++ anyway. I didn't realize the original data had no newlines.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found