Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: how to read very long text lines?


From   Phil Clayton <[email protected]>
To   [email protected]
Subject   Re: st: how to read very long text lines?
Date   Fri, 8 Mar 2013 20:57:19 +1100

It's easy enough to read the second line of a file (no matter how long) and save it to another one. Here's an example:


. type testin.txt // second line is 1000 characters long
first line
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
> 234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
> 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
> 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
> 901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
> 890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
> 789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
> 67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
third line

. type testout.txt // to show it doesn't exist yet
file testout.txt not found
r(601);

end of do-file

r(601);

. do "/var/folders/0b/3868hfw558v80kc4jrr1dr900000gn/T//SD00462.000000"

. tempname myfilein myfileout

. file open `myfilein' using testin.txt, read

. file read `myfilein' line

. file read `myfilein' line

. file close `myfilein'

. 
. display "`line'"
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
> 234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
> 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
> 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
> 901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
> 890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
> 789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
> 67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

. 
. file open `myfileout' using testout.txt, write replace
(note: file testout.txt not found)

. file write `myfileout' "`line'"

. file close `myfileout'

. 
. type testout.txt
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
> 234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
> 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
> 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
> 901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
> 890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
> 789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
> 67890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

. 
end of do-file



On 08/03/2013, at 6:24 PM, Li Chuntao (Tony) <[email protected]> wrote:

> Yes, you are right. I am also trying to handle it with Powershell, but
> the problem is, i need this specific line and then transfer it into a
> Stata data format, probably by using post command. Thus, if i can read
> this line via Stata, i do not need to shift between softwares and can
> design a package.
> 
> 
> 
> On Fri, Mar 8, 2013 at 3:17 PM, Carl Severson <[email protected]> wrote:
>> Is there a specific reason you are using Stata to do this?
>> 
>> I feel like there are a number of better tools to choose from... perl,
>> python, unix command line tools which I can't seem to remember the
>> names of this late at night, for example
>> 
>> Carl Severson
>> 
>> On Fri, Mar 8, 2013 at 12:13 AM, Li Chuntao (Tony) <[email protected]> wrote:
>>> I want to copy a specific line of a text file to another text file.
>>> the line number is given, say 2nd line, but this line is too long,
>>> more than 1000 characters.
>>> 
>>> Can I use "file read" and "file write" to handl it?
>>> 
>>> 
>>> 
>>> On Fri, Mar 8, 2013 at 2:58 PM, Phil Clayton
>>> <[email protected]> wrote:
>>>> What are you actually trying to achieve?
>>>> Phil
>>>> 
>>>> On 08/03/2013, at 5:56 PM, "Li Chuntao (Tony)" <[email protected]> wrote:
>>>> 
>>>>> Thank you, Ryan. But i am still lost with my file reading.
>>>>> 
>>>>> Tony
>>>>> 
>>>>> 
>>>>> On Fri, Mar 8, 2013 at 2:30 PM, Ryan Kessler
>>>>> <[email protected]> wrote:
>>>>>> Hi Tony,
>>>>>> 
>>>>>> The key here is to steer clear of the "=" sign; see -help macro-,
>>>>>> -help limits-, and -help extended_fcn-.
>>>>>> 
>>>>>> local long ""
>>>>>> forval i=1/500 {
>>>>>>       local long "`long' `=`i'^2'"
>>>>>> }
>>>>>> di "`long'"
>>>>>> di length("`long'")
>>>>>> di "`: length local long'"
>>>>>> 
>>>>>> Related discussion -->
>>>>>> http://www.stata.com/statalist/archive/2011-09/msg00973.html
>>>>>> 
>>>>>> Best,
>>>>>> 
>>>>>> Ryan Kessler
>>>>>> 
>>>>>> On Thu, Mar 7, 2013 at 11:22 PM, Li Chuntao (Tony) <[email protected]> wrote:
>>>>>>> Dear Listers,
>>>>>>> 
>>>>>>>   I want to use Stata to read a text file line by line. Suppose the
>>>>>>> file is d:\temp.txt. one of the line i specially interested is very
>>>>>>> long, more than 1000 characters. I use the following code to read:
>>>>>>> 
>>>>>>> capture file close myread
>>>>>>> file open myread using d:\temp.txt , read text
>>>>>>> file readmyrea line
>>>>>>> while r(eof)==0 {
>>>>>>> local linenum = `linenum' + 1
>>>>>>> local myline=`" `macval(line)'"'
>>>>>>> disp length(`"`macval(myline)'"')
>>>>>>> file myrea hexun line
>>>>>>> 
>>>>>>> }
>>>>>>> file close hexun
>>>>>>> 
>>>>>>> It seems that the maximium length for a string here is only 240.
>>>>>>> 
>>>>>>> Is it possible to change it to longer?
>>>>>>> 
>>>>>>> thanks
>>>>>>> 
>>>>>>> Tony
>>>>>>> *
>>>>>>> *   For searches and help try:
>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>> *
>>>>>> *   For searches and help try:
>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>> *
>>>>> *   For searches and help try:
>>>>> *   http://www.stata.com/help.cgi?search
>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>> 
>>>> 
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> *   http://www.ats.ucla.edu/stat/stata/
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index