Friday, January 11, 2013

Why I’m a Neanderthal, and You Could Be Too, courtesy of Altucher Confidential

I haven't gotten my 23andme results yet.  Yikes!  I like his columns, maybe you will, too.

Why I’m a Neanderthal, and You Could Be Too

My rating
I’m forgetting a lot of things today and it’s causing me to panic. But more on that later. I just got my results from 23andme.com.

(23andme started by Sergey Brin’s wife, Anne Wojcicki)
Do you know the site?
Here’s what I had to do. They shipped this vial to me and I had to spit into it until it was half full. You ever try spitting for five minutes. There’s something sexually unpleasant about it.
Then I had to put the vial in a carefully sealed box. I live in New York State which apparently is the one state in this great republic we live in where you it is illegal to do any sort of genetic testing. What’s up with that Andrew Cuomo? Do you have something to hide? I can’t mail this vial from New York.
So I had to give the box to a friend of mine, Brian, who lives in New Jersey. “I don’t want to give it to him,” I told Claudia the week before. “Why not?” she said, “He’ll just put it in his mailbox.” “He’s going to forget about it,” I said, “and then he’s going to feel pressure to do it and then will feel bad and I don’t want anyone to feel bad.”
Brian came over and Claudia gave him the box. “Why is it sealed wrong?” he said. There were two flaps that were supposed to be tucked under but they were sticking out. The box now looked like this weird paper airplane. Claudia made her usual joke, “James went to get a PhD in computer science and now he can’t seal a box.”
Brian, to his credit, just nodded his head and said, “Got it.” And took the box. A few weeks later he wrote me and said, “Dude, I’m sorry I havent’ sent it yet. I’m putting it in the mail today.”
On Christmas [the day I am writing this] I woke up to a nice surprise with an email saying the results were in. I could now know everything about my genetics. What sort of shit my mom and dad left me in their twisted DNA.
A few months ago I met with a friend of mine who had his genes tested. I asked him what the number one thing he learned was. He said, “I learned that my father is my real biological father.” I was surprised that this was news to him. “Apparently a lot of people cheated on each other,” he said, “and these genetic results are showing people the truth.”
But I did learn that my father’s ancestry is either Bedouin or an Ashkenazi Jew. Or both. I almost like thinking of myself as Bedouin. Like Omar Sharif in Lawrence of Arabia. Either way, my ancestors are either from North Africa or the Middle East.
My mother’s ancestry surprised me. She is all northern Europe. Hmmm. Most people with her “haplogroup” are Scandinavian. I wonder if this means I should get blue contact lenses. I wonder if she knows she’s Scandinavian instead of Jewish.
I’m looking at this in realtime as I write this. They give me three warnings before they let me see the Alzheimer’s information. Ok, Ok, I get it. Most people are wimps and are afraid to find out.

(am I related to Miss Scandinavia? She actually looks like a younger version of my mom a little).
Apparently between the ages of 50 and 79 I have a 14.2 out of 100 chance of being diagnosed with Alzheimer’s as opposed to a normal 7.2 out of 100. Apparently one of my parents was kind enough to give me an APOE 4 gene (the other gave me an APOE 3 gene) which is associated with twice the risk of getting Alzheimers.  Big deal. There are so many potential cures out there and I still have another 35 years to go before 79. And I think I can handle the odds of 86 out of 100 that I won’t be diagnosed. Good for me. In the community section on 23andme there are now support groups for people with the APOE4 gene.
Moving on.
Parkinson’s. Again with the warnings. Ok, I’m at the upper end of average. Average is 1-2% of people get it. I’m at 1.9%. Ok, good. I’ll exercise a lot to get that extra dopamine kicking in.
The disease I’m most likely to get based on my genetics is Type II Diabetes. I have a 38 out of 100 chance of getting it when the average is about 25 out of 100. But there are environmental factors which include obesity so I don’t think I will get it. That’s about it. Oh, I have an increased risk of glaucoma. Which once again proves my dad is my dad. He had it, his mother had it, her mother had it. And so on. We’re all good there.
So the worst that can happen to me is that I go blind, lose my memory, lose my ability to manufacture dopamine, and have a heart attack and/or strike (thanks to the imminent diabetes). The best that can happen to me is that maybe I can call myself a Bedouin. Or a North African. I like that.
Good news for my two daughters: I don’t carry any mutations for breast cancer (Ashkenazi Jews are apparently at higher risk of this). Nor am I a carrier for “Zellweger Syndrome Spectrum” which must be some predisposition to like all movies with Renee Zellweger. No wonder I didn’t like Brigid Jones Diary despite so many others of European descent loving the movie.

