linux / bash:删除两个字符之间的文本

时间:2014-03-19 21:03:08

标签: bash replace sed awk tr

我有一个文件file1,它包含:

<     16 ./lnx/apps/vlc/tsconf_1.0-11_all.deb
<     16 ./lnx/apps/vlc/vlc
<   2000 ./lnx/apps/vlc/vlc-nox_2.0.8-1_i386.deb
<     16 ./lnx/apps/vlc/vlc-plugin-notify_2.0.8-1_i386.deb
<     32 ./lnx/apps/vlc/vlc-plugin-pulse_2.0.8-1_i386.deb
<     16 ./lnx/cmds/64bit_ubuntu_add_i386
<     16 ./lnx/cmds/acroread_dl
<     16 ./lnx/cmds/dl_from_gdrv
<     16 ./lnx/cmds/dpkg_install_list_in_txt_file
<     16 ./lnx/cmds/find_and_replace
<     16 ./lnx/cmd/pearl_script.ps1
<     16 ./lnx/cmds/rm_using_find
<     16 ./lnx/cmds/wget_dl_whl_ws

我想删除<./之间(或\n./之间)./除外的所有内容,因此输出如下:

./lnx/apps/vlc/tsconf_1.0-11_all.deb
./lnx/apps/vlc/vlc
./lnx/apps/vlc/vlc-nox_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-notify_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-pulse_2.0.8-1_i386.deb
./lnx/cmds/64bit_ubuntu_add_i386
./lnx/cmds/acroread_dl
./lnx/cmds/dl_from_gdrv
./lnx/cmds/dpkg_install_list_in_txt_file
./lnx/cmds/find_and_replace
./lnx/cmd/pearl_script.ps1
./lnx/cmds/rm_using_find
./lnx/cmds/wget_dl_whl_ws

我尝试了这些命令(它们的输出是确切的输入文件):

$ sed -n '/</,/ ./ p' file1
$ sed 's/< .* " ."/./' file1
$ sed -e 's/<\n[^ .]>/<\n.>/g' file1
$ sed -e 's/\(<\).*\(.\)/\1\2/' file1

可能这只是一件简单的事情,但我是sed / awk / tr / grep / find命令的新手。

5 个答案:

答案 0 :(得分:4)

$ sed 's/[^.]*//' file
./lnx/apps/vlc/tsconf_1.0-11_all.deb
./lnx/apps/vlc/vlc
./lnx/apps/vlc/vlc-nox_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-notify_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-pulse_2.0.8-1_i386.deb
./lnx/cmds/64bit_ubuntu_add_i386
./lnx/cmds/acroread_dl
./lnx/cmds/dl_from_gdrv
./lnx/cmds/dpkg_install_list_in_txt_file
./lnx/cmds/find_and_replace
./lnx/cmd/pearl_script.ps1
./lnx/cmds/rm_using_find
./lnx/cmds/wget_dl_whl_ws

答案 1 :(得分:3)

怎么样(或者我错过了什么):

$ awk '$0=$NF' file
./lnx/apps/vlc/tsconf_1.0-11_all.deb
./lnx/apps/vlc/vlc
./lnx/apps/vlc/vlc-nox_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-notify_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-pulse_2.0.8-1_i386.deb
./lnx/cmds/64bit_ubuntu_add_i386
./lnx/cmds/acroread_dl
./lnx/cmds/dl_from_gdrv
./lnx/cmds/dpkg_install_list_in_txt_file
./lnx/cmds/find_and_replace
./lnx/cmd/pearl_script.ps1
./lnx/cmds/rm_using_find
./lnx/cmds/wget_dl_whl_ws

答案 2 :(得分:2)

使用sed:

sed -i.bak 's~^<.*[[:space:]]\(\./\)~\1~' file

OR

sed -i.bak 's~^<[^.]*\(\./\)~\1~' file

./lnx/apps/vlc/tsconf_1.0-11_all.deb
./lnx/apps/vlc/vlc
./lnx/apps/vlc/vlc-nox_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-notify_2.0.8-1_i386.deb
./lnx/apps/vlc/vlc-plugin-pulse_2.0.8-1_i386.deb
./lnx/cmds/64bit_ubuntu_add_i386
./lnx/cmds/acroread_dl
./lnx/cmds/dl_from_gdrv
./lnx/cmds/dpkg_install_list_in_txt_file
./lnx/cmds/find_and_replace
./lnx/cmd/pearl_script.ps1
./lnx/cmds/rm_using_find
./lnx/cmds/wget_dl_whl_ws

假设没有以<space>.结尾的目录。

答案 3 :(得分:2)

您可以使用BASH正则表达式匹配。

while read -r; do
    [[ $REPLY =~ .*(\.\/.*) ]] && echo ${BASH_REMATCH[1]}
done < file

答案 4 :(得分:1)

while read -r _ _ file_details; do
  echo "$file_details"
done < your_file > your_file.edited

awk中

awk '{ $1=""; $2=""; print; }'

好的,如果那些第一个空格非常令人讨厌:

awk -F. '{printf ".";print $2}'