LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-02-2007, 05:49 PM   #1
justin99
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Rep: Reputation: 0
bash script- text file manip- delete everything before/after string


I need a bash script to eliminate junk from the beginning and end of a text file.

can i search a file for 'good stuff begins', deleting each line until that string, then again, deleting each line after 'good stuff ends'

or- perhaps it is possible to copy everything from 'good stuff begins' to 'good stuff ends' to a new file?

tia

Last edited by justin99; 12-02-2007 at 05:50 PM. Reason: added 'bash'
 
Old 12-02-2007, 06:00 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
You can use sed to do that. There's a tutorial at http://www.grymoire.com/Unix/Sed.html which you might find useful. The following example removes comments (lines that start with #) between the 2 lines containing 'start' and 'stop':
Code:
sed '/start/,/stop/ s/#.*//'
 
Old 12-02-2007, 08:48 PM   #3
justin99
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
so close

thanks for the help- i'm very close now, but still no cigar.

sed '1,/<START>/ d' temp.html > temp1.html

this works great, removing everything from line 1 until <START>

but i'm stuck on removing everything after <END>
 
Old 12-02-2007, 08:57 PM   #4
justin99
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
Talking got it

turns out the ! is what i needed

this deletes everything but the selected region

sed '/<START>/,/<END>/ !d' temp.html > temp1.html



why did i waste my time googling- shoulda come here first (- ;


thanks again!
 
Old 12-02-2007, 10:21 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
I'm glad you got it working. Thanks for posting the code you ended up using too...
 
Old 05-15-2009, 03:41 PM   #6
skkuizu
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
Question similar issu with sed and the bash

My problem however that I want to remove/delete all character before the START variable and also all character following the END variable.

However START and END are not on the beginning of a line they are in the middle and furthermore in the same line similar to that example:

t6gd68g d9d8j5%9j30j 0jf087*(&&^*2hd920STARTid8 =e72920 2d9nf9END93nf300j90

How to I get the output between START and END, resulting in id8 =e72920 2d9nf9 for this example. I can't find anything on the net so far.

Happy for every idea.

Cheers.
 
Old 05-15-2009, 03:51 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
If you just have one line (or a variable you want on every line) then:

Code:
sed -e 's/.*START//' -e 's/END.*//'
HTH

Forrest
 
Old 05-15-2009, 04:09 PM   #8
skkuizu
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
Smile Thanks

Quote:
Originally Posted by forrestt View Post
If you just have one line (or a variable you want on every line) then:

Code:
sed -e 's/.*START//' -e 's/END.*//'
HTH

Forrest

Forrest, that's great it works! It's so easy that I can't believe it, I should have come straight to this forum, instead I was wasting my time with google for 24h Many thanks again!!!
 
Old 05-15-2009, 08:40 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by skkuizu View Post
instead I was wasting my time with google for 24h Many thanks again!!!
instead of searching for the exact solution, what you really need to do is to search for a tutorial on shell scripting and read up on the basics.
 
Old 11-20-2014, 04:16 AM   #10
Sergiof4
LQ Newbie
 
Registered: Nov 2014
Distribution: Mint, Ubuntu, Ubuntu MATE
Posts: 1

Rep: Reputation: Disabled
Hello,

hope it is OK to re-open this (very useful, imho) thread.

I need to clean some html pages and the "region" i have to keep is between the <article></article> tag.

I tried using

Code:
sed '/<article>/,/</article>/ !d' a.htm > b.htm
but it didn't work.

Thank you.
 
  


Reply

Tags
awk, bash, grep, script, 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
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 04:33 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 01:38 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 03:36 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 09:51 PM


All times are GMT -5. The time now is 02:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration