[SOLVED] sed, awk, Keep only text between two regular expressions
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
awk, sed, csplit, extract text body from email or RSS
Greetings - I know this is most likely answered somewhere, but would appreciate some guidance. Not important, just trying to learn more.
I have a directory of saved rss articles, all beginning with IS_*.
Each article has the top fields, then the body text, then a bunch of garbage at the bottome, links, etc.
My goal is to keep everything between the first blank line and a common phrase at the end of the article, and save all the articles to one file.
Sample file:
-----
Title: title
Date: date
Link: link
I want to keep only this text
Each article ends with this text Want podcast of every article?
[1] link
[2] link
[3] and so on
-----
I can get everything to work by hacking away at it, deleting lines that start with [ , etc., but just wondering if there is an easier, more precise way to do it.
for i in $(ls IS_*.txt); do
cat $i | egrep ^Title\: | awk -F": " '{ print $2": " }' | sed 'G' >> is.1
magic sed command to keep the body of the message
done
I've read manuals and google'd, can't seem to find what I need, any guidance would be appreciated.
-Scott
Last edited by scott_audio; 08-05-2009 at 07:16 PM.
Reason: better searching
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
I see your problem comes from not reading man csplit. That comes from not having heard about csplit. If you wish, read posix csplit manual http://www.opengroup.org/onlinepubs/...es/csplit.html instead of GNU csplit manual. I doubt you meant that any POSIX-compliant utility is forbidden..
I have a directory of saved rss articles, all beginning with IS_*.
Each article has the top fields, then the body text, then a bunch of garbage at the bottome, links, etc.
My goal is to keep everything between the first blank line and a common phrase at the end of the article, and save all the articles to one file.
-Scott
could you please copy and paste here a passage from one of your
rss article
and precede the beginning the passage you want to retrieve by
### scott_audio: beginning
and follow the last line of the passage by
### scott_audio: end
It would be easier to figure it out if I can have a look at the
actual content.
Right off the bat it should be possible to do this via a script
shell that loops thru your files and run a sed script on them.
Title: Bubble Tea / The all-in-one beverage and snack
Author: Author
Date: Fri, 24 Jul 2009 01:00:01 -0400
Link: http://link..
[image 1]
### scott_audio: beginning
Several paragraphs here that I want to keep.
One of the great things about spending time in another country is learning
about new and unique foods. When I was living in Vancouver, Canada a few years
ago, I became acquainted with bubble tea,...
### scott_audio: end
Want podcasts of every article? Support ITotD by [becoming a paid subscriber][3]
.
[Permalink][4] [Email this Article][5] Category:[Food & Drink][6]
[image 7] Good karma: priceless. For everything else, theres [PayPal donations][8]
! [[?][9]]
[[image 11]][10]More Information about Bubble Tea...
-----
This is what I have, and it works, I just want to learn how to write it better. Please don't laugh too loudly, I am a newbie
Code:
for i in $(ls $JEFF/IS_*.txt); do
cat $i | egrep ^Title\: | awk -F": " '{ print $2": " }' | sed 'G' >> $JEFF/is.rtf;
sed '/Link: /,/Want podcasts/!d' $i > $JEFF/is.1;
sed '/Link: /d' $JEFF/is.1 > $JEFF/is.2;
sed '/\[image/d' $JEFF/is.2 > $JEFF/is.3;
sed '/./,/^$/!d' $JEFF/is.3 > $JEFF/is.4
sed '/Want podcasts/d' $JEFF/is.4 >> $JEFF/is.rtf;
echo "-----" >> $JEFF/is.rtf;
done
I'll have a look and get back to you.
So it seems the top marker is '[image ...]'
and the bottom marker is
'Want podcasts of every....'
and you want to retrieve only what's in between
so that your output would be
##### beginning of output
Several paragraphs here
...
bubble tea
##### end of output
synss, hi, and thank you very much. Was exactly what I was looking for, a good solid example of what I was trying to accomplish... i made my loop, piped the final through sed '/./,/^$/!d' to take any double blank lines and tr -d '\n' to keep each paragrash one long line, and it worked perfectly. I'm reading up on what the double pipe means || ... looks like, though, it starts two lines down from the line that starts with 'Link:' and prints all the lines until it encounters 'Want podcasts...'
I've learned a lot and had fun, thanks again all
-Scott
#first marker: after date
#keep only day month year
s?Date: *\([^\n]*\) [0-9][0-9]:[0-9][^\n]*?\1º?
#just in case
s?°?\°ree;?g
#second marker: [image ...]
s?\[image [0-9][^\n]*?°?
#keep everything from top to first marker (title, date)
#remove everything between first marker and second marker
s?^\([^º]*\)º[^°]*°[ \t\n]*?\1\n?
#remove everything below the line (including the line, and
# the blank lines that precedes it) that starts with:
s|[ \t\n]*Want podcasts of every article? Support ITotD by .*$||
#remove extra blank lines
s?\n[ \t\n]*\n?\n\n?g
########### end of seder.sed ############
2. test on one of your files: ./seder.sed IS_something.txt
3. If the output on the terminal looks ok, you can then:
a) loop thru all your files
b) redirect the output to a file (./seder.sed IS_something.txt > is_something.rtf)
4. you can write a shell script that does all of this:
loop, apply seder, redirect to a file, eg,
#better put in an absolute path for this
JEFF=fill_in_the_value_here
SEDER=$CWD/seder.sed
find $JEFF/* | while read f
do
#directory
if [ -d $f ]; then
continue
fi
dir=${f%/*}
filename=${f##*/}
ext=${filename##*.}
name=${filename%.*}
#only process file if it is of type IS_something.txt
if [ "$ext" == "txt" ] && [ $(echo "$name" | grep -c '^IS_') -gt 0 ]; then
input="$f"
output="$dir/""$(echo $name | tr '[:upper:]' '[:lower:]')".rtf
$SEDER $input > $output
fi
done
######## end of shell script ##################
vonbiber, thanks, this gives me lots to experiment with, appreciate your time. I'm sure I'll find some more feeds, and I'll be able to apply what I've learned here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.