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

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

#21 Post by DMcCunney »

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.
It is a lineS editor. Run sed on a file, and it reads each line of the file and runs the edit commands/script you passed to it on the line, then reads the next. When it's processed all lines, it exists.

As for changing line endings:

Code: Select all

TEXT CONVERSION AND SUBSTITUTION:

 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 sed 's/.$//'               # assumes that all lines end with CR/LF
 sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M
 sed 's/\x0D$//'            # works on ssed, gsed 3.02.80 or higher

 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$/`echo -e \\\r`/"            # command line under ksh
 sed 's/$'"/`echo \\\r`/"             # command line under bash
 sed "s/$/`echo \\\r`/"               # command line under zsh
 sed 's/$/\r/'                        # gsed 3.02.80 or higher

 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$//"                          # method 1
 sed -n p                             # method 2

 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
 # UnxUtils version can be identified by the custom "--text" switch
 # which appears when you use the "--help" switch. Otherwise, changing
 # DOS newlines to Unix newlines cannot be done with sed in a DOS
 # environment. Use "tr" instead.
 sed "s/\r//" infile >outfile         # UnxUtils sed v4.0.7 or higher
 tr -d \r <infile >outfile            # GNU tr version 1.22 or higher
from http://sed.sourceforge.net/sed1line.txt
______
Dennis

Post Reply