File redirection inside for loop

2019-09-06 05:37发布

问题:

I have am redirecting the file inside file but at the last the file is going to nullify, my code is like below

#!/bin/ksh
newlycreated=`cat /axphome/gdevarad/file.txt|awk '{print $1}'`

for i in $newlycreated
do

    cat file1.txt |grep -v $i > /axphome/gdevarad/file1.txt

done

file.txt contains

india    30
pakistan 40

file1.txt contains

india    30
pakistan 40
germany  50
japan    60
aus      70

回答1:

Replace > /axphome/gdevarad/file1.txt with >> /axphome/gdevarad/file1.txt so you do not overwrite your results in every loop.
Evn better: Place > /axphome/gdevarad/file1.txt after done .



标签: shell unix ksh