$ 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
linux/bash: remove text between two chars
题
I have a file file1
and it contains:
< 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
I want to remove everything between <
and ./
(or between \n
and ./
) excluding ./
, so the output would be the following:
./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
I tried these commands (their output was the exact input file):
$ sed -n '/</,/ ./ p' file1
$ sed 's/< .* " ."/./' file1
$ sed -e 's/<\n[^ .]>/<\n.>/g' file1
$ sed -e 's/\(<\).*\(.\)/\1\2/' file1
Probably it’s only a simple thing, but I am new at sed/awk/tr/grep/find commands.
解决方案
其他提示
How about (or am I missing something):
$ 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
Using 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
Assuming there are no directories ending with <space>.
.
You can use BASH regex matching.
while read -r; do
[[ $REPLY =~ .*(\.\/.*) ]] && echo ${BASH_REMATCH[1]}
done < file
Bash
while read -r _ _ file_details; do
echo "$file_details"
done < your_file > your_file.edited
Awk
awk '{ $1=""; $2=""; print; }'
OK, if those first white spaces are really irksome:
awk -F. '{printf ".";print $2}'
不隶属于 StackOverflow