Click to See Complete Forum and Search --> : read file one line at a time?
Dogcow
03-06-2003, 01:56 PM
Does anyone know a linux command that can be used to read a single line from either input text or a file? I'm trying to find a way to pipe multi-lined output from one command to another command one line at a time, performing work on that line, then working on the next line.
TIA,
-Dogcow "moof!"
garskoci
03-06-2003, 02:28 PM
Depending on what you want to do... A nawk command will work pretty well!
Dogcow
03-06-2003, 02:32 PM
I have no nawk command...?
Running Redhat (Linux Version 2.4.18-14smp)
Basically, I'm looking for a simple command (or script) that will allow me to tell it "readLine 2" and it will return the second line of the text I send to it.
TIA,
-Dogcow "moof!"
garskoci
03-06-2003, 02:49 PM
If I understand you correctly.... You have a file and you want to dump a specific line from that file. Correct? That can be done in a short Perl script, bash script.
garskoci
03-06-2003, 03:43 PM
You may have to change the location of perl.
#!/opt/utilities/bin/perl
# ARGV[0] - input filename.
# ARGV[1] - line number to display
# check for input file definition
if($ARGV[0] eq ''){
print "\n\nERROR-Command line arguement <Input File> is missing.\n";
exit;
}
# check for input file definition
if($ARGV[1] eq ''){
print "\n\nERROR-Command line arguement <Input File> is missing.\n";
exit;
}
# open input files
open(INFILE, "$ARGV[0]") or die "Can't read $ARGV[0]: $!";
$i=0;
while(<INFILE>){
$i++;
if ($i == $ARGV[1]){
print $_;
exit;
}
}
close(INFILE);
Dogcow
03-06-2003, 04:30 PM
Sweet, thanks man... I'm very new to perl, but I'm looking over your code.
-Dogcow "moof!"
Mr_Munkey
03-07-2003, 03:19 AM
Hi,
I think you should be able to do a short one line comman from the prompt to accomplish this. You have Red Hat so you should have the regular awk command (I could be wrong, just thought it was a pretty old command) It would go something like this:
awk 'NR == <line number here>' /path_to_filename/filename
That of course is if you have the normal awk command.
Dogcow
03-07-2003, 12:57 PM
Sweet, that's a lot easier. Thanks man!
-Dogcow "moof!"