编辑文本文件以添加空格

编辑文本文件以添加空格

我有一个如下所示的文本文件

../pool/main/a/aalib/libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/libacl1-dev_2.2.52-3build1_amd64.deb
../pool/main/a/acpid/acpid_2.0.28-1ubuntu1_amd64.deb
../pool/main/a/acpi-support/acpi-support_0.142_amd64.deb
../pool/main/a/adduser/adduser_3.116ubuntu1_all.deb
../pool/main/a/adium-theme-ubuntu/adium-theme-ubuntu_0.3.4-0ubuntu4_all.deb
../pool/main/a/advancecomp/advancecomp_2.1-1_amd64.deb
../pool/main/a/adwaita-icon-theme/adwaita-icon-theme_3.28.0-1ubuntu1_all.deb
../pool/main/a/aisleriot/aisleriot_3.22.5-1_amd64.deb
../pool/main/a/alabaster/python3-alabaster_0.7.8-1_all.deb
../pool/main/a/alembic/alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/python3-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/python-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alsa-driver/alsa-base_1.0.25+dfsg-0ubuntu5_all.deb
../pool/main/a/alsa-driver/linux-sound-base_1.0.25+dfsg-0ubuntu5_all.deb

我想在最后一个斜杠和 deb 包文件的名称之间添加空格,例如

../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb

我尝试使用 sed,但无法正确完成。

答案1

sed 's_.*/_& _'

s将直到最后一个斜杠 (,正则表达式是贪婪的) 的所有内容替换为其自身,并在每一行中.*/后跟一个空格 ( )。&␣

示例运行

假设您的文本文件被调用file并且位于当前工作目录中:

$ <file sed 's_.*/_& _'
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/ accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/ acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1-dev_2.2.52-3build1_amd64.deb
../pool/main/a/acpid/ acpid_2.0.28-1ubuntu1_amd64.deb
../pool/main/a/acpi-support/ acpi-support_0.142_amd64.deb
../pool/main/a/adduser/ adduser_3.116ubuntu1_all.deb
../pool/main/a/adium-theme-ubuntu/ adium-theme-ubuntu_0.3.4-0ubuntu4_all.deb
../pool/main/a/advancecomp/ advancecomp_2.1-1_amd64.deb
../pool/main/a/adwaita-icon-theme/ adwaita-icon-theme_3.28.0-1ubuntu1_all.deb
../pool/main/a/aisleriot/ aisleriot_3.22.5-1_amd64.deb
../pool/main/a/alabaster/ python3-alabaster_0.7.8-1_all.deb
../pool/main/a/alembic/ alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/ python3-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alembic/ python-alembic_0.9.3-2ubuntu1_all.deb
../pool/main/a/alsa-driver/ alsa-base_1.0.25+dfsg-0ubuntu5_all.deb
../pool/main/a/alsa-driver/ linux-sound-base_1.0.25+dfsg-0ubuntu5_all.deb

要就地更改文件,请使用:

sed -i 's_.*/_& _' file

答案2

可以做的是使用模式分组并利用贪婪通配符:

$ echo '../pool/main/a/aalib/libaa1_1.4p5-44build2_amd64.deb' | 
> sed -r 's/(^.*\/)(.*\.deb)/\1 \2/'
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb

括号与\integer替换模式中的项目相对应。因此,第一个括号将贪婪地匹配最后一个斜杠之前的所有内容,第二个括号将贪婪地匹配其后的所有内容

问题中的输入文件:

$ sed -r 's/(^.*\/)(.*\.deb)/\1 \2/' pool.txt
../pool/main/a/aalib/ libaa1_1.4p5-44build2_amd64.deb
../pool/main/a/accountsservice/ accountsservice_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ gir1.2-accountsservice-1.0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/accountsservice/ libaccountsservice0_0.6.45-1ubuntu1_amd64.deb
../pool/main/a/acl/ acl_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
<...and so on and so forth...>

答案3

另外,awk版本

$ awk -F/ '{ OFS="/"; $NF=" "$NF; print; }' /tmp/XXXX
[...]
../pool/main/a/acl/ libacl1_2.2.52-3build1_amd64.deb
../pool/main/a/acl/ libacl1-dev_2.2.52-3build1_amd64.deb
[...]
  • -F/将字段分隔符设置为/
  • 对于每行输入,取自/tmp/XXXX包含您的数据
    • 将输出字段分隔符也设置/
    • NF包含字段数,$NF最后一个字段也是如此
    • 将最后一个字段设置为与原始最后一个字段值连接的空格字符串
    • 并打印

相关内容