Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Other *NIX Forums > Other *NIX
User Name
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.


  Search this Thread
Old 03-11-2008, 12:16 AM   #1
LQ Newbie
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Efficient way to read only 1st line from number of files


I am struck up with some problem. I am analyzing on an efficient way to read number of files. Actually I want to read only the first line from each of them.
There could be hundreds of them in the directory/sub-directories; but not huge in size.
Can anyone suggest a way I can do it without compromising too much on memory and performance.

Thanks in advance.

Old 03-11-2008, 12:32 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,824

Rep: Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452
"head n=1" ???.
Stick it in a (bash) loop. Shouldn't be too onerous.
Old 03-31-2008, 11:55 AM   #3
Senior Member
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS
Posts: 1,040

Rep: Reputation: 59
Originally Posted by anurade View Post
... I want to read only the first line from each of them.
There could be hundreds of them in the directory/sub-directories
I'd use:
find . -type f -exec head -1 {} \;
Or, if you need the name of the file as its first line:
find . -type f | while read FN
  echo "$FN : head -1 $FN"


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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 04:49 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 02:42 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
How can I read pdf files at the command line? hansi umayangan Linux - General 8 09-02-2004 08:27 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM

All times are GMT -5. The time now is 05:48 PM.

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