Read the specified line number from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read the specified line number from file
# 1  
Old 12-18-2009
Read the specified line number from file

Hi Guys,
I am new to unix.
Actually i want help in writing an single command where
i can actually read specific line number in file where the line number will be passed to command as parameter.

ex.
1 a
2 b
3 c
4 d

And to my command i pass as 2.
so i should get output as 2 b

---------- Post updated at 05:28 PM ---------- Previous update was at 05:25 PM ----------

i implemented this by below code
Code:
head -n parameter | tail -n 1

but if file is big this will degrade the performance.

But i need to have this as in single line only.
And if possible without awk.
# 2  
Old 12-18-2009
Code:
sed '2!d' yourfile

Number = your wanted line number.
# 3  
Old 12-18-2009
Hi.

Code:
n=3
$ sed "${n}!d" file
or
$ sed -n "${n}p" file
or
awk -v n=$n 'NR == n' file

# 4  
Old 12-18-2009
Quote:
Originally Posted by zaxxon
Code:
sed '2!d' yourfile

Number = your wanted line number.
Hi zaxxon,

Thanks for reply.
Can you please explain me what this command does.
i tried reading man sed in shell but it didn't help
# 5  
Old 12-18-2009
Code:
$ num=3
$ gawk -v n=$num 'NR==n{print;exit}' file

# 6  
Old 12-18-2009
Tools Here is another approach

num=3
cat -n samplefile.txt | grep " $num" | cut -f2
# 7  
Old 12-18-2009
You might want to check websites for sed tutorials etc. or get a good book about it like sed&awk from O'Reilly.

It means
  • 2 ____ when the address is line 2
  • !d ____ do not delete it - ie. it will be printed

The opposite would have been:
Code:
sed -n '2p' infile

Which have meant to not print out every line but only the address you want to process.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

5. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

7. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

10. Shell Programming and Scripting

How to get the line number from while read

I have a file TXTPROCESS.TXT.20071129 in which line 1 contains the file name and rest of the records are data. The file data looks like this: TXTPROCESS.TXT.20071129 DIVD20071129 INTR20071129 BALN20071129 From line 2 onwards the first 4 characters defines individual process. What I... (2 Replies)
Discussion started by: skymirror
2 Replies
Login or Register to Ask a Question