oldfile=test.csv
read -p "Enter File location: "$savelocation1
read -p "Enter File name: " $newfile1
grep "foobar" $oldfile > $savelocation1/$newfile1
awk ' BEGIN {FS=","}
{
printf "%-5s %-10s" \n, $1, $3
} ' < $savelocation1/$newfile1
Run Code Online (Sandbox Code Playgroud)
grep 将创建一个名为的新文件$newfile
,该文件仅包含"foobar"
来自的行,$oldfile
但是当我执行 awk 以将第一列和第三列打印到$newfile1
问题中时,问题在于它没有将 awk 的输出写入 ,$newfile1
但它只是将结果打印到终端。
编辑 - 我使用一个临时文件来存储然后输出然后将其传输到原始文件。但是,由于某种原因,这只适用于 grep 而不适用于任何 awk 语句。
$savelocation1/$oldfile> $savelocation1/tempfile.csv && mv $savelocation1/tempfile.csv $savelocation1/$newfile
Run Code Online (Sandbox Code Playgroud)