LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2009, 12:59 PM   #1
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
Reading the content of line number $ (Shell Script)


Hello Geeks and Fellows,

I'm writing a shell script.. and in some part of it I need that script to read the content of a line by giving that line number.

Well, I found a work-around to achieve that, but I'm looking for something more efficient to enhance the script performance.

Here is the work-around I got to:


Code:
#!/bin/bash

LineNumber=5
FileLocation='/some/locations/file'

# Reading the content of line number $LineNmber stored in FileLocation

Content=$(head $FileLocation -n$LineNumber | tail -n1)

# End
So, is there any thing simpler that that?


Regards
 
Old 11-11-2009, 01:05 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You can use sed:
Code:
num=10
file=my_file
sed -n "$num p" $file
 
Old 11-11-2009, 05:23 PM   #3
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Thanks sycamorex..

That was good..

I hope to see some more choices..

Regards
 
Old 11-12-2009, 12:18 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Perl snippet
Code:
# '$.' is current line num
open(FH,'foo.txt') or die "$!"; 
while (<FH>) 
{ 
  print "$_" if( $. == 5 ); 
} 
close(FH);
 
Old 11-12-2009, 12:30 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
read a line using line number in gawk
Code:
#!/bin/bash
num=10
gawk -v ln="$num" 'NR==ln' file
 
Old 11-12-2009, 03:35 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
shorter perl

Code:
 perl -ne 'print if $. == 3' file
 
Old 11-15-2009, 07:44 PM   #7
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Original Poster
Rep: Reputation: 17
Thanks all..

That's fair enough

Much Regards..
 
Old 11-17-2009, 11:53 AM   #8
stevenworr
LQ Newbie
 
Registered: Oct 2008
Posts: 13

Rep: Reputation: 1
Why don't you just do it in bash?

Code:
#!/bin/bash

typeset -i ln
typeset -i ii
ln=$1
fn=$2
for ((ii=1; ii < ln + 1; ii=ii+1 ))
do
    read line
    (( ii == ln )) && echo "$line"
done < $fn
Now you're not starting any subprocesses.
 
Old 11-17-2009, 02:10 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by stevenworr View Post
Why don't you just do it in bash?

[/CODE]

Now you're not starting any subprocesses.
one of the advantage is that if he is reading a very big file, using a file reading tool such as awk is the way to go. bash's loop+read is simply too slow.
 
Old 11-17-2009, 03:27 PM   #10
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
But, if the files are small and he is doing lots of reads on multiple files, the bash solution will be much faster, than calling any external program. I suspect that sed would be faster than awk, which would be faster than perl -because of latency in forking the external program.
Here's another example using bash:
Code:
#!/bin/bash
LineNumber=5
FileLocation='/some/locations/file'
# or: LineNumber=$1 FileLocation=$2

COUNT=1
while read LINE ; do
  # break once the line is found to avoid looping through the whole file
  [[ $COUNT -eq $LineNumber ]] && echo $LINE && break
done < $FileLocation
 
Old 11-17-2009, 06:20 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gnashley View Post
But, if the files are small and he is doing lots of reads on multiple files, the bash solution will be much faster, than calling any external program.
how about a small test on file with 20000 lines ( a small file) and getting the 1000th line
Code:
$ head -5 file
     1  this is a line
     2  this is a line
     3  this is a line
     4  this is a line
     5  this is a line
$ tail -5 file
 19996  this is a line
 19997  this is a line
 19998  this is a line
 19999  this is a line
 20000  this is a line

$ more test.sh
#!/bin/bash
LineNumber=$1
FileLocation="file1"
COUNT=1
while read LINE ; do
  [[ $COUNT -eq $LineNumber ]] && echo $LINE && break
  let COUNT=COUNT+1
done < $FileLocation

$ time ./test.sh 1000
1000 this is a line

real    0m0.090s
user    0m0.072s
sys     0m0.012s

$ time ./test.sh 1000
1000 this is a line

real    0m0.096s
user    0m0.073s
sys     0m0.012s

$ time awk 'NR==1000{print;exit}' file
  1000  this is a line

real    0m0.003s
user    0m0.001s
sys     0m0.001s

$ time awk 'NR==1000{print;exit}' file
  1000  this is a line

real    0m0.004s
user    0m0.000s
sys     0m0.003s

$ time sed -n '1000{p;q}' file
  1000  this is a line

real    0m0.003s
user    0m0.000s
sys     0m0.003s

$ time sed -n '1000{p;q}' file
  1000  this is a line

real    0m0.003s
user    0m0.001s
sys     0m0.002s
calling sed or awk is much faster. not to mention the amount of bash code to cook up as well.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script that sorts data with number beginning on each line. sunksullen Programming 12 05-09-2007 03:35 PM
how to program in shell to get a part of a file by line number George2 Linux - General 2 11-19-2006 02:41 AM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 06:09 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM
Extracting the line number in Unix Shell? Aziz Programming 2 12-01-2004 11:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration