Grep and replace

Samsung 55 series 8 grey led flat panel...
April 12, 2010 at 23:29:47
Specs: unix
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

Every time i grep a file name in the file, the counter has to get updated by 1.

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.......


regards,
giri


See More: Grep and replace

Report •

#1
April 13, 2010 at 07:57:56
I see know reason to use the while loop. Embed the loop and if statement within the awk script:

#!/bin/sh

filename=`grep -i a.sh filename.sh`
#echo "filename is: $filename"

a=`echo "$filename"|cut -d "|" -f4`
#echo "a is: $a"

b=`expr $a + 1`

nawk ' BEGIN { FS="|"; OFS="|" }
{
if($0 == "'"$filename"'")
   sub("'"$a"'", "'"$b"'", $4)
print $0
} ' filename.sh
# end script

Or you might consider placing everything within the awk script:

nawk ' BEGIN { FS="|"; OFS="|" }
{ 
if($0 ~ /a.sh/)
   $4=$4+1
print $0
} ' filename.sh



Report •

#2
April 13, 2010 at 21:30:44
Thanks nails......

Report •

#3
August 19, 2010 at 01:41:54
Select lines that start with space
select the lines from file1 that end with one
Select lines that start and end with space

Report •
Related Solutions


Ask Question