Results 1 to 2 of 2
Hi all I am trying to write a bash script that will recognize a sequence of numbers and will replace them according an if statement. Could you please let me ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-29-2014 #1
- Join Date
- Apr 2012
Assign rexeg result into var
I am trying to write a bash script that will recognize a sequence of numbers and will replace them according an if statement.
Could you please let me know how to assign that number that is recognized according to a regex into a variable? For instance, if my txt file looks like:
Text text text 15 text text text Text text text text text 7 ….
My code is :
while read line do number=$(echo $line | sed ‘s/[0-9][0-9]/g’) if [ $number >=16 ] then number=$( sed ‘s/([0-9])([0-9])/\2\1/g’) fi echo $number done <file.txt
I would appreciate your help.
- 05-29-2014 #2
- Join Date
- Dec 2013
You might find it works better if you use bashes own regex matching.
while read line do if [[ $line =~ ^[0-9]+$ ]]; then if [[ $line -ge 16 ]]; then echo $line fi fi done
while read line for each in $line do echo $each done done