I have a small script where I want to see if a file exists & then delete the first line from it.
I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile:
This is what I have so far just to output if the file exists:
for a in $(cat ~/scripts/test.txt)
do
if [ -f $DEST/$a ]
then
echo file $a exists >> ~/scripts/test2.txt
else
echo file $a does not exist >> ~/scripts/test3.txt
fi
done
You can take the 2nd to last line of the file with below command
sed -n '2,$ p' filename
the problem is that I don't know how I can do sed to remove a line when the file name is only specified by "-f $DEST/$a" as I don't know how to put that into a variable.
I have added the sed statement in your script
for a in $(cat ~/scripts/test.txt)
do
if [ -f $DEST/$a ]
then
echo file $a exists >> ~/scripts/test2.txt
sed -n '2,$ p' $DEST/$a > $DEST/$a.tmp
mv $DEST/$a.tmp $DEST/$a
else
echo file $a does not exist >> ~/scripts/test3.txt
fi
done