Skip to content
Advertisement

Bash issue with floating point numbers in specific format

(Need in bash linux)I have a file with numbers like this

1.415949602
91.09582241
91.12042924
91.40270349
91.45625033
91.70150341
91.70174342
91.70660043
91.70966213
91.72597066
91.7287678315
91.7398645966
91.7542977976
91.7678146465
91.77196659
91.77299733
abcdefghij
91.7827827
91.78288651
91.7838959
91.7855
91.79080605
91.80103075
91.8050505

sed 's/^91.//' file (working) 

Any way possible I can do these 3 steps?

1st I try this

cat input | tr -d 91. > 1.txt (didnt work) 
cat input | tr -d "91." > 1.txt (didnt work) 
cat input | tr -d '91.' > 1.txt (didnt work) 

then

grep -x '.{10}' (working)  

then

grep "^[6-9]" (working)

Final 1 line solution

cat input.txt | sed 's/91.//g' | grep -x '.{10}'  | grep "^[6-9]" > output.txt

Advertisement

Answer

Your “final” solution:

cat input.txt |
sed 's/91.//g' |
grep -x '.{10}' |
grep "^[6-9]" > output.txt

should avoid the useless cat, and also move the backslash in the sed script to the correct place (and I added a ^ anchor and removed the g flag since you don’t expect more than one match on a line anyway);

sed 's/^91.//' input.txt |
grep -x '.{10}' |
grep "^[6-9]" > output.txt

You might also be able to get rid of at least one useless grep but at this point, I would switch to Awk:

awk '{ sub(/^91./, "") } /^[6-9].{9}$/' input.txt >output.txt

The sub() does what your sed replacement did; the final condition says to print lines which match the regex.

The same can conveniently, but less readably, be written in sed:

sed -n 's/^91.([6-9][0-9]{9})$/1/p' input.txt >output.txt

assuming your sed dialect supports BRE regex with repetitions like [0-9]{9}.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement