以下のようにcsvファイルがあります。
ADRESSE_1,ADRESSE_SUITE,CODE
1 boulevard Veyrier Montagnères,,33120
2, rue du Débarcadère,33120
6 bis avenue du Général de Gaulle,,44180
avenue du Parc Pereire,,93250
数百行のファイルを3行にまとめました。
このファイルをクリーンアップして編集して、次のように作成したいと思います。
NUMERO,ADRESSE_1,ADRESSE_SUITE,CODE
1,boulevard Veyrier Montagnères,,33120
2,rue du Débarcadère,,33120
6 bis,avenue du Général de Gaulle,,44180
,avenue du Parc Pereire,,93250
行の範囲は 16 ~ 17 列で、printf を使用してこのスクリプトを書式設定することができました。
BEGIN {
FS = "[,]";
OFS = ",";
}
NF != 16 {printf("%s,%s,%s,%s,%s%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"), $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17}
ベストアンサー1
私は私の解決策を一緒に投げ、sed
特別にAWKが必要であるにもかかわらず、この解決策はより簡単で簡単であることがわかりました。
GNU Sed(CentOSでテスト済み):
sed -n '1!p' addresses.csv | sed -r 's!^([0-9]*(\sbis|\ster)?),?(.*)$!\1,\3!g;s!(.*)([^,])(,[0-9]*)$!\1\2,\3!g'
OS-X/BSDセッション
sed -n '1!p' addresses.csv | sed -E 's!^([0-9]*( bis| ter)?),?(.*)$!\1,\3!g;s!(.*)([^,])(,[0-9]*)$!\1\2,\3!g'
最初のsedコマンドは、最初の行(ヘッダー)を除くすべての行を取得することです。
第二に、sed
交換を使用します。
^ : Starting text.
[0-9]* : all numbers (0, 1, ... 99, 999, 99999999 and so on)
( bis| ter)? : optionally followed by " bis" or " ter" (notice the space before); group 2
,? : optionally followed by a comma
(.*)$ : the rest of the string until the end ($) (group 3)
!\1,\3 : replaced by first group (number + extension) - comma - third group
2番目のグループは「bis」と「ter」の括弧で、最初のグループは次のようになります。([0-9]*( bis| ter){0,1})
2番目の置換はコンマを正規化することです(まだ完了していない場合は、追加,,\d
のコンマを追加します)。