【发布时间】:2018-08-25 23:50:43
【问题描述】:
我想查找并删除特定行,但它只能部分工作
我的文件:(文件名、日期、计数、文件路径)
test1.txt;2018-03-16;2;/path/to/file1
test2;2018-03-16;2;/path/to/file1
test3.txt;2018-03-16;2;/path/to/file2
脚本参数:
-
$1= 目录(最后一列) -
$2= 文件名(第一列)
我的目标:
- A) 我想保存以 $2 结尾的列的文件名并将它们写出来
- B) 我想用 Given $1 和 $2 参数删除整行
到目前为止我得到了什么:
#This only gets the last part of path/to/file -> 'file' in this case
EndOfDirPath=`echo "$1" | sed 's/\(^.*\)\/\(.*\)/\2/'`
#This removes the files starting with the inputed filename and last part of #directory
sed -i '/^'$2'.*'$EndOfDirPath'$/d' file
我想要它做什么:
$ foo test2 /path/to/file1
test1.txt
test2
文件应该是这样的:
test1.txt;2018-03-16;2;/path/to/file1
test3.txt;2018-03-16;2;/path/to/file2
任何提示都会有所帮助!
【问题讨论】:
-
即使您的论点是
/path/to/file1,为什么还要在输出中期待test3.txt;2018-03-16;2;/path/to/file2?? -
这不是输出,这只是使用文件参数运行所需脚本后的文件。 foo(name of the script) test2 (-> $1) /path/to/file1(->$2) 当我运行脚本时我想在标准输出上写入与 $2 具有相同路径的文件名而不是我想删除 $1 (test2) 如果它也有相同的 $2(Path) 否则文件保持不变