(genetically disposed to not like her)
Uh oh. Bad news. I have 3 times the risk of becoming a heroin addict. Which is too bad because I was thinking if I was ever in great pain for something then morphine or heroin would be my drug of choice. Hmmm. Have to file that one away. Can I at least take Percocet? It doesn’t say.
Here’s where they are completely wrong. Apparently I’ve inhererited an ability to “effectively learn from errors”. Hahaha. “People with the GG genotype learned to avoid choices associated with negative feedback relatively easily.” When the Alzheimer’s kicks in it will probably be harder for me, though, so I guess it balances out.
On a side note, ever since I got these test results, I’ve been forgetting things. I wrote to a friend of mine, “maybe I need to write an “Apo4e Cookbook” for people with this gene issue” and she wrote back, “It will be a bestseller because everyone will forget they already own it.” I like that positive way of looking at things.
I got excited for a second. 2.5% of my genome comes from Neanderthals. At last! I knew I wasn’t human. Oh wait. Unfortunately 2.5% is average. We killed them and raped them back in the day and all of us are basically 2.5% Neanderthal.
My kids will probably be surprised to find I have no risk of Tourette’s Syndrome. A lot of times when I randomly think of something embarassing that happened to me in the past, even from 30 years ago, I’ll shout out “No!” by accident. They’ve had to explain to their friends that, “Daddy does this sometimes” and Claudia has gotten used to it. But apparently this is all me, and not from my genes.
Two other things that could affect my life. It turns out I am not likely to be a very good sprinter. I have no working copies of the ACTN3 genotype found in many world-class sprinters. So if a mugger is chasing me my best bet is to just give him all my money and not try to outrun him. I kind of suspected this one already.
Finally, I am genetically disposed to have a higher sensitivity to detecting the smell of aparagus in my urine after eating asparagus. This is true. Thank you 23 and me and God Bless You All.

Why I’m a Neanderthal, and You Could Be Too Altucher Confidential

'via Blog this'

Sed One-Liners: File Spacing, Numbering and Text Conversion and Substitution - thanks to Peteris Krumins at good coders code, great reuse

Real programmers would do well to memorize these; even the first few are pretty cool!

Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution

...


1. File spacing.

1. Double-space a file.
sed G
This sed one-liner uses the 'G' command. If you grabbed the cheat-sheet you'd see that the command 'G' appends to the pattern space. It appends a newline followed by the contents of hold buffer. In this example the hold buffer is empty all the time (only three commands 'h', 'H' and 'x' modify hold buffer), so we end up simply appending a newline to the pattern space. Once all the commands have been processed (in this case just the 'G' command), sed puts the contents of pattern space to output stream followed by a newline. There we have it, two newlines -- one added by the 'G' command and the other by output stream. File has been double spaced.
2. Double-space a file which already has blank lines in it. Do it so that the output contains no more than one blank line between two lines of text.
sed '/^$/d;G'
Sed allows to restrict commands only to certain lines. This one-liner operates only on lines that match a regular expression /^$/. Which are those? Those are the empty lines. Note that before doing the regular expression match sed pushes the input line to pattern space. When doing it, sed strips the trailing newline character. The empty lines contain just the newline character, so after they have been put into pattern space, this only character has been removed and pattern space stays empty. Regular expression /^$/ matches an empty pattern space and sed applies 'd' command on it, which deletes the current pattern space, reads in the next line, puts it into the pattern space and aborts the current command, and starts the execution from the beginning. The lines which do not match emptiness get a newline character appended by the 'G' command, just like in one-liner #1.
In general sed allows to restrict operations to certain lines (5th, 27th, etc.), to a range of lines (lines 10-20), to lines matching a pattern (lines containing the word "catonmat"), and to lines between two patterns (lines between "catonmat" and "coders").
3. Triple-space a file.
sed 'G;G'
Several sed commands can be combined by separating them by ';'. Such commands get executed one after another. This one-liner does twice what the one-liner #1 does -- appends two newlines (via two 'G' commands) to output.
4. Undo double-spacing.
sed 'n;d'
This one-liner assumes that even-numbered lines are always blank. It uses two new commands - 'n' and 'd'. The 'n' command prints out the current pattern space (unless the '-n' flag has been specified), empties the current pattern space and reads in the next line of input. We assumed that even-numbered lines are always blank. This means that 'n' prints the first, third, fifth, ..., etc. line and reads in the following line. The line following the printed line is always an empty line. Now the 'd' command gets executed. The 'd' command deletes the current pattern space, reads in the next line, puts the new line into the pattern space and aborts the current command, and starts the execution from the first sed command. Now the the 'n' commands gets executed again, then 'd', then 'n', etc.
To make it shorter - 'n' prints out the current line, and 'd' deletes the empty line, thus undoing the double-spacing.
5. Insert a blank line above every line that matches "regex".
sed '/regex/{x;p;x;}'
This one liner uses the restriction operation together with two new commands - 'x' and 'p'. The 'x' command exchanges the hold buffer with the pattern buffer. The 'p' command duplicates input -- prints out the entire pattern space. This one-liner works the following way: a line is read in pattern space, then the 'x' command exchanges it with the empty hold buffer. Next the 'p' command prints out emptiness followed by a newline, so we get an empty line printed before the actual line. Then 'x' exchanges the hold buffer (which now contains the line) with pattern space again. There are no more commands so sed prints out the pattern space. We have printed a newline followed by the line, or saying it in different words, inserted a blank line above every line.
Also notice the { ... }. This is command grouping. It says, execute all the commands in "..." on the line that matches the restriction operation.
6. Insert a blank line below every line that matches "regex".
sed '/regex/G'
This one liner combines restriction operation with the 'G' command, described in one-liner #1. For every line that matches /regex/, sed appends a newline to pattern space. All the other lines that do not match /regex/ just get printed out without modification.
7. Insert a blank line above and below every line that matches "regex".
sed '/regex/{x;p;x;G;}'
This one-liner combines one-liners #5, #6 and #1. Lines matching /regex/ get a newline appended before them and printed (x;p;x from #5). Then they are followed by another newline from the 'G' command (one-liner #6 or #1).

2. Numbering.

8. Number each line of a file (named filename). Left align the number.
sed = filename | sed 'N;s/\n/\t/'
One-liners get trickier and trickier. This one-liner is actually two separate one-liners. The first sed one-liner uses a new command called '='. This command operates directly on the output stream and prints the current line number. There is no way to capture the current line number to pattern space. That's why the second one-liner gets called. The output of first one-liner gets piped to the input of second. The second one-liner uses another new command 'N'. The 'N' command appends a newline and the next line to current pattern space. Then the famous 's///' command gets executed which replaces the newline character just appended with a tab. After these operations the line gets printed out.
To make it clear what '=' does, take a look at this example file:
line one
line two
line three
Running the first one-liner 'sed = filename', produces output:
1
line one
2
line two
3
line three
Now, the 'N' command of the second one-liner joins these lines with a newline character:
1\nline one
2\nline two
3\nline three
The 's/\n/\t/' replaces the newline chars with tabs, so we end up with:
1     line one
2     line two
3     line three
The example is a little inaccurate as line joining with a newline char happens line after line, not on all lines at once.
9. Number each line of a file (named filename). Right align the number.
sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'
This one-liner is also actually two one-liners. The first one liner numbers the lines, just like #8. The second one-liner uses the 'N' command to join the line containing the line number with the actual line. Then it uses two substitute commands to right align the number. The first 's' command 's/^/ /' appends 5 white-spaces to the beginning of line. The second 's' command 's/ *\(.\{6,\}\)\n/\1 /' captures at least six symbols up to a newline and replaces the capture and newline with the back-reference '\1' and two more whitespace to separate line number from the contents of line.
I think it's hard to understand the last part of this sed expression by just reading. Let's look at an example. For clearness I replaced the '\n' newline char with a '@' and whitespace with '-'.
$ echo "-----12@contents" | sed 's/-*\(.\{6,\}\)@/\1--/'
----12--contents
The regular expression '-*\(.\{6,\}\)@' (or just '-*(.{6,})@') tells sed to match some '-' characters followed by at least 6 other characters, followed by a '@' symbol. Sed captures them (remembers them) in \1.
In this example sed matches the first '-' (the '-*' part of regex), then the following six characters "----12" and '@' (the '*\(.\{6,\}\)@' part of regex). Now it replaces the matched part of the string "-----12@" with the contents of captured group which is "----12" plus two extra whitespace. The final result is that "-----12@" gets replaced with "----12--".
10. Number each non-empty line of a file (called filename).
sed '/./=' filename | sed '/./N; s/\n/ /'
This one-liner is again two one-liners. The output of the first one-liner gets piped to the input of second. The first one-liner filters out lines with at least one character in them. The regular expression '/./' says: match lines with at least one char in them. When the empty lines (containing just a newline) get sent to the pattern space, the newline character gets removed, so the empty lines do not get matched. The second one-liner does the same one-liner #8 did, except that only numbered lines get joined and printed out. Command '/./N' makes sure that empty lines are left as-is.
11. Count the number of lines in a file (emulates "wc -l").
sed -n '$='
This one-liner uses a command line switch "-n" to modify sed's behavior. The "-n" switch tells sed not to send the line to output after it has been processed in the pattern space. The only way to make sed output anything with the "-n" switch being on is to use a command that modifies the output stream directly (these commands are '=', 'a', 'c', 'i', 'I', 'p', 'P', 'r' and 'w'). In this one-liner what seems to be the command "$=" is actually a restriction pattern "$" together with the "=" command. The restriction pattern "$" applies the "=" command to the last line only. The "=" command outputs the current line number to standard output. As it is applied to the last line only, this one-liner outputs the number of lines in the file.

3. Text Conversion and Substitution.

12. Convert DOS/Windows newlines (CRLF) to Unix newlines (LF).
sed 's/.$//'
This one-one liner assumes that all lines end with CR+LF (carriage return + line feed) and we are in a Unix environment. Once the line gets read into pattern space, the newline gets thrown away, so we are left with lines ending in CR. The 's/.$//' command erases the last character by matching the last character of the line (regex '.$') and substituting it with nothing. Now when the pattern space gets output, it gets appended the newline and we are left with lines ending with LF.
The assumption about being in a Unix environment is necessary because the newline that gets appended when the pattern space gets copied to output stream is the newline of that environment.
13. Another way to convert DOS/Windows newlines (CRLF) to Unix newlines (LF).
sed 's/^M$//'
This one-liner again assumes that we are in a Unix environment. It erases the carriage return control character ^M. You can usually enter the ^M control char literally by first pressing Ctrl-V (it's control key + v key) and then Ctrl-M.
14. Yet another way to convert DOS/Windows newlines to Unix newlines.
sed 's/\x0D$//'
This one-liner assumes that we are on a Unix machine. It also assumes that we use a version of sed that supports hex escape codes, such as GNU sed. The hex value for CR is 0x0D (13 decimal). This one-liner erases this character.
15-17. Convert Unix newlines (LF) to DOS/Windows newlines (CRLF).
sed "s/$/`echo -e \\\r`/"
This one-liner also assumes that we are in a Unix environment. It calls shell for help. The 'echo -e \\\r' command inserts a literal carriage return character in the sed expression. The sed "s/$/char/" command appends a character to the end of current pattern space.
18. Another way to convert Unix newlines (LF) to DOS/Windows newlines (CRLF).
sed 's/$/\r/'
This one-liner assumes that we use GNU sed. GNU sed is smarter than other seds and can take escape characters in the replace part of s/// command.
19. Convert Unix newlines (LF) to DOS/Windows newlines (CRLF) from DOS/Windows.
sed "s/$//"
This one-liner works from DOS/Windows. It's basically a no-op one-liner. It replaces nothing with nothing and then sends out the line to output stream where it gets CRLF appended.
20. Another way to convert Unix newlines (LF) to DOS/Windows newlines (CRLF) from DOS/Windows.
sed -n p
This is also a no-op one-liner, just like #19. The shortest one-liner which does the same is:
sed ''
21. Convert DOS/Windows newlines (LF) to Unix format (CRLF) from DOS/Windows.
sed "s/\r//"
Eric says that this one-liner works only with UnxUtils sed v4.0.7 or higher. I don't know anything about this version of sed, so let's just trust him. This one-liner strips carriage return (CR) chars from lines. Then when they get output, CRLF gets appended by magic.
Eric mentions that the only way to convert LF to CRLF on a DOS machine is to use tr:
tr -d \r outfile
22. Delete leading whitespace (tabs and spaces) from each line.
sed 's/^[ \t]*//'
Pretty simple, it matches zero-or-more spaces and tabs at the beginning of the line and replaces them with nothing, i.e. erases them.
23. Delete trailing whitespace (tabs and spaces) from each line.
sed 's/[ \t]*$//'
This one-liner is very similar to #22. It does the same substitution, just matching zero-or-more spaces and tabs at the end of the line, and then erases them.
24. Delete both leading and trailing whitespace from each line.
sed 's/^[ \t]*//;s/[ \t]*$//'
This one liner combines #22 and #23. First it does what #22 does, erase the leading whitespace, and then it does the same as #23, erase trailing whitespace.
25. Insert five blank spaces at the beginning of each line.
sed 's/^/     /'
It does it by matching the null-string at the beginning of line (^) and replaces it with five spaces "     ".
26. Align lines right on a 79-column width.
sed -e :a -e 's/^.\{1,78\}$/ &/;ta'
This one-liner uses a new command line option and two new commands. The new command line option is '-e'. It allows to write a sed program in several parts. For example, a sed program with two substitution rules could be written as "sed -e 's/one/two/' -e 's/three/four'" instead of "sed 's/one/two/;s/three/four'". It makes it more readable. In this one-liner the first "-e" creates a label called "a". The ':' command followed by a name crates a named label. The second "-e" uses a new command "t". The "t" command branches to a named label if the last substitute command modified pattern space. This branching technique can be used to create loops in sed. In this one-liner the substitute command left-pads the string (right aligns it) a single whitespace at a time, until the total length of the string exceeds 78 chars. The "&" in substitution command means the matched string.
Translating it in modern language, it would look like this:
while (str.length() <= 78) {
 str = " " + str
}
27. Center all text in the middle of 79-column width.
sed  -e :a -e 's/^.\{1,77\}$/ & /;ta'
This one-liner is very similar to #26, but instead of left padding the line one whitespace character at a time it pads it on both sides until it has reached length of at least 77 chars. Then another two whitespaces get added at the last iteration and it has grown to 79 chars.
Another way to do the same is
sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'
This one-liner left pads the string one whitespace char at a time until it has reached length of 78 characters. Then the additional "s/\( *\)\1/\1/" command gets executed which divides the leading whitespace "in half". This effectively centers the string. Unlike the previous one-liner this one-liner does not add trailing whitespace. It just adds enough leading whitespace to center the string.
28. Substitute (find and replace) the first occurrence of "foo" with "bar" on each line.
sed 's/foo/bar/'
This is the simplest sed one-liner possible. It uses the substitute command and applies it once on each line. It substitutes string "foo" with "bar".
29. Substitute (find and replace) the fourth occurrence of "foo" with "bar" on each line.
sed 's/foo/bar/4'
This one-liner uses a flag for the substitute command. With no flags the first occurrence of pattern is changed. With a numeric flag like "/1", "/2", etc. only that occurrence is substituted. This one-liner uses numeric flag "/4" which makes it change fourth occurrence on each line.
30. Substitute (find and replace) all occurrence of "foo" with "bar" on each line.
sed 's/foo/bar/g'
This one-liner uses another flag. The "/g" flag which stands for global. With global flag set, substitute command does as many substitutions as possible, i.e., all.
31. Substitute (find and replace) the first occurrence of a repeated occurrence of "foo" with "bar".
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'
Let's understand this one-liner with an example:
$ echo "this is foo and another foo quux" | sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'
this is bar and another foo quux
As you can see, this one liner replaced the first "foo" with "bar".
It did it by using two capturing groups. The first capturing group caught everything before the first "foo". In this example it was text "this is ". The second group caught everything after the first "foo", including the second "foo". In this example " and another foo". The matched text was then replaced with contents of first group "this is " followed by "bar" and contents of second group " and another foo". Since " quux" was not part of the match it was left unchanged. Joining these parts the resulting string is "this is bar and another foo quux", which is exactly what we got from running the one-liner.
32. Substitute (find and replace) only the last occurrence of "foo" with "bar".
sed 's/\(.*\)foo/\1bar/'
This one-liner uses a capturing group that captures everything up to "foo". It replaces the captured group and "foo" with captured group itself (the \1 back-reference) and "bar". It results in the last occurrence of "foo" getting replaced with "bar".
33. Substitute all occurrences of "foo" with "bar" on all lines that contain "baz".
sed '/baz/s/foo/bar/g'
This one-liner uses a regular expression to restrict the substitution to lines matching "baz". The lines that do not match "baz" get simply printed out, but those that do match "baz" get the substitution applied.
34. Substitute all occurrences of "foo" with "bar" on all lines that DO NOT contain "baz".
sed '/baz/!s/foo/bar/g'
Sed commands can be inverted and applied on lines that DO NOT match a certain pattern. The exclamation "!" before a sed commands does it. In this one-liner the substitution command is applied to the lines that DO NOT match "baz".
35. Change text "scarlet", "ruby" or "puce" to "red".
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'
This one-liner just uses three consecutive substitution commands. The first replaces "scarlet" with "red", the second replaced "ruby" with "red" and the last one replaces "puce" with "red".
If you are using GNU sed, then you can do it simpler:
gsed 's/scarlet\|ruby\|puce/red/g'
GNU sed provides more advanced regular expressions which support alternation. This one-liner uses alternation and the substitute command reads "replace 'scarlet' OR 'ruby' OR 'puce' with 'red'".
36. Reverse order of lines (emulate "tac" Unix command).
sed '1!G;h;$!d'
This one-liner acts as the "tac" Unix utility. It's tricky to explain. The easiest way to explain it is by using an example.
Let's use a file with just 3 lines:
$ cat file
foo
bar
baz
Running this one-liner on this file produces the file in reverse order:
$ sed '1!G;h;$!d' file
baz
bar
foo
The first one-liner's command "1!G" gets applied to all the lines which are not the first line. The second command "h" gets applied to all lines. The third command "$!d" gets applied to all lines except the last one.
Let's go through the execution line by line.
Line 1: Only the "h" command gets applied for the first line "foo". It copies this line to hold buffer. Hold buffer now contains "foo". Nothing gets output as the "d" command gets applied.
Line 2: The "G" command gets applied. It appends the contents of hold buffer to pattern space. The pattern space now contains. "bar\nfoo". The "h" command gets applied, it copies "bar\nfoo" to hold buffer. It now contains "bar\nfoo". Nothing gets output.
Line 3: The "G" command gets applied. It appends hold buffer to the third line. The pattern space now contains "baz\nbar\nfoo". As this was the last line, "d" does not get applied and the contents of pattern space gets printed. It's "baz\nbar\nfoo". File got reversed.
If we had had more lines, they would have simply get appended to hold buffer in reverse order.
Here is another way to do the same:
sed -n '1!G;h;$p'
It silences the output with "-n" switch and forces the output with "p" command only at the last line.
These two one-liners actually use a lot of memory because they keep the whole file in hold buffer in reverse order before printing it out. Avoid these one-liners for large files.
37. Reverse a line (emulates "rev" Unix command).
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
This is a very complicated one-liner. I had trouble understanding it the first time I saw it and ended up asking on comp.unix.shell for help.
Let's re-format this sed one-liner:
 sed '
   /\n/ !G
   s/\(.\)\(.*\n\)/&\2\1/
   //D
   s/.//
 ' 
The first line "/\n/ !G" appends a newline to the end of the pattern space if there was none.
The second line "s/\(.\)\(.*\n\)/&\2\1/" is a simple s/// expression which groups the first character as \1 and all the others as \2. Then it replaces the whole matched string with "&\2\1", where "&" is the whole matched text ("\1\2"). For example, if the input string is "1234" then after the s/// expression, it becomes "1234\n234\n1".
The third line is "//D". This statement is the key in this one-liner. An empty pattern // matches the last existing regex, so it's exactly the same as: /\(.\)\(.*\n\)/D. The "D" command deletes from the start of the input till the first newline and then resumes editing with first command in script. It creates a loop. As long as /\(.\)\(.*\n\)/ is satisfied, sed will resume all previous operations. After several loops, the text in the pattern space becomes "\n4321". Then /\(.\)\(.*\n\)/ fails and sed goes to the next command.
The fourth line "s/.//" removes the first character in the pattern space which is the newline char. The contents in pattern space becomes "4321" -- reverse of "1234".
There you have it, a line has been reversed.
38. Join pairs of lines side-by-side (emulates "paste" Unix command).
sed '$!N;s/\n/ /'
This one-liner joins two consecutive lines with the "N" command. They get joined with a "\n" character between them. The substitute command replaces this newline with a space, thus joining every pair of lines with a whitespace.
39. Append a line to the next if it ends with a backslash "\".
sed -e :a -e '/\\$/N; s/\\\n//; ta'
The first expression ':a' creates a named label "a". The second expression looks to see if the current line ends with a backslash "\". If it does, it joins it with the line following it using the "N" command. Then the slash and the newline between joined lines get erased with "s/\\\n//" command. If the substitution was successful we branch to the beginning of expression and do the same again, in hope that we might have another backslash. If the substitution was not successful, the line did not end with a backslash and we print it out.
Here is an example of running this one-liner:
$ cat filename
line one \
line two
line three
$ sed -e :a -e '/\\$/N; s/\\\n//; ta' filename
line one line two
line three
Lines one and two got joined because the first line ended with backslash.
40. Append a line to the previous if it starts with an equal sign "=".
sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
This one-liner also starts with creating a named label "a". Then it tests to see if it is not the last line and appends the next line to the current one with "N" command. If the just appended line starts with a "=", one-liner branches the label "a" to see if there are more lines starting with "=". During this process a substitution gets executed which throws away the newline character which came from joining with "N" and the "=". If the substitution fails, one-liner prints out the pattern space up to the newline character with the "P" command, and deletes the contents of pattern space up to the newline character with "D" command, and repeats the process.
Here is an example of running it:
$ cat filename
line one
=line two
=line three
line four
$ sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D' filename
line one line two line three
line four
Lines one, two and three got joined, because lines two and three started with '='. Line four got printed as-is.
41. Digit group (commify) a numeric string.
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
This one-liner turns a string of digits, such as "1234567" to "1,234,567". This is called commifying or digit grouping.
First the one-liner creates a named label "a". Then it captures two groups of digits. The first group is all the digits up to last three digits. The last three digits gets captures in the 2nd group. Then the two matching groups get separated by a comma. Then the same rules get applied to the line again and again until all the numbers have been grouped in groups of three.
Substitution command "\1,\2" separates contents of group one with a comma from the contents of group two.
Here is an example to understand the grouping happening here better. Suppose you have a numeric string "1234567". The first group captures all the numbers until the last three "1234". The second group captures last three numbers "567". They get joined by a comma. Now the string is "1234,567". The same stuff is applied to the string again. Number "1" gets captured in the first group and the numbers "234" in the second. The number string is "1,234,567". Trying to apply the same rules again fail because there is just one digit at the beginning of string, so the string gets printed out and sed moves on to the next line.
If you have GNU sed, you can use a simpler one-liner:
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
This one-liner starts with creating a named label "a" and then loops over the string the same way as the previous one-liner did. The only difference is how groups of three digits get matched. GNU sed has some additional patterns. There are two patterns that make this one-liner work. The first is "\B", which matches anywhere except at a word boundary. It's needed so we did not go beyond word boundary. Look at this example:
$ echo "12345 1234 123" | sed 's/[0-9]\{3\}\>/,&/g'
12,345 1,234 ,123
It's clearly wrong. The last 123 got a comma added. Adding the "\B" makes sure we match the numbers only at word boundary:
$ echo "12345 1234 123" | sed 's/\B[0-9]\{3\}\>/,&/g'
12,345 1,234 123
The second is "\>". It matches the null string at the end of a word. It's necessary because we need to to match the right-most three digits. If we did not have it, the expression would match after the first digit.
42. Add commas to numbers with decimal points and minus signs.
gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'
This one-liner works in GNU sed only. It turns on extended regular expression support with the "-r" switch. Then it loops over a line matching three groups and separates the first two from the third with a comma.
The first group makes sure we ignore a leading non-digit character, such as + or -. If there is no leading non-digit character, then it just anchors at the beginning of the string which always matches.
The second group matches a bunch of numbers. The third group makes sure the second group does not match too many. It matches 3 consecutive numbers at the end of the string.
Once the groups have been captured, the "\1\2,\3" substitution is done and the expression is looped again, until the whole string has been commified.
43. Add a blank line after every five lines.
sed 'n;n;n;n;G;'
The "n" command is called four times in this one-liner. Each time it's called it prints out the current pattern space, empties it and reads in the next line of input. After calling it four times, the fifth line is read into the pattern space and then the "G" command gets called. The "G" command appends a newline to the fifth line. Then the next round of four "n" commands is done. Next time the first "n" command is called it prints out the newlined fifth line, thus inserting a blank line after every 5 lines.
The same can be achieved with GNU sed's step extension:
gsed '0~5G'
GNU sed's step extensions can be generalized as "first~step". It matches every "step"'th line starting with line "first". In this one-liner it matches every 5th line starting with line 0.


More at:
Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution - good coders code, great reuse

'via Blog this'

How to tweak CFLAGS globally when configure is in use, such as a gcc build

I plan to use this to pass down "-save-temps=obj" to force temporary assembly files to be preserved in a place where they can be found for reference.  Wish me luck (or tell me where I go astray)!  :-)    C.
----------------------------------------------------------------------------------------
config.site - automake: "2.2.5 Overriding Default Configuration Setting with config.site

