LinuxQuestions.org
Register a domain and help support LQ
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 10-26-2010, 08:19 AM   #1
vrusu
LQ Newbie
 
Registered: Oct 2010
Posts: 6

Rep: Reputation: 0
sed/awk : remove section from file


Hi
I have a file like this:

[first]
a
b
c
[sec]
d
e
f
[n-th]
x
y
z

I would like to remove a section between [first] and [second]:
$cat test |sed '/\[first\]/,/\[sec\]/d'
d
e
f
[n-th]
x
y
z
Is almost what I want. I would like the section header to say in place.
Desired output:
[sec]
d
e
f
[n-th]
x
y
z

Thank you
 
Old 10-26-2010, 08:38 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 543Reputation: 543Reputation: 543Reputation: 543Reputation: 543Reputation: 543
Code:
 awk  '{
if($0 ~ /\[first\]/){output="off"; next}
if($0 ~ /\[second\]/){output="on"; print; next}
if(output == "on"){print}
}' input-file
The above is an untested, but rather simple, way of (I believe) printing what you wish. If it doesn't work, and/or if you would like explanation of it, please explain or ask more.

There are myriad ways of accomplishing this so I look forward to seeing some other methods too.

Good luck!

EDIT: See the bold part - I fixed it.

Last edited by GrapefruiTgirl; 10-26-2010 at 08:47 AM. Reason: EDIT: Noticed the regex didn't work. Fixed now. See the bold part. Oh, and printed the second header too.
 
1 members found this post helpful.
Old 10-26-2010, 08:46 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Code:
sed '/sec/,$!d' input_file
 
1 members found this post helpful.
Old 10-26-2010, 08:49 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 543Reputation: 543Reputation: 543Reputation: 543Reputation: 543Reputation: 543
Ahh, friend grail, with another amazingly short working solution! Nice one! If we ever have a contest for "the most code written to perform a task", I'll get that one.
 
  


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
SED or AWK - remove every 4 of 5 new lines Mallardle Linux - Newbie 6 08-30-2010 07:44 AM
[SOLVED] sed or awk help - need to remove text on each line before a regular expression kmkocot Linux - Newbie 15 10-30-2009 03:20 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM
How to remove everything before the first space in Sed or Awk OutThere Linux - General 1 04-05-2009 10:45 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM


All times are GMT -5. The time now is 10:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration