sed or awk Solution

Hi

I am having a csv file like this

ahsh,90.82,add,32424,ahha
hhdh,98.89,hdhdh,92728,neha
hshs,you,97.7,hdhdhd,are,a 
jsjsj,wonderful,9788,79.9,aheh
ahdh,95.5,girl, 2737,jolll

I need to add width="100" to the value which is greater than 90 like decimal points but less than 100

Output file 


ahsh,width="100" 90.82,add,32424,ahha
hhdh,width="100" 98.89,hdhdh,92728,neha
hshs,you,width="100" 97.7,hdhdhd,are,a 
jsjsj,wonderful,9788,79.9,aheh
ahdh,width="100" 95.5,girl, 2737,jolll

I tried to use this sed script

sed 's/[9][0-9]\.[0-9]*/width="100" [9][0-9]\.[0-9]*/g' file 

But it is adding [9][0-9]\.[0-9]* to the prior numbers
Prior numbers I need as it is as shown in output file
Please help me out how to tackle this ?

Thanks
Kshitij

Which column to refer to ? You have 2nd have 3rd column in your case.

1 Like

Try :

$ cat file
ahsh,90.82,add,32424,ahha
hhdh,98.89,hdhdh,92728,neha
hshs,you,97.7,hdhdhd,are,a 
jsjsj,wonderful,9788,79.9,aheh
ahdh,95.5,girl, 2737,jolll
$ awk -F, 'match($0,/[0-9]+\.[0-9]+/){value = substr($0,RSTART,RLENGTH)+0; if(value > 90 && value < 100 )gsub(value,"width=" q 100 q " &")}1' q='"' OFS=',' file

Resulting

ahsh,width="100" 90.82,add,32424,ahha
hhdh,width="100" 98.89,hdhdh,92728,neha
hshs,you,width="100" 97.7,hdhdhd,are,a 
jsjsj,wonderful,9788,79.9,aheh
ahdh,width="100" 95.5,girl, 2737,jolll
1 Like

Or adjusting your original sed script:

sed 's/[9][0-9]\.[0-9]*/width="100" &/g' file
1 Like

try also:

awk -F, '{f=0; for (i=1;i<=NF;i++){a=$i;if(!f && a+=0)f=i} ; if ($f<100) $f="witdh=\"100\" " $f} 1' OFS=, infile
1 Like

Thanks a lot everyone