When installing several packages using the same setup, it can be convenient to create a file to capture common settings. If a file named prefix/share/config.site exists, configure will source it at the beginning of its execution.

Recall the command from the previous section:

~/amhello-1.0 % ./configure --prefix ~/usr CC=gcc-3 \
CPPFLAGS=-I$HOME/usr/include LDFLAGS=-L$HOME/usr/lib
Assuming we are installing many package in ~/usr, and will always want to use these definitions of CC, CPPFLAGS, and LDFLAGS, we can automate this by creating the following ~/usr/share/config.site file:

test -z "$CC" && CC=gcc-3
test -z "$CPPFLAGS" && CPPFLAGS=-I$HOME/usr/include
test -z "$LDFLAGS" && LDFLAGS=-L$HOME/usr/lib
Now, any time a configure script is using the ~/usr prefix, it will execute the above config.site and define these three variables.

~/amhello-1.0 % ./configure --prefix ~/usr
configure: loading site script /home/adl/usr/share/config.site
...
See Setting Site Defaults, for more information about this feature."

Also, thanks to this site for the following:
http://stackoverflow.com/questions/7561509/how-to-add-include-and-lib-paths-to-configure-make-cycle

" You want a config.site file. Try:
$ mkdir -p ~/local/share
$ cat << EOF > ~/local/share/config.site
CPPFLAGS=-I$HOME/local/include
LDFLAGS=-L$HOME/local/lib
...
EOF 
Whenever you invoke an autoconf generated configure script with --prefix=$HOME/local, the config.site will be read and all the assignments will be made for you. CPPFLAGS and LDFLAGS should be all you need, but you can make any other desired assignments as well (hence the ... in the sample above). Note that -I flags belong in CPPFLAGS and not in CFLAGS, as -I is intended for the pre-processor and not the compiler."

