Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to the particular line in a file 1

Status
Not open for further replies.

Johnathon

Programmer
Jul 29, 2006
23
US
Hi,
What the script command will return me the string on the line number that I supply to a file?

Regards,
Johnathon
 
This script works in the manner you describe:

NUMBER=0
for line in `cat /etc/passwd`
do
NUMBER=`expr $NUMBER + 1`
if [ $NUMBER = $1 ]
then
echo $line
exit
fi
done

supply your line number as a parameter (eg findline 5) and the /etc/passwd with your filename. Hope this helps.
 
NUMBER=0
cat /etc/passwd|while read line
do
NUMBER=`expr $NUMBER + 1`
if [ $NUMBER = $1 ]
then
echo $line
exit
fi
done
This even works when there are spaces used in the file.
Gregor.Weertman@mailcity.com
 
If your line number is NUMBER and your file name is file_name, you can do

head -n $NUMBER file_name|tail -1


 
yes right. Till ± 162598 lines in HPUX 10.20

When it is a large file use this:

awk '{ if( NR == "'$1'") { print $0; exit}}' $2
put it in lineno

lineno 432439 thefile

Gregor.Weertman@mailcity.com
 
line="$1"
awk 'NR=="$line" { print } ' $2

if you save this as linenum
run as follows
linenum 5 filename
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top