Re: Skipping Commented Line in File [message #67585] |
Fri, 07 August 2009 10:31 |
Gregory
Messages: 2 Registered: August 2009
|
Junior Member |
|
|
On Aug 6, 4:45 pm, Vikram <vikramivat...@gmail.com> wrote:
> Hello,
>
> I was wondering if there is a way to skip a commented line in a .txt
> file, for example:
>
> #Count Time
> 1 60
> 2 80
> 3 100
>
> I would like to start from the second row data and skip the labels.
>
> Thanks,
> Vikram
There is a command readcol in IDL Astronomy user's library which
is very convenient in cases like this. It skips comment lines and
reads each column in a separate vector. In this case all you need to
do would be:
readcol,'file.txt',count,time,format='f,f'
and you have all your numbers in Count column are in variable "count",
Time column is in "time".
The library is in http://idlastro.gsfc.nasa.gov/homepage.html
Gregory
|
|
|
Re: Skipping Commented Line in File [message #67592 is a reply to message #67585] |
Thu, 06 August 2009 15:13  |
vikramivatury
Messages: 24 Registered: May 2009
|
Junior Member |
|
|
On Aug 6, 6:08 pm, Paul van Delst <paul.vande...@noaa.gov> wrote:
> Vikram wrote:
>> Hello,
>
>> I was wondering if there is a way to skip a commented line in a .txt
>> file, for example:
>
>> #Count Time
>> 1 60
>> 2 80
>> 3 100
>
>> I would like to start from the second row data and skip the labels.
>
>> Thanks,
>> Vikram
>
> I can think of several ways, but below is what I use for files that may also include
> embedded blank lines and comments:
>
> FUNCTION Is_A_CommentLine, Line
> RETURN, STRMID(STRTRIM(Line,1),0,1) EQ '#'
> END ; FUNCTION Is_A_CommentLine
>
> FUNCTION Is_A_BlankLine, Line
> RETURN, STRLEN(STRTRIM(Line,2)) EQ 0
> END ; FUNCTION Is_A_BlankLine
>
> PRO test
> Line = ''
> OPENR, fid, 'test.txt', /GET_LUN
> WHILE ( NOT EOF(fid) ) DO BEGIN
> ; ...Read a line from the file
> READF, fid, Line
> ; ...Cycle loop if this is an embedded comment or blank line
> IF ( Is_A_CommentLine(Line) OR Is_A_BlankLine(Line) ) THEN CONTINUE
> ; Otherwise read the line into variables
> READS, Line, x, y
> PRINT, x, y
> ; Store x and y into array
> ; .... etc etc ....
> ENDWHILE
> FREE_LUN, fid
> END
>
> Here's my test.txt file:
>
> #<----start of file----->
> #Count Time
> 1 60
> 2 80
> 3 100
> 4 80
> 5 100
> 6 100
> #embedded comment!
> 7 80
> 8 100
> 9 100
> 10 80
>
> # comment surrounded by blank lines!
>
> 11 100
> 12 100
> 13 80
> 14 100
> 15 100
> 16 80
> 17 100
> #<----end of file----->
>
> and here the above test.pro output:
>
> IDL> .run test
> % Compiled module: IS_A_COMMENTLINE.
> % Compiled module: IS_A_BLANKLINE.
> % Compiled module: TEST.
> IDL> test
> 1.00000 60.0000
> 2.00000 80.0000
> 3.00000 100.000
> 4.00000 80.0000
> 5.00000 100.000
> 6.00000 100.000
> 7.00000 80.0000
> 8.00000 100.000
> 9.00000 100.000
> 10.0000 80.0000
> 11.0000 100.000
> 12.0000 100.000
> 13.0000 80.0000
> 14.0000 100.000
> 15.0000 100.000
> 16.0000 80.0000
> 17.0000 100.000
>
> cheers,
>
> paulv
Thanks for your help Paul! I will take at look at this program.
-Vikram
|
|
|
Re: Skipping Commented Line in File [message #67593 is a reply to message #67592] |
Thu, 06 August 2009 15:08  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Vikram wrote:
> Hello,
>
> I was wondering if there is a way to skip a commented line in a .txt
> file, for example:
>
> #Count Time
> 1 60
> 2 80
> 3 100
>
> I would like to start from the second row data and skip the labels.
>
> Thanks,
> Vikram
I can think of several ways, but below is what I use for files that may also include
embedded blank lines and comments:
FUNCTION Is_A_CommentLine, Line
RETURN, STRMID(STRTRIM(Line,1),0,1) EQ '#'
END ; FUNCTION Is_A_CommentLine
FUNCTION Is_A_BlankLine, Line
RETURN, STRLEN(STRTRIM(Line,2)) EQ 0
END ; FUNCTION Is_A_BlankLine
PRO test
Line = ''
OPENR, fid, 'test.txt', /GET_LUN
WHILE ( NOT EOF(fid) ) DO BEGIN
; ...Read a line from the file
READF, fid, Line
; ...Cycle loop if this is an embedded comment or blank line
IF ( Is_A_CommentLine(Line) OR Is_A_BlankLine(Line) ) THEN CONTINUE
; Otherwise read the line into variables
READS, Line, x, y
PRINT, x, y
; Store x and y into array
; .... etc etc ....
ENDWHILE
FREE_LUN, fid
END
Here's my test.txt file:
#<----start of file----->
#Count Time
1 60
2 80
3 100
4 80
5 100
6 100
#embedded comment!
7 80
8 100
9 100
10 80
# comment surrounded by blank lines!
11 100
12 100
13 80
14 100
15 100
16 80
17 100
#<----end of file----->
and here the above test.pro output:
IDL> .run test
% Compiled module: IS_A_COMMENTLINE.
% Compiled module: IS_A_BLANKLINE.
% Compiled module: TEST.
IDL> test
1.00000 60.0000
2.00000 80.0000
3.00000 100.000
4.00000 80.0000
5.00000 100.000
6.00000 100.000
7.00000 80.0000
8.00000 100.000
9.00000 100.000
10.0000 80.0000
11.0000 100.000
12.0000 100.000
13.0000 80.0000
14.0000 100.000
15.0000 100.000
16.0000 80.0000
17.0000 100.000
cheers,
paulv
|
|
|