Archive for June 2009
join & filter multiline data records with sed
Grouping with sed:
$ cat nrs.txt 01 02 03 04 05 06 07 08 09 10 11 12 $ cat nrs.txt | sed -n -e 'N;s/\n/ /g;p' 01 02 03 04 05 06 07 08 09 10 11 12 $ cat nrs.txt | sed -n -e 'N;N;s/\n/ /g;p' 01 02 03 04 05 06 07 08 09 10 11 12 $ cat nrs.txt | sed -n -e 'N;N;N;s/\n/ /g;p' 01 02 03 04 05 06 07 08 09 10 11 12 $ cat nrs.txt | sed -n -e 'N;N;N;N;N;s/\n/ /g;p' 01 02 03 04 05 06 07 08 09 10 11 12 $
Good for the following task:
$ cat entries.txt entry-1-data-1 entry-1-data-2 entry-2-data-1 entry-2-data-2 $ cat entries.txt | sed -n -e 'N;s/\n/ /;p' entry-1-data-1 entry-1-data-2 entry-2-data-1 entry-2-data-2 $
Two long groups, removing elements (1st, 2nd):
$ cat nrs.txt | sed -n -e 'p;n' 01 03 05 07 09 11 $ cat nrs.txt | sed -n -e 'n;p' 02 04 06 08 10 12 $
In general case you have groups of length _k_ and the starting pattern is ‘n;n;…;n’. There’s k-1 number of letter n here. You can place letter p around the n-s, which is exactly k possibility. If there’s a p in position k0 then the k0-th element will be printed out.
So if you have 6 long groups and you want every fifth element:
$ cat nrs.txt | sed -n -e 'n;n;n;n;p;n' 05 11 $
office open xml file sample
Here’s the spec.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part
pkg:name="/_rels/.rels"
pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
<pkg:xmlData>
<Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship
Id="foo"
Type=
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Target="/word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part
pkg:name="/word/document.xml"
pkg:contentType=
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:t>Hello World</w:t>
<w:br />
<w:yearLong />
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>
cumulating minutes begun
$ cat seconds.txt 120 123 $ cat seconds.txt | sed 's/$/ 60 ~ 0 !=r +/' | sed '1i[1+] sr 0' | sed '$ap' | dc 5 $
compiler with flex & bison in 77 lines
Please note that you cannot copy-paste the Makefile below.
afroid-laptop% ls
lexer.l Makefile parser.y
afroid-laptop% cat * | wc -l
77
afroid-laptop% cat -A Makefile
all:^Icompiler$
$
compiler:^Iparser.tab.o lex.yy.o$
^Igcc -o $@ parser.tab.o lex.yy.o -lm -lfl$
$
parser.tab.o:^Iparser.y$
^Ibison -dv parser.y$
^Igcc -c parser.tab.c$
$
lex.yy.o:^Ilexer.l parser.tab.o$
^Iflex lexer.l$
^Igcc -c lex.yy.c$
$
clean:$
^Irm -rf lex.yy.c lex.yy.o parser.tab.o parser.tab.c parser.tab.h parser.output compiler$
afroid-laptop% cat lexer.l
%{
#include
#include "parser.tab.h"
%}
DIGIT [0-9]
ALPHA [a-zA-Z]
WS [ \n\t]
%%
{DIGIT}+ { yylval.intval = atoi(yytext); return NUMBER; }
{ALPHA}+ { yylval.id = (char *) strdup(yytext); return IDENTIFIER; }
{WS}+ {}
%%
int yywrap (void)
{
}
afroid-laptop% cat parser.y
%{
#include
#include
extern FILE *yyin;
extern FILE *yyout;
%}
%union
{
int intval;
char* id;
}
%start S
%token NUMBER
%token IDENTIFIER
%%
S:
/* empty */
| IDENTIFIER S NUMBER { fprintf (yyout, "let %s = %d;\n", $1, $3); }
%%
main (int argc, char **argv)
{
char out[1024];
++argv; --argc;
yyin = fopen (argv[0], "r");
strcpy(out,argv[0]);
strcat(out,".out");
yyout = fopen (out, "w");
yyparse ();
printf ("Parse Completed\n");
}
yyerror (char *s)
{
printf("error\n");
}
afroid-laptop% cat > test.src << EOF
heredoc> a b c 1 2 3
heredoc> EOF
afroid-laptop% make
bison -dv parser.y
gcc -c parser.tab.c
flex lexer.l
gcc -c lex.yy.c
gcc -o compiler parser.tab.o lex.yy.o -lm -lfl
afroid-laptop% ./compiler test.src
Parse Completed
afroid-laptop% cat test.src.out
let c = 1;
let b = 2;
let a = 3;
afroid-laptop%