LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2008, 04:21 AM   #1
Hisu
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Rep: Reputation: 0
[sed || gawk]: find and delete blocks and lines from file


I have a file named foo.bar
It consists of something like this:

[foo1]
barbarbar=bbaar
barbar=bbarr
bar=baarr
[foo2]
barbar=bbaarrbbaarr

I need to locate and remove either anything from [foo1] to [foo2], OR barbar= ONLY in block [foo2]. How do i do this by sed or awk?
 
Old 09-16-2008, 02:01 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Code:
jan@jack:~/tmp/barbar> cat data
[foo1]
barbarbar=bbaar
barbar=bbarr
bar=baarr
[foo2]
barbar=bbaarrbbaarr
bar=baarr
[foo3]
barbar=bbaarrbbaarr

jan@jack:~/tmp/barbar> sed '/^\[foo2\]/,/^\[foo[^2]\]/{/^barbar=/d};/^\[foo1]/,/^\[foo2\]/{/^\[foo2\]/p;d}' data
[foo2]
bar=baarr
[foo3]
barbar=bbaarrbbaarr

jan@jack:~/tmp/barbar> awk ' /^\[foo1\]/ { del = 1; }
>       /^\[foo2\]/ { del = 2; }
>       /^\[foo[^12]\]/ { del = 0; }
>       /^barbar=/  { if (del > 0) getline; }
>       { if (del != 1) print; } ' data
[foo2]
bar=baarr
[foo3]
barbar=bbaarrbbaarr
Jan
 
  


Reply

Tags
sed


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 grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
Sed: how to delete all lines except those matching........ DX398 Programming 9 07-23-2008 02:39 AM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:47 AM.

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