参见英文答案 >
How to use sed/grep to extract text between two words?11个
我有一个名为’plainlinks’的文件,如下所示:
我有一个名为’plainlinks’的文件,如下所示:
13080. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94092-2012.gz 13081. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94094-2012.gz 13082. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94096-2012.gz 13083. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94097-2012.gz 13084. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94098-2012.gz 13085. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94644-2012.gz 13086. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94645-2012.gz 13087. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94995-2012.gz 13088. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-94996-2012.gz 13089. ftp://ftp3.ncdc.noaa.gov/pub/data/noaa/999999-96404-2012.gz
999999-94092 999999-94094 999999-94096 999999-94097 999999-94098 999999-94644 999999-94645 999999-94995 999999-94996 999999-96404
解决方法
使用sed:
sed -E 's/.*\/(.*)-.*/\1/' plainlinks
输出:
999999-94092 999999-94094 999999-94096 999999-94097 999999-94098 999999-94644 999999-94645 999999-94995 999999-94996 999999-96404
要将更改保存到文件,请使用-i选项:
sed -Ei 's/.*\/(.*)-.*/\1/' plainlinks
sed -E 's/.*\/(.*)-.*/\1/' plainlinks > newfile.txt
说明:
s/ # subsitution .* # match anything \/ # upto the last forward-slash (escaped to not confused a sed) (.*) # anything after the last forward-slash (captured in brackets) - # upto a hypen .* # anything else left on line / # end match; start replace \1 # the value captured in the first (only) set of brackets / # end