次のようなテキストがあります:
A1JOURNEY0TO1
.BYTE 00, 00, 00
A2JOURNEY0TO2
.BYTE 00, 01, 00
A3JOURNEY1TO0
.BYTE 00, 01, 01
必要なもの:
JOURNEY_01 ; 00 TO 01
.BYTE 00, 00, 00
JOURNEY_02 ; 00 TO 02
.BYTE 00, 01, 00
JOURNEY_03 ; 01 TO 00
.BYTE 00, 01, 01
以下同様ですが、「;」は行の 41 文字目に置く必要があり、「TO」の前後に使用される値は行の先頭のテキスト文字列から取得されます。
答え1
JOURNEY
この詳細は、入力の可変性によって異なります。 が不変であり、それに追加したい数字が 2 文字以上または以下にならないと仮定すると、01-99
次のようになります ( )。
perl -pe 's/^.(\d+) ## ignore the first character and capture
## as many digits as possible after it.
(.+?) ## Capture everything until the next digit: 'JOURNEY'
(\d+)TO(\d+) ## Capture the two groups of digits on
## either side of "TO".
/ ## End match, begin replacement.
"$2_" . ## The 2nd captured group, 'JOURNEY'.
sprintf("%.2d",$1) . ## The number, 0-padded.
" " x 31 . ## 31 spaces.
sprintf("; %.2d TO %.2d",$3,$4) ## The start and end, 0-padded.
/ex; ## The 'e' lets us evaluate expressions in the substitution
## operator and the 'x' is only to allow whitespace
## and these explanatory comments
' file
上記は次のように要約することもできます。
perl -pe 's/^.(\d+)(.+?)([\d]+)TO(\d+)/"$2_" . sprintf("%.2d",$1). " " x 31 . sprintf("; %.2d TO %.2d",$3,$4)/e;' file
さまざまな文字列の長さも可変である場合は、それを考慮する必要があります。
perl -pe 's/^.+?(\d+)(.+?)([\d]+)TO(\d+)/
"$2_" . sprintf("%.2d",$1) .
" " x (41-length(sprintf("%.2d",$1) . "$2_")) .
sprintf("; %.2d TO %.2d",$3,$4)/xe;' file
答え2
awkを使って、あなたが望むことを推測する
ファイル ul.awk (編集済み)
/JOURNEY/ { jn=substr($1,2,1) ; x=substr($1,10,1) ; y=substr($1,13) ;
printf "JOURNEY_%02d%s; %02d TO %02d\n",jn,substr(" ",1,31),x,y ;
next ; }
{print ;}
そして実行する
awk -f ul.awk u
JOURNEY_01 ; 00 TO 01
.BYTE 00, 00, 00
JOURNEY_02 ; 00 TO 02
.BYTE 00, 01, 00
JOURNEY_03 ; 01 TO 00
.BYTE 00, 01, 01
数字は常に 1 桁であると想定していたため、これはやや不十分なコーディングです。