4

I have an example below where I need to replace the column 9 value if it is less than 8 else exit or ignore using sed or awk function:

) in datadbs extent size 16 next size 4 lock mode row; 

If I use the below awk function it only prints the value I need in column 9, but I still want to maintain the sentence structure.

echo ") in datadbs extent size 16 next size 4 lock mode row;" | awk '{if ($9 < 8 ) print 8;}' 

OUTPUT:

8 

What I want is the below:

) in datadbs extent size 16 next size 8 lock mode row; 

3 Answers 3

6

Without knowing any awk I'd suggest to change the parameter and print everything:

echo ") in datadbs extent size 16 next size 4 lock mode row;" | awk '{if ($9 < 8 ) $9 = 8; print;}' 
1
  • 2
    Or even just awk '$9 < 8 { $9 = 8 } 1' Commented Apr 12, 2017 at 13:44
3
sed -e 's/\S\+/&\n/9; s/ [0-7]\n/ 8/' 
2

sed alternative:

s=") in datadbs extent size 16 next size 4 lock mode row;" echo $s | sed 's/size [0-7] lock/size 8 lock/' 
2
  • As long as no-one writes the number there with leading zeroes Commented Apr 12, 2017 at 13:53
  • with leading zeroes - in that case the number can be treated as octal and that's not the case Commented Apr 12, 2017 at 14:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.