Update a field in a file based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Update a field in a file based on condition
# 1  
Old 12-07-2010
Update a field in a file based on condition

Hi

i am new to scripting. i have a file file.dat with content as :

Code:
 
CONTENT_STORAGE PERCENTAGE FLAG:
/storage_01 64% 0
/storage_02 17% 1

I need to update the value of FLAG for a particular CONTENT_STORAGE value

I have written the following code
Code:
 
 #!/bin/sh
threshold=20
alert="threshold"
#Get default file store 
defaultStorage1=DEFAULT_STORE 
#DEFAULT_STORE is obtained as storage_01 from database table
 
echo "file reading"
{
 
while read CONTENT_STORAGE PERCENTAGE FLAG
do
 
    #Code for reading through lines and ignoring all until CONTENT_STORAGE is found
 
    # Ignore all lines until content storage namematches the name given 
    if [[ ${CONTENT_STORAGE} != $defaultStorage1 ]]; then
 echo "here"
        continue
    fi   
echo "CONTENT_STORAGE = ${CONTENT_STORAGE} / PERCENTAGE = ${PERCENTAGE} / FLAG = ${FLAG}" 
done < file.dat 
}
echo "Getting the percentage og $defaultStorage1 *********************"
#check the percentage stored in defaultStorage
df -H | grep $defaultStorage1 | awk '{ print $4 " " $5}' | while read output;
do 
  #Get the used percentage
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
 echo "used percentage .................. $usep"
# Check if the used percentage is > threshold_value. Do nothing till its > threshold
   if [ $(($usep)) -le $(($threshold)) ]; then
 echo "Used percentage of $defaultStorage1 ................. $(($usep))"
       continue
 
else 
 
echo "Used percentage is greater than threshold"
 
    # Ignore all lines until content storage namematches the name given 
    if [[ ${CONTENT_STORAGE} != $defaultStorage1 ]]; then
        continue
    else
       $FLAG = 1  
#i am trying to update the FLAG field in here. But not happening. Also while creating the tmp file file.mp and rewriting to file.dat, the headings are ignored. i want to just modify the FLAG value for /storage1 to 1
    fi 
    echo $CONTENT_STORAGE $PERCENTAGE $FLAG >> file.tmp
  done < file.dat  
mv file.tmp file.dat
   # check the percentage of each other filestore with flag =0
   #get the least used filestore
   #update the default store
 
   fi
 
done

Thanks for all the help in advance
# 2  
Old 12-07-2010
In sh one says FLAG=1 ?

Updating a field in a file is pretty hard in sh. You can make a new file beside or in place of the original, or append.

The df command will do specific dirs without a grep.

I hope the first line leading space is a pasting typo.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Beginners Questions & Answers

Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix. Here is my condition for(i=1;i<=NF;i++) { print "<td> "$i"</td> } Please use CODE tags when displaying sample input, output, and code segments. (17 Replies)
Discussion started by: CatchMe
17 Replies

3. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

4. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Perl to update field based on a specific set of rules

In the perl below, which does execute, I am having trouble with the else in Rule 3. The digit in f{8} is extracted and used to update f accordinly along with the value in f. There can be either - * or + before the number that is extracted but the same logic applies, that is if the value is greater... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. Shell Programming and Scripting

Perl to update field in file based of match to another file

In the perl below I am trying to set/update the value of $14 (last field) in file2, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (4 Replies)
Discussion started by: cmccabe
4 Replies

7. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

8. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

9. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies
Login or Register to Ask a Question