Last active
March 13, 2025 08:39
-
-
Save akanehara/580ca09afe0862db05ca33cb8ace087a to your computer and use it in GitHub Desktop.
実用一行野郎
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VBoxManage list runningvms | awk '$0=$1' | tr -d '"' | xargs -n1 -I@ VBoxManage controlvm @ acpipowerbutton | |
VBoxManage list vms | sed -E 's/"(.+)".*$/\1/' | tr '\n' '\0' | xargs -0 -I@ VBoxManage unregistervm @ --delete | |
# 事前ソート不要の uniq | |
awk '!s[$0]{s[$0]=1;print}' | |
# nl 用例 | |
# - ゼロ埋め2桁連番空白セパレータ | |
nl -nrz -w2 -s ' ' | |
# 使われていないvmの削除コマンド (さらにshにパイプして実行) | |
join -v1 <(\ls ~/"VirtualBox VMs" | sort) <(VBoxManage list vms | sed -E 's/^"([^"]+)".*$/\1/' | sort) | sed 's/.*/rm -r ~\/"VirtualBox VMs"\/&/' | |
# ストリーム特定列の日付変換 (unix epochに変換する例) | |
# (Perl) セパレータ未 | |
perl -MTime::Piece -anle 'print Time::Piece->strptime($F[0],"%Y-%m-%dT%H:%M:%S")->epoch, $F[1], $F[2]' | |
# (dateコマンド) (毎行forkするので非効率) | |
while read date rest; do echo "$(date -d "$started" +%s)" $rest; done | |
# 引数で与えられた2つの日付の差(秒) | |
sh -c 'echo $(date +%s -d"$1") - $(date +%s -d"$0") | bc' | |
# データは変更せずに列区切りだけを変更 | |
# (いずれかの列を変更しないと読んだ行全体($0)をそのまま出力するので $1=$1 を実行して $0 を更新する) | |
awk -v OFS='\t' '$1=$1' | |
# 日付間の差(秒) (GNU coreutils の date のみ) | |
,(){ echo $(date +%s.%N -d "$2") - $(date +%s.%N -d "$1")|bc;};, '2024/02/22T09:47:05' '2024/02/22T10:16:21' | |
# 前の行から変化ありなら出力 | |
awk 'p!=$0;{p=$0}' | |
# 1列目に時刻付与 | |
awk '{print strftime("%H:%M:%S"),$0;fflush()}' | |
## UNIXエポック.マイクロ秒 | |
perl -MTime::HiRes=gettimeofda -e '$|=1;printf("%d.%06d\n",gettimeofday);' | |
# 1列目に付与 | |
perl -MTime::HiRes=gettimeofday -aple '$|=1;$_=sprintf("%d.%06d\t%s",gettimeofday,$_);' | |
## ISO日付.マイクロ秒 | |
# 1列目に付与 | |
perl -MTime::HiRes=gettimeofday -aple '$|=1;($t,$u)=gettimeofday;($s,$m,$h,$D,$M,$Y)=localtime($t);$Y+=1900;$_=sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%06d\t%s",$Y,$M,$D,$h,$m,$s,$u,$_);' | |
# 空ファイル | |
dd if=/dev/zero of=${FILE} bs=${BLOCK_SIZE} count=${COUNT} | |
# ランダムファイル | |
head -c /dev/urandom > ${FILE} | |
# 列番号 -> 列記号 | |
seq 78 | perl -aple 'sub f{my$d=int(($_[0]-1)/26);($d?f($d):"").chr 65+($_[0]-1)%26};$_=f($F[0]);' | |
seq 78 | dc -e '[[d1-26/r1-26%65+rd0<F]dsFxsN[Pz0<G]dsGx[]pc?z0<M]sM?z0<M' | |
# 列記号 -> 列番号 | |
# スプレッドシートの37万8861列目は UNKO | |
echo UNKO | perl -aple 'my($d,$x);for(map ord(uc$_)-65,reverse split//,$F[0]){$x+=($_+1)*(26**$d++)};$_=$x' | |
# MからAN | |
echo M AN | xargs -n1 | perl -aple 'my($d,$x);for(map ord(uc$_)-65,reverse split//,$F[0]){$x+=($_+1)*(26**$d++)};$_=$x' | xargs seq | perl -aple 'sub f{my$d=int(($_[0]-1)/26);($d?f($d):"").chr 65+($_[0]-1)%26};$_=f($F[0]);'| xargs | tr \ , | |
# Perlの範囲が使える | |
perl -le 'print for ("M".."AN")' | |
# ファイル送受信 | |
_()(nc -l $1|(read F;gzip -d >$F&&echo $F received.));_ $PORT | |
# 受信側 | |
_()((basename $3;gzip -c $3)|nc $1 $2&&echo $3 send.);_ $HOST $PORT $FILEPATH | |
# 送信側 | |
# べき集合 | |
perl -e '$"="\t";@V=@ARGV;for$i(0..2**@V-1){print"@{[map{($i&2**$_)?$V[$_]:()}(0..$#V)]}\n"}' りんご みかん ぶどう | |
# 16進->10進変換 | |
dc -e '10o16i?[n[]pn?z0!=.]ds.x' | |
# Perlのunpackフォーマット(ごく一部,CWSLQのみ)を与えるとサイズやオフセットを出力する | |
echo QQSLQQQL16LQQQ | sed 's/[a-zA-Z][0-9]*/& /g' | xargs -n1 | sed 's/^./& /' | awk '{$1=toupper($1);if(!$2)$2=1}1' | sed 's/[CW]/1/;s/S/& 2/;s/L/& 4/;s/Q/& 8/' | awk -v OFS=\\t 'BEGIN{o=0;i=0;print "Index","Offset","Format","Size"}{s=$2*$3;print i,o,$1$3,s;o+=s;i+=$3}END{print "",o}' | expand | |
# ${SSH_CONF}に定義されているホストに${DIR}を一括転送 | |
# (ホストの抽出は雑なので単純なケースしか対応できないかも) | |
awk 'tolower($1)~/^host$/ && $2!~/\*/ && $0=$2' $SSH_CONF | xargs -n1 -I@ scp -F $SSH_CONF -r $DIR '@:~' | |
# (macOS) markdownの見出しとcodeをtextile表記に変換 | |
pbpaste | awk '$1~/#+/{$1="h."length($1)} $1=="```"{if(!x){$1="<pre>"}else{$1="</pre>"};x=!x} 1' | tee /dev/tty | pbcopy | |
# NHK地震情報(日時 震度 場所) | |
curl -Ls http://www3.nhk.or.jp/sokuho/jishin/data/JishinReport.xml | nkf -w | sed -E -n 's@.*shindo="(.*)" url=".*JSA(.{3})(.{2})(.{2})(.{2})(.{2})(.{2})_.*\.xml">(.*)</item>.*@2\2-\3-\4 \5:\6:\7 \1 \8@p' | sort -k1,2 | |
# 日本郵政 追跡 | |
curl -s 'https://trackings.post.japanpost.jp/services/srv/search/?search.x=78&search.y=5&locale=ja&requestNo1=$ID1&requestNo2=$ID2' | xmllint --html --xpath "//table[@class='tableType01 txt_c m_b5']//tr//text()" - 2> /dev/null | nkf -Lu | xargs -n1 | gsed -E '/....\/..\/../{N;s/\n/T/g}' | xargs -n7 | awk -vOFS=\\t '{sub("T"," ",$3);print}' | |
# HTMLタグ除去 | |
sed -E 's/<[^>]+(>|$)//g;s/^[^>]+>//g' | |
# Shift_JISのHTMLパース | |
nkf -w | sed 's/Shift_JIS/UTF-8/' | xmllint --html --encode utf-8 --xpath "/" - 2>/dev/null | |
# sed(POSIX) 上書き | |
sed '$a192.0.2.0 example.com' /etc/hosts | (sudo rm -f; sudo tee /etc/hosts) | |
# sort -Rが使えないときのランダムソート | |
## AWK(POSIX)を使う方法 | |
awk 'BEGIN{srand()}{print rand(),$0}' | sort -k1,1n | cut -d\ -f2- | |
awk 'BEGIN{srand()}$0=rand()"\t"$0' | sort -k1,1n | cut -f2- | |
## /dev/urandomを使う方法 | |
{ while read x;do echo $(od -vAn -N1 -tu1 < /dev/urandom) "$x";done;} | sort -k1,1n | cut -d\ -f2- | |
# 時刻毎の件数 | |
awk 'BEGIN{OFS="\t"}{t=substr($1,1,19);if(t0&&t!=t0){print(t0,a);a=0}t0=t}{a++}' | |
# URIパース | |
perl -MURI::Split=uri_split -ple '$_=join "\t", uri_split $_' | |
perl -MURI::Split=uri_join -F\\t -aple '$_=uri_join map {""eq$_?undef:$_} @F' | |
# URLエンコード/デコード | |
perl -MURI::Escape -ple '$_=uri_escape $_;' | |
perl -MURI::Escape -ple '$_=uri_unescape $_;' | |
# 依存モジュールなしの簡易デコード | |
perl -pe 's/%([0-9a-fA-F]{2})/pack("H2",$1)/eg;' | |
# YAMLトラバース | |
perl -MYAML=Load -e 'sub t{my($n,$p)=@_;if("ARRAY"eq ref$n){my$i=0;for(@{$n}){t($_,"$p\[$i\]");$i++}return}if("HASH"eq ref$n){for(keys%{$n}){t($n->{$_},"$p.$_")};return};print"$p\t$n\n"};t Load join"",<>' | |
# ssh-configに含まれるホストの接続テスト | |
cat ssh_config | awk '$1=="Host"&&$2!~/\*/&&$0=$2' | while read H; do echo $H $(ssh -n -F ssh_config $H echo OK); done | |
# GNU Parallel インストール | |
U=http://ftp.gnu.org/gnu/parallel/parallel-latest.tar.bz2;D=$HOME/$(basename $U|cut -d. -f1);(mkdir -p $D&&cd $D&&curl -s $U|tar xjv --strip=1 -C .&&./configure&&make&&sudo make install) | |
# Nmap最新版 | |
curl -Ls https://nmap.org/dist/ | sed -En '/nmap.*tgz/s@^.*href="([^"]+)".*$@https://nmap.org/dist/\1@p' | tail -n1 | |
# Nmap最新版インストール | |
U=https://nmap.org/dist/;B=$(curl -s $U|sed -En '/nmap.*tgz/s@^.*href="([^"]+).tgz".*$@\1@p'|tail -n1);curl -O $U$B.tgz&&tar zxvf $B.tgz&&(cd $B&&./configure&&sudo make install) | |
# jqインストール | |
U=https://github.com/stedolan/jq/releases/download/jq-1.5/jq-1.5.tar.gz;D=$HOME/$(basename $U|cut -d. -f1,2);(mkdir -p $D&&cd $D&&curl -s -L $U|tar xzv --strip=1 -C .&&./configure --disable-maintainer-mode&&make&&sudo make install) | |
# joインストール | |
U=https://github.com/jpmens/jo/archive/v1.0.tar.gz;D=$HOME/jo-$(basename $U|cut -d. -f1,2);(mkdir -p $D&&cd $D&&curl -s -L $U|tar xzv --strip=1 -C .&&autoreconf -i&&./configure&&make check&&sudo make install) | |
# Redmine作業時間 | |
pbpaste | sed -E -e 's/^['$'\t\s'']*//' | awk -F \\t '$2~/[0-9]{4}\/[0-9]{2}\/[0-9]{2}/' | awk -F \\t -v OFS=\\t '{k=$2;gsub(/\//,"",k);d[k]=$2;r[k]=r[k]$1"\t"$4"\t"$5"\t"$6"\n";h[k]+=$7;l[k]++}END{for(k in d){rhk=int(h[k]+0.5);printf "%s\t%.6g(≒%.6g)[h]\t%.6g(≒%.6g)[man*day]\t%d Rows\n",d[k],rhk,h[k],rhk/8,h[k]/8,l[k];print r[k]}}' | |
# say ボイスサンプル | |
say -v \? | fgrep -v en_ | sed -E -e 's/^(.*) +([-_a-zA-Z]+) *# (.*)$/say -v \1 \3/' | sh -x | |
# 振替休日 | |
join -a1 <(gseq 20160101 20181231|gdate +%F\ %w -f- 2>/dev/null) <(curl -sL https://t.co/jPhvj7aMn8 |nkf -wLu|tail -n+2|tr , \ )|awk -vOFS=, 'p&&$2!=0!$3{$3="振替休日";p=0}$2==0&&$3{p=1}NF==3{print$1,$3}' | |
# 衆参国会議員一覧 | |
echo url="http://seiji.kpi-net.com/api/?format=json&type="{1,2}"&count=999"|xargs -n1|curl -s -K -|sed 's/\\n//g'|jq -r '.[]|"\(.category)\t\(.seitou)\t\(.name)\t\(.yomi)\t\(.birth)\t\(.area)\t\(.hatsu_tousen)\t\(.tousen_kaisu)"'|awk -F\\t '$0~//' | |
# カレントディレクトリのEXIF から撮影日付を取得してファイル名のプレフィックスとする | |
# FIXME: EXIFを持たないファイルのエラー処理 | |
# FIXME: カレントディレクトリに依存せず、findの結果を入力として処理できるようにする | |
ls | while read F;do mv $F $(identify -format "%[Exif:DateTimeOriginal]\n" $F | awk '$0=$1' | tr : -)_$F;done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment