简体   繁体   English

如何将 vim 正则表达式替换命令转换为 sed 或 awk?

[英]How to convert a vim regex replace command to sed or awk?

How would one apply this command I'm using in vim to sed or awk?如何将我在 vim 中使用的这个命令应用到 sed 或 awk?

:%s/\v\n(\D)/ \1/g

Explanation解释

  • :% : Complete buffer :% : 完整缓冲区
  • s/ : Substitute s/ : 替换
  • \v : Use regex magic...I frankly still don't understand this \v :使用正则表达式魔法...坦率地说,我仍然不明白这一点
  • \n : Match new line \n : 匹配新行
  • (\D) : Match 'Not a digit'. (\D) :匹配“不是数字”。 Surrounded by braces to mark it as group用大括号包围以将其标记为组
  • / \1/g : Replace matches with space and group 1 / \1/g :用空格和第 1 组替换匹配项
  • /g : Confirm replace for all occurences /g :确认替换所有出现的地方

INPUT输入

Datum   Transaktion Branche/Partner Verrechnet  Belastung   Gutschrift  Bonuspunkte
24.12.2017  "Zinsen*
Zinsperiode: vom 24.11. bis 24.12.
Zins auf EUR 23'001'011.43 vom 20.12.-20.12. EUR 121.31
Zins auf EUR 23'002'045.73 vom 21.12.-23.12. EUR 173.99
Zins auf EUR 23'006'067.38 vom 24.12.-24.12. EUR 191.33"        Ja  239.42      0.0
23.12.2017  "Acme Ent.
Lebensmittelgeschäft
"   Lebensmittelgeschäft   Ja  121.65      121.7
20.12.2017  "Restaurant Lorem ipsum
Restaurant
"   Restaurant  Ja  15.00       15.0

OUTPUT输出

Datum   Transaktion Branche/Partner Verrechnet  Belastung   Gutschrift  Bonuspunkte
24.12.2017  "Zinsen* Zinsperiode: vom 24.11. bis 24.12. Zins auf EUR 23'001'011.43 vom 20.12.-20.12. EUR 121.31 Zins auf EUR 23'002'045.73 vom 21.12.-23.12. EUR 173.99 Zins auf EUR 23'006'067.38 vom 24.12.-24.12. EUR 191.33"        Ja  239.42      0.0
23.12.2017  "Acme Ent. Lebensmittelgeschäft "   Lebensmittelgeschäft    Ja  121.65      121.7
20.12.2017  "Restaurant Lorem ipsum Restaurant "    Restaurant  Ja  15.00       15.0

Awk equivalent would look as follows: Awk等效项如下所示:

awk '{ printf "%s%s", (NR==1? "" : (/^[0-9]/? ORS : OFS)), $0 }END{ print "" }' file
  • OFS - output field separator (defaults to space char) OFS - 输出字段分隔符(默认为空格字符)
  • ORS - output record separator ORS - 输出记录分隔符

The output:输出:

Datum   Transaktion Branche/Partner Verrechnet  Belastung   Gutschrift  Bonuspunkte
24.12.2017  "Zinsen* Zinsperiode: vom 24.11. bis 24.12. Zins auf EUR 23'001'011.43 vom 20.12.-20.12. EUR 121.31 Zins auf EUR 23'002'045.73 vom 21.12.-23.12. EUR 173.99 Zins auf EUR 23'006'067.38 vom 24.12.-24.12. EUR 191.33"        Ja  239.42      0.0
23.12.2017  "Acme Ent. Lebensmittelgeschäft "   Lebensmittelgeschäft    Ja  121.65      121.7
20.12.2017  "Restaurant Lorem ipsum Restaurant "    Restaurant  Ja  15.00       15.0

Or just use vim in batch mode:) about 8 times slower, but it's an option.或者只是在批处理模式下使用 vim :) 大约慢 8 倍,但这是一个选项。

echo '%s/blah/BLAH/ | w'|vi -e file.cfg

Source: https://www.brianstorti.com/vim-as-the-poor-mans-sed/来源: https ://www.brianstorti.com/vim-as-the-poor-mans-sed/

sed , perl solutions: sed , perl解决方案:

perl perl

tr -d '\v\n\r' < input.txt | perl -pe 's/(\d{2}\.\d{2}\.\d{4})/\n\1/g'

sed sed

tr -d '\v\n\r' < input.txt | sed 's/\([0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{4\}\)/\n\1/g'

Output:输出:

Datum   Transaktion Branche/Partner Verrechnet  Belastung   Gutschrift  Bonuspunkte
24.12.2017  "Zinsen* Zinsperiode: vom 24.11. bis 24.12. Zins auf EUR 23'001'011.43 vom 20.12.-20.12. EUR 121.31 Zins auf EUR 23'002'045.73 vom 21.12.-23.12. EUR 173.99 Zins auf EUR 23'006'067.38 vom 24.12.-24.12. EUR 191.33"        Ja  239.42      0.0
23.12.2017  "Acme Ent. Lebensmittelgeschäft "   Lebensmittelgeschäft    Ja  121.65      121.7
20.12.2017  "Restaurant Lorem ipsum Restaurant "    Restaurant  Ja  15.00       15.0

Explanations:说明:

the tr command will remove all the \v , \n , and \r characters, then the sed command will add a new line before each date element to create your CSV structure. tr命令将删除所有\v\n\r字符,然后sed命令将在每个日期元素之前添加一个新行以创建 CSV 结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM