head buried in sed

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

head buried in sed

#1 Post by 01micko »

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
Puppy Linux Blog - contact me for access

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#2 Post by dejan555 »

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

Make backup of file before editing?
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#3 Post by 01micko »

Hi Dejan,

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

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: Select all

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
Puppy Linux Blog - contact me for access

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#4 Post by dejan555 »

That's a good tutorial :D
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 :lol: Maybe he clarifies it for us.

Anyway, why not make backup of file before sed command with cp?
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#5 Post by 01micko »

hi Dejan :)

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
Puppy Linux Blog - contact me for access

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#6 Post by amigo »

No, it means 'exec'. It lets you do stuff like this:

Code: Select all

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

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#7 Post by 01micko »

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. :)
Puppy Linux Blog - contact me for access

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#8 Post by dejan555 »

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/ ... -file.html
Thise one has interesting examples and some in reader comments too.
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#9 Post by amigo »

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...

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#10 Post by zigbert »

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. :)
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 :D

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

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: Select all

sed -i "s/sed -ie/sed -i/g"
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
prehistoric
Posts: 1744
Joined: Tue 23 Oct 2007, 17:34

sed scripts

#12 Post by prehistoric »

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

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#13 Post by amigo »

"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'.

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#14 Post by 8-bit »

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.

User avatar
prehistoric
Posts: 1744
Joined: Tue 23 Oct 2007, 17:34

Comments

#15 Post by prehistoric »

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: Select all

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.

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#16 Post by 8-bit »

Did you notice I said section and not line?
A section of code in a program would be commented as to what you expect it to do.
There is no advantage to commenting lines.
Also a line that tries to combine a lot is not my idea of fun.
Especially when it comes to debugging.
I try to keep from combining a bunch of actions on one line.

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

Re: Comments

#17 Post by 01micko »

prehistoric wrote:The trick is to make them (comments) 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.
Lately that is how I have been planning a script. Write the comments into a logical order then write the code, specially if there is something new I've learned and may not revisit for awhile.

My current sig aroused some interest...

You may have understood the script perfectly when you wrote it. But six months from now it could look like modem noise.
Bruce Barnett (I put here for future ref, I will eventually change my sig again :wink: )

Good healthy discussion on comments and when/how to use them. Still learning, and it's not all about code. :)

eg

Code: Select all

#need something to append "&" to every line
sed -i 's/$/ \&/' /path/to/file
Cheers
Puppy Linux Blog - contact me for access

aragon
Posts: 1698
Joined: Mon 15 Oct 2007, 12:18
Location: Germany

#18 Post by aragon »

technosaurus wrote: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.
tr does a good job for this kind e.g.

Code: Select all

tr "\n" "NEWCHAR" 
aragon

bill
Posts: 490
Joined: Wed 28 May 2008, 15:32

remarks and comments in code etc.

#19 Post by bill »

Just my two cents but I see it like Prehistoric,and I think remarks are not only for the original programmer but for anyone whom you may share your work with.Isn't this what Puppy is all about ? I'm thinking also that remarks can help some kind soul who wishes to lend a hand on something you may not be able to get quite right.I know this is old school thinking and I certainly fit that category.(old) :? cheers

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

Re: head buried in sed

#20 Post by DMcCunney »

01micko wrote: 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?
sed is the Unix "stream editor", intended to be used in scripts to make unattended edits on files.

By default, sed expects to be used in "one liners", where you issue the sed command, the editing commands, and the file(s) you want sed to process on the command line. This is fine for simple edits, but some things you want to do may be more extensive. and require multi-line sed scripts read from an external file.

That's what -e is for. After "sed -e", sed expects the name of an editing script, like "sed -e foobar.sed <input file>"

sed offers the full range of editing features of the ed line editor, including regular expressions and branching operations. When you run sed on a file, sed reads each line of the input file in turn and runs the editing script on it, then writes out the line and reads the next.

When it has processed all lines in the file, sed exits, and control passes to whatever called it.

.The canonical home for sed is on SourceForge, and much good information may be found there: http://sed.sourceforge.net/
______
Dennis

Post Reply