Skip to content

Instantly share code, notes, and snippets.

@aliyome
Created October 29, 2014 08:16
Show Gist options
  • Select an option

  • Save aliyome/77b0b90b5282ae8a4441 to your computer and use it in GitHub Desktop.

Select an option

Save aliyome/77b0b90b5282ae8a4441 to your computer and use it in GitHub Desktop.
import std.stdio;
import std.string;
import std.array;
import pegged.grammar;
import markdown;
mixin(grammar(MarkdownGrammar));
void main() {
auto doc = Markdown(sampleMd).children.front;
//writeln(doc);
//writeln("--------------------");
foreach (child; doc.children) {
if (child.name == "Markdown.Heading") {
if (child.matches[0] == "##") {
writeln(child.matches[1].strip);
writeln(childrenToString(child));
}
}
}
//writeln(childrenToString(doc));
//writeln(doc.matches.join);
}
string childrenToString(ParseTree root) {
if (root.name == "Markdown.Heading") {
return "\n------------------------\n";
}
else {
string result = root.matches.join;
//foreach (child; root.children) {
// result ~= childrenToString(child);
//}
return result;
}
}
enum sampleMd = `
# 101 基本コマンド
## date
date "+%Y/%m/%d %H:%M:%S" # 2014/10/04 21:47:50
## man
参照するドキュメントの位置
/usr/share/man
/usr/local/share/man
## history
history -d 100 # 履歴番号100を削除
history -c # 現在のシェルの履歴を削除
## su
su - root # rootの権限・環境でログイン
su - # su - root と同じ
su root # 現在のユーザの環境のままrootにログイン
## mkdir
mkdir -p # 親ディレクトリがない場合は作る
## cp
cp -R hoge foo # ディレクトリをコピーする場合は-Rが必要
## lsattr
ファイルの属性を表示する
## tar
オプション
* c # Create
* t # Table(表示)
* x # eXtract
* f # File(指定)
## dd
# write fd image
dd if=fd.img of=/dev/fd0
## リダイレクト
# 標準出力を標準エラー出力にリダイレクト
echo hoge 1> hoge.txt 1>&2
echo hoge > hoge.txt >&2 # 1は省略できる
## tac
ファイルの内容を最後の行から表示する
## nl
行頭に行番号を付加
## tee
標準出力をリダイレクトしつつファイルにも保存する
## grep
* -n # 行番号の表示
* -i # 大文字小文字を区別しない
* -v # パターンに一致しない行を表示
* -l # パターンに一致するファイル名を表示
fgrep 正規表現ではなく単純な文字列として検索する
## tr
tr a-z A-Z < file # file内のアルファベットを大文字へ変換
## sed
* -i # 編集結果を直接ファイルに書き込む
sed s/bon/BoN/g # ファイル全体のbonをBoNに置換
sed 1d # 1行目を削除
sed 1,2d # 1~2行目を削除
sed -n /hoge/p # hogeを含む行のみ表示
## cut
cut -d ' ' -f 2,3 hoge # スペース区切りのcsvの2,3列目だけ表示
cut -c 1-4 # 各行の1-4文字目だけ表示
## wc
* -c # バイト数を表示
* -l # 行数を表示
* -w # 単語数を表示
## od
ファイルの内容を8進数で表示
* -x # 16進数で表示
* -d # 10進数で表示
## expand
タブをスペースへ変換
* -i # 行頭のみ
* -t # スペース数を指定
## fmt
fmt -w 80 file # 幅を指定して整形
## pr
印刷用のファイル整形コマンド
## sort
行単位で並び替える
## uniq
重複行の削除
## split
指定したサイズにファイルを分割
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment