Search and replace a number

April 9, 2010 at 03:55:35
Specs: unix
Hi,


In a file i have got the below result

User |id |file|count
--------------------------
oracle|0123|a.sh|0
oracle|0124|b.sh|1
oracle|0125|c.sh|3
oracle|0126|d.sh|0
oracle|0127|e.sh|1
oracle|0128|f.sh|3

when i grep for a.sh every time my count has to increase by 1 for that record.
i have got how to extract the single character


grep -i a.sh filename|cut -d "|" -f4

can you help me in updating the count


See More: Search and replace a number

Report •

#1
April 9, 2010 at 06:51:14
a=`grep -i a.sh filename.sh|cut -d "|" -f4`
b=`expr $a + 1`
filename=`grep -i a.sh filename.sh`
while read line
do
echo $line
echo $filename
if [ $line = $filename ]
then
echo "entered if"
nawk ' BEGIN { FS="|"; OFS="|" }
{ sub('$a', '$b', $4)
print $0}' filename.sh
fi
echo "exit if"
done < filename.sh


filename.sh
User |id |file|count
--------------------------
oracle|0123|a.sh|0
oracle|0124|b.sh|1
oracle|0125|c.sh|3
oracle|0126|d.sh|0
oracle|0127|e.sh|1
oracle|0128|f.sh|3


expected output
User |id |file|count
--------------------------
oracle|0123|a.sh|1
oracle|0124|b.sh|1
oracle|0125|c.sh|3
oracle|0126|d.sh|0
oracle|0127|e.sh|1
oracle|0128|f.sh|3


i tried this command, if the counter is same for other line, then changes for all the line which are same numbers, i require for single line update, please help me.......


Report •
Related Solutions


Ask Question