LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to use grep and tail to get portion of a file (https://www.linuxquestions.org/questions/linux-general-1/how-to-use-grep-and-tail-to-get-portion-of-a-file-577871/)

laurasong 08-17-2007 03:35 PM

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,

slakmagik 08-17-2007 03:52 PM

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?

laurasong 08-17-2007 05:34 PM

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!

laurasong 08-17-2007 05:42 PM

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!

Tinkster 08-17-2007 05:53 PM

Untested .... but you may get the idea.
Code:

awk '{print; if ($0 ~ /connections/){ system(cat reply.xml)}}' message.xml

CHeers,
Tink

homey 08-17-2007 06:09 PM

Here's an example of sed which may work for you.
Code:

sed -n '/connections/,$p' Message.xml > Reply.xml

slakmagik 08-17-2007 07:07 PM

Quote:

Originally Posted by laurasong (Post 2862493)
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.

laurasong 08-19-2007 11:59 PM

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!


All times are GMT -5. The time now is 10:26 AM.