# Read Data from text file and write to text file

Status
Not open for further replies.

#### murari208@gmail.com

##### Newbie level 3
hi all,
there is a text file containing some data like number of samples , time ,x0...
i need to read only times of the sample and write it in a text file since i am taking large number of samples .can you please help me in solving this.

##### Super Moderator
Staff member
How about the following short perl program.

Code Perl - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/local/bin/perl
use 5.012;
use warnings;
use strict;

# extract time in a file with the following format:
# number_of_samples, time, x0, x1, ....
# Expects that the data is comma+1space separated, change the split line if you have a different separator.
my $filename =$ARGV[0];
open (my $read_fh, '<',$filename);

while (my $line = <$read_fh>) {
my @data = split /, /, $line; # split on comma+1space if (length$data[1]) {print "$data[1]\n"} } close ($read_fh);

If you want the output in another file just redirect the output on the command line to another file.

Status
Not open for further replies.