niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
sed substitute From -> From:
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
Icarus Sparry
*nix forums Guru


Joined: 19 Feb 2005
Posts: 342

PostPosted: Mon Feb 14, 2005 7:01 pm    Post subject: Re: sed substitute From -> From: Reply with quote

On 2005-02-14, Florian Reiser <dy@reiser-familie.de> wrote:
Quote:
Me neither - i'm pretty much new to bash scripting.

More precise is
sed '1,5 s/^From /From: /'

Hey, it did the trick! The Header-lines didn't get deleted.
It couldn't have been the "g" - that just searches the whole file as much as
i know.

No, the 'g' causes ALL the matches on the current line to be changed,
rather than just the first.
Back to top
Florian Reiser
*nix forums beginner


Joined: 14 Feb 2005
Posts: 6

PostPosted: Mon Feb 14, 2005 6:03 pm    Post subject: Re: sed substitute From -> From: Reply with quote

Hi Ed,

Quote:
You're needlessly using cat twice

I had given it a thought ... and just kept it that way, not knowing any
better, due to the e-mail issue.
I must say it was not very performant in any way :)

Quote:
do
sed '1,5 s/From /From: /' "$datei" > tmp
cp tmp "$datei" && rm tmp

Wow, that's a lot faster - of course! Especially when there are a lot of
e-mails!
And it works, just like Michael's solution.
I should have isolated this part of the script and let it run alone - i
guess the other parts of it did the deleting.

Quote:
You might want to use "formail" <...

I thought there must be something like this - normally my problems
concerning e-mails popped up long ago somewhere else.

So thank's a lot again - it saved me from spending a lot more time on such a
little thing.

Greetings,

Florian Reiser
Back to top
Florian Reiser
*nix forums beginner


Joined: 14 Feb 2005
Posts: 6

PostPosted: Mon Feb 14, 2005 5:51 pm    Post subject: Re: sed substitute From -> From: Reply with quote

Hi Michael,


Quote:
I dont know what's wrong

Me neither - i'm pretty much new to bash scripting.

Quote:
More precise is
sed '1,5 s/^From /From: /'

Hey, it did the trick! The Header-lines didn't get deleted.
It couldn't have been the "g" - that just searches the whole file as much as
i know.

Maybe it was the "^". The problem is i have more lines getting deleted
because of other issues and it's a bit difficult getting it sorted out.

Anyhow, thank you for your help!

Florian Reiser
Back to top
Ed Morton
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1073

PostPosted: Mon Feb 14, 2005 1:21 pm    Post subject: Re: sed substitute From -> From: Reply with quote

Florian Reiser wrote:

Quote:
I have a lot of e-mails where the header got messed up (I'm trying to feed
Cyrus-IMAP with those mails and they don't get taken).

One of the problems is that there's a ":" missing in the "From:"-line, like
this:

From xy@domain.xyz
... instead of ...
From: xy@domain.xyz

I wrote a script using sed to substitute "From" with "From:" but instead of
subtituting it DELETES those lines! It ist supposed to subbstitute only in
the first 5 lines.

This is what it looks like:


for datei in *
do

cat $datei | sed '1,5 s/From /From: /g' | cat > tmp
cp tmp $datei && rm tmp

done


Can anyone point me to what's wrong ?

You're needlessly using cat twice and have a couple of other problems.
Try this:

for datei in *
do
sed '1,5 s/From /From: /' "$datei" > tmp
cp tmp "$datei" && rm tmp
done

and let us know what happens.

You might want to use "formail" to fix mail formatting problems instead
of writing your own scripts though. See http://www.procmail.org and
http://www.rt.com/man/formail.1.html for more information.

Ed
Quote:
Thank's a lot,

Florian Reiser
Back to top
Michael Tosch
*nix forums Guru


Joined: 21 Feb 2005
Posts: 539

PostPosted: Mon Feb 14, 2005 1:14 pm    Post subject: Re: sed substitute From -> From: Reply with quote

Florian Reiser wrote:
Quote:
I have a lot of e-mails where the header got messed up (I'm trying to feed
Cyrus-IMAP with those mails and they don't get taken).

One of the problems is that there's a ":" missing in the "From:"-line, like
this:

From xy@domain.xyz
... instead of ...
From: xy@domain.xyz

I wrote a script using sed to substitute "From" with "From:" but instead of
subtituting it DELETES those lines! It ist supposed to subbstitute only in
the first 5 lines.

This is what it looks like:


for datei in *
do

cat $datei | sed '1,5 s/From /From: /g' | cat > tmp
cp tmp $datei && rm tmp

done


Can anyone point me to what's wrong ?

Thank's a lot,

Florian Reiser

I dont know what's wrong but you expression probably can do more than you intended.
More precise is
sed '1,5 s/^From /From: /'
or
sed '1,/^$/ s/^From /From: /'


--
Michael Tosch @ hp : com
Back to top
Florian Reiser
*nix forums beginner


Joined: 14 Feb 2005
Posts: 6

PostPosted: Mon Feb 14, 2005 12:54 pm    Post subject: sed substitute From -> From: Reply with quote

I have a lot of e-mails where the header got messed up (I'm trying to feed
Cyrus-IMAP with those mails and they don't get taken).

One of the problems is that there's a ":" missing in the "From:"-line, like
this:

From xy@domain.xyz
.... instead of ...
From: xy@domain.xyz

I wrote a script using sed to substitute "From" with "From:" but instead of
subtituting it DELETES those lines! It ist supposed to subbstitute only in
the first 5 lines.

This is what it looks like:


for datei in *
do

cat $datei | sed '1,5 s/From /From: /g' | cat > tmp
cp tmp $datei && rm tmp

done


Can anyone point me to what's wrong ?

Thank's a lot,

Florian Reiser
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 12:43 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts How to substitute everything but something? Eric.Medlin@gmail.com Perl 7 Wed Jul 19, 2006 6:32 pm
No new posts Single-liner for one-line substitute? Mike Pearson Perl 13 Wed Jun 28, 2006 9:38 am
No new posts Hyper Terminal Substitute dave Suse 8 Tue Jun 27, 2006 4:38 am
No new posts URGENT: Samba Won't Substitute %u In "Logon *" When LDAP ... Brian White Debian 1 Mon May 22, 2006 8:30 pm
No new posts Substitute for floatint point Sathyaish C 7 Mon May 08, 2006 7:11 am

Mortgage Calculator | Credit Counseling | Debt Consolidation | Web Advertising | Mortgage Calculator
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1684s ][ Queries: 20 (0.0791s) ][ GZIP on - Debug on ]