Hi,
I have a working script.
It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the moment.
I am posting this to get some help on how to use sed to get it to work. I've run out of things to try. OS is Solaris 5.9. Not sure how to check the sed version. I can't use sed -i, it gives error.
list.all is the input file that we want to search and delete line. Below is just an example. The original file is 10K lines long, it contains the absolute file path plus some other commands of sorts in between.
'/one/one/one/one',
'/two/two/two/two',
'/three/three/three/three',
'/four/four/four/four',
'/five/five/five/five',
'/six/six/six/six',
'/seven/seven/seven/seven',
'/eight/eight/eight/eight',
'/nine/nine/nine/nine',
'/ten/ten/ten/ten',
list.err contain the string that we want to search and delete line from list.all. We want to search for the string that is the first field and delete line containing that string.
/one/one/one/one: No such file or directory
/three/three/three/three: No such file or directory
/five/five/five/five: No such file or directory
/seven/seven/seven/seven: No such file or directory
/nine/nine/nine/nine: No such file or directory
Below is the script that I am using at the moment:
$: cat fix.ksh
#!/bin/ksh
#
source="list.all"
cp -p ${source}.save ${source}
echo
echo "------------------------------------------"
echo
echo "- This is the list that we want to cleanse"
echo
cat ${source}
echo
echo "------------------------------------------"
echo
echo ""
echo "============================"
echo "- sed not working :("
echo "============================"
echo ""
count=0
while read line
do
let count=$count+1
datafile=`echo $line | awk -F":" '{ print $1 }'`
echo "- $count // Checking $datafile ... "
grep -i "${datafile}" ${source}
sed "#${datafile}#d" ${source} > ${source}.tmp
cp -p ${source}.tmp ${source}
echo ""
done < list.err
echo
echo "==> Not working :("
echo "Contents of ${source} is as below:"
echo
cat ${source}
echo
echo "============================"
echo "- Using grep -v "
echo "============================"
echo
cp -p ${source}.save ${source}
count=0
while read line
do
let count=$count+1
datafile=`echo $line | awk -F":" '{ print $1 }'`
echo "- $count // Checking $datafile ... "
grep -v "${datafile}" ${source} > ${source}.tmp
cp -p ${source}.tmp ${source}
echo ""
done < list.err
echo
echo "==> Required Working Output:"
echo "Contents of ${source} is as below:"
echo
cat ${source}
echo
echo "============================"
echo "- Using grep and sed"
echo "============================"
echo
cp -p ${source}.save ${source}
count=0
while read line
do
let count=$count+1
datafile=`echo $line | awk -F":" '{ print $1 }'`
echo "- $count // Checking $datafile ... "
n=`grep -n "${datafile}" ${source} | awk -F":" '{ print $1 }'`
sed "${n}d" ${source} > ${source}.tmp
cp -p ${source}.tmp ${source}
echo ""
done < list.err
echo
echo "==> Required Working Output:"
echo "Contents of ${source} is as below:"
echo
cat ${source}
Below is an example run of the script:
$: ./fix.ksh
------------------------------------------
- This is the list that we want to cleanse
'/one/one/one/one',
'/two/two/two/two',
'/three/three/three/three',
'/four/four/four/four',
'/five/five/five/five',
'/six/six/six/six',
'/seven/seven/seven/seven',
'/eight/eight/eight/eight',
'/nine/nine/nine/nine',
'/ten/ten/ten/ten',
------------------------------------------
============================
- sed not working :(
============================
- 1 // Checking /one/one/one/one ...
'/one/one/one/one',
- 2 // Checking /three/three/three/three ...
'/three/three/three/three',
- 3 // Checking /five/five/five/five ...
'/five/five/five/five',
- 4 // Checking /seven/seven/seven/seven ...
'/seven/seven/seven/seven',
- 5 // Checking /nine/nine/nine/nine ...
'/nine/nine/nine/nine',
==> Not working :(
Contents of list.all is as below:
'/one/one/one/one',
'/two/two/two/two',
'/three/three/three/three',
'/four/four/four/four',
'/five/five/five/five',
'/six/six/six/six',
'/seven/seven/seven/seven',
'/eight/eight/eight/eight',
'/nine/nine/nine/nine',
'/ten/ten/ten/ten',
============================
- Using grep -v
============================
- 1 // Checking /one/one/one/one ...
- 2 // Checking /three/three/three/three ...
- 3 // Checking /five/five/five/five ...
- 4 // Checking /seven/seven/seven/seven ...
- 5 // Checking /nine/nine/nine/nine ...
==> Required Working Output:
Contents of list.all is as below:
'/two/two/two/two',
'/four/four/four/four',
'/six/six/six/six',
'/eight/eight/eight/eight',
'/ten/ten/ten/ten',
============================
- Using grep and sed
============================
- 1 // Checking /one/one/one/one ...
- 2 // Checking /three/three/three/three ...
- 3 // Checking /five/five/five/five ...
- 4 // Checking /seven/seven/seven/seven ...
- 5 // Checking /nine/nine/nine/nine ...
==> Required Working Output:
Contents of list.all is as below:
'/two/two/two/two',
'/four/four/four/four',
'/six/six/six/six',
'/eight/eight/eight/eight',
'/ten/ten/ten/ten',
I used
sed "#${datafile}#d" ${source}
instead of
sed "/${datafile}/d" ${source}
because the latter gives the error
First RE may not be null
For the moment, I stick with the grep -v option because the grep -n and sed "${n}d" option doesn't work if there is multiple match of the variable string. I've tried some of the ones posted to the forum that has a similar question and they doesn't work for me. The sed that I have doesn't allow the sed -i option.
Any advise much appreciated. Thanks in advance.