Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 17 Sep 2014, 01:33
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
head buried in sed
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Author Message
01micko


Joined: 11 Oct 2008
Posts: 7801
Location: qld

PostPosted: Mon 11 Jan 2010, 03:25    Post subject:  head buried in sed
Subject description: sed options
 

Hi there

I have had my head buried in sed man pages and tutorials today. I did have basic concept of sed but I was doing things parrot fashion, anyone can copy stuff and achieve a desired result. Today I decided to see what I could do with out copying the work of others.

Ok, I'm progressing nicely, actually wrote some sed code and knew what the result would be. Good, it's starting to sink in.

A query I have is of zigbert's work. If you read this ziggy maybe you could answer but if you don't it doesn't matter, I'm sure someone else will.

In a lot of zigbert's programs sed is used, mainly to replace text in config files or scripts on the fly, much of the time using "sed" with "-ie" option. Now as I understand it the "-i" option reads the target file, finds the match, makes a backup in memory, removes the old string or char if specified, and replaces or adds with the new string or chars, deleting the backup. But a suffix can be added to "-i" to keep the backup... this is where my brain is failing me.. I can't get a handle on the suffix bit.

Now in zigbert's code the suffix is usually "e"... "e" is another sed option but I am not sure of how this works. Until I do know how this works I am leaving the e out of my code.

Any body know about this one?

Cheers

_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send private message Visit poster's website 
dejan555


Joined: 30 Nov 2008
Posts: 2648
Location: Montenegro

PostPosted: Mon 11 Jan 2010, 03:52    Post subject:  

Seems that -e is for specifying multiple commands:
http://www.grymoire.com/Unix/Sed.html#uh-13

Make backup of file before editing?

_________________


Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
01micko


Joined: 11 Oct 2008
Posts: 7801
Location: qld

PostPosted: Mon 11 Jan 2010, 04:07    Post subject:  

Hi Dejan,

That link you gave is the one in my current sig. Laughing

I don't think it's the same thing because if you see Sigmund's code where it is used it is only one command.

Code:
sed -ie 's%#!/bin/sh%#!/bin/sh\n/usr/local/pshutdownsound/sound #Stardust%' $MOUNT_PATH/usr/bin/wmpoweroff


(from stardust-004 thread)

There are many examples in Pwidgets too.

Cheers

_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send private message Visit poster's website 
dejan555


Joined: 30 Nov 2008
Posts: 2648
Location: Montenegro

PostPosted: Mon 11 Jan 2010, 04:53    Post subject:  

That's a good tutorial Very Happy
I started learning sed from it myself, haven't gone far from basic options though, even it really has bunch of them. I also found another good blog post with practical example of manipulating text files with sed and sometimes in combination with other commands but lost link.

Not sure what could else -e be, maybe zigbert just got used to have it in script so he puts it there anyway Laughing Maybe he clarifies it for us.

Anyway, why not make backup of file before sed command with cp?

_________________


Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
01micko


Joined: 11 Oct 2008
Posts: 7801
Location: qld

PostPosted: Mon 11 Jan 2010, 06:05    Post subject:  

hi Dejan Smile

Now, I must agree that the tutorial is good and it is well maintained, last update was only last year... sed has been around for over 20 years.

I think the "e" in zigbert's code is a suffix.. it is explained on the sed manpage.. http://www.gnu.org/software/sed/manual/sed.html

I don't know if it is a backup option, but if it is, where is the backup?

Backing up with cp is of course an option, but if you are running a script in conjunction with gtkdialog and you need a sed call, it would be better to use the internal commands than relying on an extra bash call, especially if running something on the fly. I am trying to code efficiently, as we all do. Also, if your code is bullet proof, then there is not the need for a backup in this instance.

Thanks for your ideas, anyone else know? Or at least got an idea?

Cheers

_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2247

PostPosted: Mon 11 Jan 2010, 06:48    Post subject:  

No, it means 'exec'. It lets you do stuff like this:
Code:
sed -ie 's%stuff%no-stuff%' \
 's%more-stuff%less-stuff%'


To use a backup suffix, you'd do this:
sed -i .bkp ... (notice the space between -i and the suffix)
or:
sed --in-place=bkp ...

Using -ie all the time doesn't hurt anything, even if onyl one substitution is being specified.
For backups, you could always do things the old-fashioned way -like older sed versions had to be used:
cp file file.bkp
sed -ie 's%stuff%no-stuff%' file.bkp > file
Back to top
View user's profile Send private message 
01micko


Joined: 11 Oct 2008
Posts: 7801
Location: qld

PostPosted: Mon 11 Jan 2010, 07:00    Post subject:  

Hi amigo

Thanks for that explanation.

That possibility had entered my mind, so now I know.

Cheers

EDIT: there will be more questions on sed... I would like to keep this discussion going. Smile

_________________
Woof Mailing List | keep the faith Cool |
Back to top
View user's profile Send private message Visit poster's website 
dejan555


Joined: 30 Nov 2008
Posts: 2648
Location: Montenegro

PostPosted: Mon 11 Jan 2010, 07:56    Post subject:  

Yes -e is for exec but it's not neccessary when having one task like Mick gave example I think.

http://linuxcommando.blogspot.com/2008/03/using-sed-to-extract-lines-in-text-file.html
Thise one has interesting examples and some in reader comments too.

