LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   a simple question (https://www.linuxquestions.org/questions/linux-software-2/a-simple-question-472254/)

basak 08-09-2006 06:18 AM

a simple question
 
Hi..Is there a bash shell command which exactly selects and returns the given numbered line in a file (ex. 3rd line)?Thanks..

homey 08-09-2006 06:30 AM

Do you mean something like this?
Code:

sed -n '3p' file.txt

Valdis Grinbergs 08-09-2006 10:02 PM

basak and homey,

Here is another way:
head -3 file.txt | tail -1
This captures the first 3 lines of file.txt
then displays only the last of these lines.

This approach can easily be modified to get a range of lines,
for example:
head -2 file.txt | tail -3
gives the third line and the lines directly before and after it (lines 2-4).

'head' and 'tail' are two simple utilities that are very useful when you do not need to do something more complicated with sed, awk, perl, etc. You might want to check out the man pages for both.

If you find another (and especially if you find a better) way, please post it here.

Oxagast 08-10-2006 12:52 AM

Sed makes my brain bleed. I wrote a log stripper which implimented sed heavily... gahhhhh. Worked amazing when I finally figured out how to do all that I wanted to do though... very versatile program.


All times are GMT -5. The time now is 05:53 AM.