'via Blog this'

Thursday, January 10, 2013

Degree from the School of Hard Knocks? thanks to Nick Corcodilos


Can I earn a degree from the School of Hard Knocks?

Filed under: Q&A, Readers' Forum, Success at Work
In the December 18, 2012 Ask The Headhunter Newsletter, a job hunter considers an online, or “distance learning” degree:
I have over 24 years experience in industry, but I never got a college degree. Now I want to get a bachelors. A “distance learning” college has approved my application for a B.S. in Business Administration. This is one of those schools that delivers its courses online and also awards credits for “life experience.” Please give me your opinion on degrees of this nature. Thanks.

Nick’s Reply

These “life experience” credits can be legit. They are based on knowledge you’ve acquired on the job rather than through college courses. The school administers a test on the material and if you pass, they give you the same credits you’d get if you actually took the course. You just need to be sure the school itself is legit — or those School of Hard Knocks credits could be worthless. Several times each week I get solicitations for questionable degree programs.
My advice: Whatever state you are in, contact the state department of education. Find out whether this school is accredited. If it is not, forget it. Find one that is.
To test the value of this school’s programs, contact a few well-known colleges or universities and talk to the admissions office. Ask whether they would accept “transfer credits” from the school in question. A good distance school’s credits will be accepted toward a degree at other good schools. If credits are not transferable, find another school.
If the online school you choose is legit, you may be able to leverage your investment by finishing your degree program at a bricks-and-mortar school — and you’d get your diploma from a more recognized school. Just beware: Some online degree programs cost more than traditional schools charge! The good news: Many good traditional schools offer online courses and combination programs. Don’t assume you need to start with an online-only school.
Want more certainty? Ask the company you work for (or want to work for) how it regards degrees from the distance school. This will tell you a lot about the value of the degree.
I’d start your research by checking the Sloan Consortium to see whether the school you’re considering is a member.
(For every problem, there’s a flip side. And the flip side of this problem is academics with degrees who can’t overcome their own obstacles to win a job. For more on this, see Breaking Ranks & Rules: How academics can avoid 5 fatal mistakes in the job hunt.)
Do you have an online degree? Has it paid off? Does your company look favorably on distance learning schools? On credits from the School of Hard Knocks? What are the alternatives to traditional education, and what do they mean to employers?


..M ore, including reader comments (these are usually very instructive) at:
Ask The Headhunter® | Nick Corcodilos – Can I earn a degree from the School of Hard Knocks?

'via Blog this'