Reputation: 207
Below is my input file:
sample.txt
:
3"
6-position
7' 4" to 10' 3-1/2"
4.8"
Adjustable from 99" to 111" - max 148
and in the output I only need 3, i.e.
output.txt
:
3
4.8
So basically I need to print the numeric value for the "
symbol, other non-numeric text needs to be removed entirely.
I tried to implement this with sed
, but I was not able to get the desired result.
Is there any way to achieve this on UNIX?
Upvotes: 0
Views: 215
Reputation: 58488
This might work for you (GNU sed):
sed '/^[0-9.]\+"/!d;s/".*//' file
Upvotes: 0
Reputation: 785721
awk is more suited to perform this type of task:
awk '/^ *[0-9]*(\.[0-9]+)?" *$/{sub(/"/, ""); print}' inFile
OUTPUT:
3
4.8
Upvotes: 1
Reputation: 65821
One way with sed:
sed -n 's/^\([0-9]\+\(\.[0-9]\+\)\?\)"$/\1/p' sample.txt > out.txt
or with GNU sed
sed -rn 's/^([0-9]+(\.[0-9]+)?)"$/\1/p' sample.txt > out.txt
or with GNU grep
grep -oP '^[0-9]+(\.[0-9]+)?(?="$)' > out.txt
Be sure to use the correct inch mark (”
or "
). Or you can match both with a character class [”"]
.
Edit: updated to work for floating point numbers.
Upvotes: 1
Reputation: 7627
I think you are asking for grep -o [0-9][0-9]*\" sample.txt
Which will match one or more numbers followed my a '"', and print each occurrence separately and without surrounding text.
Upvotes: 0