LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-17-2007, 03:35 PM   #1
laurasong
LQ Newbie
 
Registered: Aug 2007
Posts: 9

Rep: Reputation: 0
how to use grep and tail to get portion of a file


Hi
Can someone tell me how to use grep to find a line number of the key words that I'm looking for, say, "connections" in a file and pipe the result into tail to get portion of a file that I needed.

Thanks,
 
Old 08-17-2007, 03:52 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I'm often deaf to 'homework questions', but *this* either sounds like one or a question that assumes methods. What 'portion' do you need and how do the 'connections' lines delimit those portions? Why do you need grep and tail? Etc.

What is your actual input and expected output?
 
Old 08-17-2007, 05:34 PM   #3
laurasong
LQ Newbie
 
Registered: Aug 2007
Posts: 9

Original Poster
Rep: Reputation: 0
This might be a stupid question, but not a "homework". I'm trying to modify a script to do file editing. I have two huge xml files. Message.xml is auto generated on the daily basis. Reply.xml is manually created once a week. I need to modify the script to search the key word "connections" in Message.xml and insert Reply.xml after the key word. Thought I could use grep to find "connections" line number then use tail to break Message.xml into two temp files - file1, file2. cat Reply.xml to file1, then cat file2 to file1. It may not be a good way of doing it, also I don't know the syntax of piping grep and tail. I'd like to know what is the better way.

THX!
 
Old 08-17-2007, 05:42 PM   #4
laurasong
LQ Newbie
 
Registered: Aug 2007
Posts: 9

Original Poster
Rep: Reputation: 0
This might be a stupid question, but not a "homework". I'm trying to modify a script to do file editing. I have two huge xml files. Message.xml is auto generated on the daily basis. Reply.xml is manually created once a week. I need to modify the script to search the key word "connections" in Message.xml and insert Reply.xml after the key word. Thought I could use grep to find "connections" line number then use tail to break Message.xml into two temp files - file1, file2. cat Reply.xml to file1, then cat file2 to file1. It may not be a good way of doing it, also I don't know the syntax of piping grep and tail. I'd like to know what is the better way.

THX!
 
Old 08-17-2007, 05:53 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Untested .... but you may get the idea.
Code:
awk '{print; if ($0 ~ /connections/){ system(cat reply.xml)}}' message.xml

CHeers,
Tink
 
Old 08-17-2007, 06:09 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example of sed which may work for you.
Code:
sed -n '/connections/,$p' Message.xml > Reply.xml
 
Old 08-17-2007, 07:07 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by laurasong View Post
This might be a stupid question, but not a "homework".
I didn't mean to imply that it was a stupid question at all - sorry for any misunderstanding. I simply meant that the specification of those two tools seemed like it might be a contrived problem rather than a 'real world' problem. As you see, neither of the solutions offered use grep and tail but sed and (what I was originally thinking of) awk. However, given that the input is xml, aren't there xslt approaches that would be specifically designed for this sort of thing?

I'm still not precisely following the situation, based on the absence of example input and output. Hopefully one or both of the suggestions help you out.
 
Old 08-19-2007, 11:59 PM   #8
laurasong
LQ Newbie
 
Registered: Aug 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks a lot for reply. I tried to use awk as suggested, somehow it didn't work. Maybe it needs some tweek. I'll give sed a shot tomorrow. I know it won't be a big deal if I can write it in c,or xslt. Since I'm modifying a existing bash script. I don't have much choice, which I should have explained earlie. Sorry about that.

Again, thanks a lot!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto tail -f /var/log/messages | grep isdninfo Mopp Programming 4 07-22-2011 10:00 AM
Weird tail + grep bug Aeiri Linux - Software 2 12-15-2006 05:30 AM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 05:30 AM
Only load last portion of a file, instead of the whole really big text file 1veedo Linux - General 2 02-20-2006 06:34 PM
Help pls!!! grep or tail->restart poj Linux - Software 3 11-26-2004 12:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:09 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