
次のようなテキストがあるとしましょう(の出力objdump -d
)。
0: 0f a2 cpuid
2: a9 01 00 00 00 test eax,0x1
7: 74 01 je a <myFunc+0xa>
9: c3 ret
a: 0f 0b ud2a
^ +[0-9a-f]+:
(長さを維持するため)対応するスペースの数でテキストを変更したいのですがただ前の部分が:
他の場所で言及されていない場合(単語、つまり単語の境界内に含まれている場合)、たとえば、上記の例では、タグ、、、は空白で置き換えられ、変更されていません(3行目で述べたように0
))。2
7
9
a
処理後の上記の例は次のとおりです。
0f a2 cpuid
a9 01 00 00 00 test eax,0x1
74 01 je a <myFunc+0xa>
c3 ret
a: 0f 0b ud2a
タグの発生回数を計算してから、その回数に応じて行を処理するよりも、シェル/vimの方が良い方法はありますか?
現在のコードは2300行のファイルを3分で処理します(Intel Atom CPUで)。
#!/bin/bash -e
if [ $# -ne 2 ]; then
echo "Usage: $0 infile outfile" >&2
exit 1
fi
file="$1"
outfile="$2"
cp "$file" "$outfile"
labelLength=$(sed -n '/^ \+\([0-9a-f]\+\):.*/{s@@\1@p;q}' "$file"|wc -c)
replacement=$(printf %${labelLength}c ' ')
sed 's@^ \+\([0-9a-f]\+\):.*@\1@' "$file" | while read label
do
if [ $(grep -c "\<$label\>" "$file") = 1 ]; then
sed -i "s@\<$label\>:@$replacement@" "$outfile"
fi
done
答え1
Perlソリューション:
$ perl -lne '$k{"$_:"}++ for split(/\b/); push @l,$_; }{
map{s/\S+:/$k{$&}<2 ? " " x length($&) : $&/e; print}@l;' file
0f a2 cpuid
a9 01 00 00 00 test eax,0x1
74 01 je a <myFunc+0xa>
c3 ret
a: 0f 0b ud2a
これは次のとおりです。
#!/usr/bin/perl
use strict;
my %wordsHash;
my @lines;
## Read the input file line by line, saving each
## line as $_. This is what 'perl -n` means.
while (<>) {
## Remove trailing newlines. This is done
## by -l in the one liner.
chomp;
## Split the current line on word boundaries
my @wordsArray=split(/\b/);
## Save each word + ":" as a key in the hash %wordsHash,
## incrementing the value by one each time the word
## is seen.
foreach my $word (@wordsArray) {
$wordsHash{"$word:"}++;
}
## Add the line to the array @lines
push @lines, $_;
}
## After the file has been read. ('}{' in the one-liner)
## Iterate over each line in @lines ( map{}@l in the one-liner)
foreach my $line (@lines) {
## Grab the first set of non-whitespace
## characters until the 1st ':'
$line=~/\S+:/;
## $& is whatever was matched
my $match=$&;
## If the match was seen only once
## as a word (all will be seen at least once)
if ($wordsHash{$match}<2) {
## The replacement is as many spaces
## as $match has characters.
my $rep = " " x length($match);
## Replace it in the line
$line=~s/$match/$rep/;
}
## Print the line
print "$line\n";;
}
答え2
行間を読みながら、ジャンプや同様のコマンドで参照されていない行のアドレスを削除して、分解を読みやすくしたいとします。最後の列が「<」で始まる場合、awk は最後の列の数字がアドレスであると仮定します。逆アセンブリを一度読み込み、配列内のすべてのアドレスを覚えてからアドレスが表示されない場合は、2行目の行の先頭のアドレスを置き換えます。
$ objdump -d /bin/ls >/tmp/a
$ awk '
NR==FNR { if($NF ~ /</) address[$(NF-1)] = 1; next }
$1 ~ /:/ { if(!address[substr($1,1,length($1)-1)]){
i = index($0,":")
printf "%*s%s\n",i," ",substr($0,i+1)
next }
}
{ print }
' /tmp/a /tmp/a