LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 11-08-2014, 02:38 PM   #1
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Rep: Reputation: Disabled
help with grep


Hi guys,

I have one sort of file with the same name in many sub-directories in the same mother folder, I need to pull out the last line in each file of those files I don't know how to do that in once, it takes very long time to go to each sub directory and in to the file and (tail -1) for each one to take the last line, I tried with grep command but I failed, any idea please?

Best,
Ave3
 
Old 11-08-2014, 03:49 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
Quote:
I have one sort of file
What sort of file is this ?

and what have you tried so far ?

please post an example
 
Old 11-08-2014, 03:52 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Perhaps the easiest way to print the last line of a file is to use
Code:
sed -n '$p' input.file
To find all the files, use the "find" command - it has an "-exec" option that will enable you to run the sed on each file found.
That should do it all for you - the hard part is learning the "find" parameters. See how you go.
 
1 members found this post helpful.
Old 11-08-2014, 05:51 PM   #4
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hi syg00,I am new using linux and never used find but after u told me I read about it and found the solution for my problm with this beautiful line ( find . -iname 'fe.dat' -exec tail -n1 {} \, can you tell me what this bracket does or this part {} \; of the line?

Thank you johnvv for your concern.

Best,
 
Old 11-08-2014, 06:25 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Good for you figuring it out.
Quote:
Originally Posted by manpage
The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command
The semi-colon marks the end of the command being exec'd, and needs to be escaped (the backslash) so the shell doesn't try to interpret it.
 
1 members found this post helpful.
Old 11-09-2014, 02:37 PM   #6
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thank you that helped me.
I have another question, is there any way to run a script on many directories at once?
 
Old 11-10-2014, 07:05 AM   #7
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,493

Rep: Reputation: Disabled
That script should find all the files you have/want, run it from your home directory.
(find . -iname 'fe.dat' -exec tail -n1 {} \)
 
Old 11-10-2014, 01:13 PM   #8
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Yes this script is to find the files and it helped my first problem, but now my other concern is to run another script on many directory, I have many directories in one main directory what I do is open each directory and run a specific script that I have to get the output files, it takes a long time to open each directory and run the script in it.
My question was is it possible to find the directories and run the script on them at the same time.
 
Old 11-10-2014, 09:23 PM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Commands xargs and parallel will keep a number of processes running. So if you find the paths of the directories, either one of those can then create a queue of processes filled until it runs out of work to do. The man page for xargs should get you going, and parallel may be in your repository (it's in Gnu/Debian jessie, for example).

Best wishes ... cheers, makyo
 
Old 11-10-2014, 10:22 PM   #10
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
Quote:
Thank you johnvv for your concern.
it is not concern

it is getting the answers to the game of 20 questions answered

for starters
#1 ) is this "fe.dat'" file a text file or a binary file ?

#2 ) if a text file what format is it ?
CSV or what
 
  


Reply



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
grep to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 04:46 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:38 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
Open Source Consulting | Domain Registration