LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-03-2013, 07:25 AM   #1
trijit
Member
 
Registered: Sep 2010
Location: Kolkata
Distribution: Ubuntu
Posts: 35

Rep: Reputation: 3
Cutting out a particular region of a file


Hi All,

I have a file where I need to cut a specific set of lines. For example my file contains:
========
The quick
brown fox
jumps
over
the lazy
dog
========

Now I am providing 2 strings "brown fox" and "the lazy ". So, I should get a output like:
========
jumps
over
========

Any help would be appreciable.

Thanks,
Trijit
 
Old 07-03-2013, 07:32 AM   #2
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
What about 'The quick' and 'dog'? where do they go? Your question is ambiguous.

If you dont want 'brown fox' and 'the lazy' - then you get 'The quick', 'jumps',
'over' and 'dog' as results.

But if want only 'jumps' and 'over' then grep for only these two.
 
Old 07-03-2013, 08:18 AM   #3
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
OK, now i got it. just googled, this is what i got:

Code:
sed -n '1,/brown fox/d;/the lazy/q;p' file
From: http://stackoverflow.com/questions/1...-sed-start-end

Last edited by mddnix; 07-03-2013 at 08:19 AM.
 
1 members found this post helpful.
Old 07-03-2013, 09:17 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Alternatively you can try awk:
Code:
~$ awk '/brown fox/ {i=1;next} /the lazy/ {exit} i' infile.txt
You can use this to print file content between any two given patterns.
 
Old 07-08-2013, 05:34 AM   #5
trijit
Member
 
Registered: Sep 2010
Location: Kolkata
Distribution: Ubuntu
Posts: 35

Original Poster
Rep: Reputation: 3
Smile

Quote:
Originally Posted by mddesai View Post
OK, now i got it. just googled, this is what i got:

Code:
sed -n '1,/brown fox/d;/the lazy/q;p' file
From: http://stackoverflow.com/questions/1...-sed-start-end
Thanks a lot. I was able to do it by sed.
 
  


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
Need some help cutting a file up jakev383 Linux - General 10 10-24-2009 09:40 PM
how to identify the frame buffer update region or find a hook for the updated region sanjeevsk Linux - Software 0 08-08-2007 12:43 AM
cutting the last N characters form a file name Rindert Linux - General 20 08-18-2006 07:38 AM
MPlayer cutting off the start of each file nutthick Linux - Software 3 04-26-2006 01:40 PM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 02:16 AM


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