_________________


Back to top
View user's profile Send private message Visit poster's website MSN Messenger 
amigo

Joined: 02 Apr 2007
Posts: 2247

PostPosted: Mon 11 Jan 2010, 11:17    Post subject:  

I avoid sed like the plague, except for the simplest replacements like you asked about. regex's make my head spin too much, so any fancy work with sed makes me go gaga...
Back to top
View user's profile Send private message 
zigbert


Joined: 29 Mar 2006
Posts: 5719
Location: Valåmoen, Norway

PostPosted: Mon 11 Jan 2010, 11:45    Post subject:  

Quote:
If you read this ziggy maybe you could answer but if you don't it doesn't matter, I'm sure someone else will.
As always, I'm too late. Smile
Quote:
Using -ie all the time doesn't hurt anything, even if onyl one substitution is being specified.
I got many bad habbits, some much worse than this Very Happy
_________________
Stardust resources
Back to top
View user's profile Send private message Visit poster's website 
technosaurus


Joined: 18 May 2008
Posts: 4351

PostPosted: Mon 11 Jan 2010, 17:39    Post subject:  

I never could figure out how to easily replace an existing "new line" \n with a different character |,\t(tab) .... probably because it is a LINE editor and that would make it a lineS editor.

Fortunately the replace function in Geany easily does this as well as most of the stuff that is hard to do in sed and vice-versa. Probably not as good if you are developing a bash script to automate something, but passable if you are just data-mining the bits that you need.

For this I use geany for the parts that are exactly the same throughout and sed for the areas where I want just any non-specific string[a-zA-Z]*, number[0-9]* etc... and want to append, prepend, iterate or swap positions...

I usually do first without the -i to make sure I am not going to bork my file (, but I think there is a parameter that will do an automatic backup)

It is also pretty handy to use sed in combination with find -execI use it often to replace all of the builtin -O[0-3] with -Os in *akefile or *.mk

you could always leave the sed -ie in your code and later remove the e
Code:
sed -i "s/sed -ie/sed -i/g"

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
prehistoric


Joined: 23 Oct 2007
Posts: 1271

PostPosted: Mon 11 Jan 2010, 20:22    Post subject: sed scripts  

amigo wrote:
I avoid sed like the plague, except for the simplest replacements like you asked about. regex's make my head spin too much, so any fancy work with sed makes me go gaga...


I can tolerate regular expressions when they meet the definition of regular expressions in formal language theory. The addition of state dependence means you have to have a very good understanding of how the pattern matcher works, which defeats the idea of regular expressions as abstractions.

The main reason for using sed is that it has been around since the mid 1970s, so you find it in just about any descendant of Unix. When it works, it is compact and fast.
--------
"You may have understood the script perfectly when you wrote it. But six months from now it could look like modem noise." Bruce Barnett
--------
This is a real hazard for maintenance programmers. Always include a comment explaining what the script is supposed to do. Keep scripts short, and, please, don't use bizarre tricks that depend on details of the sed pattern matcher. Don't strive for the shortest script at the expense of clarity.

This is close to being a write-only language. But, then I feel the same way about Perl because I am

prehistoric
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2247

PostPosted: Tue 12 Jan 2010, 14:27    Post subject:  

"You may have understood the script perfectly when you wrote it. But six months from now it could look like modem noise." Bruce Barnett

That's the real practical reason that I avoid fancy one-liners of al kinds -because I maintain a large collection of code, I don't want to be having to study what I myself wrote a few days/weeks/years ago to know what it is trying to do.

sed doesn't work for well for replacing end-of-line chars exactly beacuse it is a line editor. There are a couple of programs whcih may do better and make nice replacements for sed -as far as simple replacement goes -one is called 'replace' and the other is 'rpl'. they both have very straightforward usage:
replace old-word new-word file-name
Another problem with fancy usage of sed is compatibility between versions. What works 'here' may well not work 'elsewhere'.
Back to top
View user's profile Send private message 
8-bit


Joined: 03 Apr 2007
Posts: 3368
Location: Oregon

PostPosted: Tue 12 Jan 2010, 18:31    Post subject:  

As to remembering what a section of code does, that is why one should comment written code as to what it is to do.
If one examines some of Barry's code, you will see that he uses comments a lot.
And I for one, knowing nothing of using sed, went to the link concering sed and printed a pdf file of it for offline use in learning it.
Back to top
View user's profile Send private message 
prehistoric


Joined: 23 Oct 2007
Posts: 1271

PostPosted: Tue 12 Jan 2010, 21:41    Post subject: Comments  

8-bit wrote:
As to remembering what a section of code does, that is why one should comment written code as to what it is to do...
One problem with comments is that they often don't get written because they aren't necessary to make the machine work. And, when they are written after the fact, they often turn out to be like this familiar howler:
Code:
n++;  /* Increment count*/
The trick is to make them an aid to development, and write them first. Write pseudo-code to be executed by the human who writes the detailed code, even if this is yourself.

A deliberate effort to describe the algorithm at a higher level of abstraction pays off. Do this well, and you can reuse the comments when you fix the code so it actually does what you intended. If you keep having to change descriptive comments every time you alter the code, you are operating at too low a level of abstraction.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0991s ][ Queries: 12 (0.0061s) ][ GZIP on ]