LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-15-2009, 03:18 AM   #1
cgkee
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
Output specific line in file


I need to have a look at a specific line (355) in a file that is reporting an error. Can someone tell me how to do this?
 
Old 09-15-2009, 03:38 AM   #2
cgkee
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Output Nth Line from a file

I think I have answered the question myself:
Code:
head -N file | tail -1
Where N is the line number you wish to view, and file is the filename
but if any experts then have additional useful tips that this often leads on to, I would be delighted to hear from you!
 
Old 09-15-2009, 03:44 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by cgkee View Post
I think I have answered the question myself:
Code:
head -N file | tail -1
Where N is the line number you wish to view, and file is the filename
but if any experts then have additional useful tips that this often leads on to, I would be delighted to hear from you!
It's not an uppercase 'N' but lowercase, and Linux commands almost always care about case.

Another approach in a situation like this is to find something about the specific error message's content that's unique, like certain words or numbers, and filter on that instead of the line number, which is typically more difficult to pinpoint. This had the added advantage that any similar error messages will appear in the result.
 
Old 09-15-2009, 03:44 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Here are two additional ways using sed and awk:
Code:
awk 'NR==355' file
sed -n 355p file
 
Old 09-15-2009, 03:48 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by lutusp View Post
It's not an uppercase 'N' but lowercase, and Linux commands almost always care about case.
I think the OP meant N for integer, using the old posix standard. Indeed, better to do as you suggest:
Code:
head -n355 file | tail -1
that assures portability to newer systems that discarded compatibility with the old standard.
 
Old 09-15-2009, 04:08 AM   #6
cgkee
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
I was indeed using N as an integer, and did not include it in the command, so to get line 355 I did the following
Code:
head -355 file | tail -1
but some of colucix's suggestions are even shorter and easier.

Thank you all for your quick responses and help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 01:38 PM
Remove specific text in output line dwarf007 Linux - General 8 07-04-2007 12:28 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 05:02 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 08:45 PM


All times are GMT -5. The time now is 04:50 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration