LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-25-2009, 12:11 PM   #1
belbono
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Rep: Reputation: 0
delete/filter parts of file


Hi,

this already keeps me busy for some hours...

I'm trying to delete/filter some parts of a file.
Used sed an grep so far.

My file looks like this:

Code:
[TOPIC]
par1
par2

[NEXTTOPIC]
par3
par4
My problem is extracting par1 and par2 so that I only have a list of these two
Code:
par1
par2
I tried the following

Code:
cat filename | grep [a-z_0-9]
to get rid of the lines with the uppercase letters and square brackets.

By this I get some output where the correct lines a marked in red, but the [TOPIC] line, which does not match the grep-pattern and not colored, is still included in the output.

Any idea how to get this working?

Last edited by belbono; 02-25-2009 at 12:17 PM.
 
Old 02-25-2009, 12:48 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
sed -n -e '/[TOPIC]/,/\[/p'  | sed -e '$d' -e '1d'
 
Old 02-25-2009, 12:50 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Not sure I understand what you want. Does this do what you want?
Code:
grep par[1-2] filename > filename2
 
Old 02-25-2009, 12:58 PM   #4
belbono
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
Code:
sed -n -e '/[TOPIC]/,/\[/p'  | sed -e '$d' -e '1d'
thanks

I solved the problem by using
Code:
sed -n "/^\s*\[\s*$nml\s*\]/,/^$/p" $verFile | grep -v "^\s*\["
but I don't like my grep syntax, because its too restrictive.

Your command looks better than mine. Can you explain me what exactly
Code:
sed -e '$d' -e '1d'
does? The man page won't help me in this..
@weibullguy as you see it wasn't quite that what I wanted, but thanks a lot for helping

Last edited by belbono; 02-25-2009 at 01:00 PM.
 
Old 02-25-2009, 01:00 PM   #5
belbono
LQ Newbie
 
Registered: Feb 2004
Posts: 15

Original Poster
Rep: Reputation: 0
sorry doublepost...delete plz

Last edited by belbono; 02-25-2009 at 01:33 PM.
 
Old 02-25-2009, 02:04 PM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
sed -n -e '/[TOPIC]/,/\[/p' | sed -e '$d' -e '1d'

Example: sed '1,5p' will print lines 1-5, plug in regular expressions in for the 1 and 5 and you have

/[TOPIC]/,/\[/p

This show the text between the text [TOPIC] and the first '[' it runs into, but this displays those lines as well.

Code:
[TOPIC]
par1
par2

[NEXTTOPIC]
This is where the second sed comes in.

sed -e '$d' -e '1d'

$d deletes the last line
1d deletes the first line


Code:
par1
par2
 
  


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
Repeat parts of audio/video file fw12 Linux - Software 1 09-01-2008 02:48 PM
NFS for which parts of file tree? sniff Linux - General 3 04-09-2007 04:08 PM
Copying parts of a text file austen77 Linux - Newbie 3 09-01-2005 10:56 PM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 03:46 AM
Divide a file into 2 parts bmbsa Linux - Newbie 3 08-13-2003 05:08 PM


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