This question already has an answer here:
-
Find and remove in unix
4 answers
I need to convert the below awk command into sed,
awk -F ',' '$2 ~ /^ *[0-9]*(\.[0-9]+)?" *$/{sub(/"/, "", $2); print $2}'
Below is my input file:
sample.txt:
3",3"
6-position,6-position
7' 4" to 10' 3-1/2",7' 4" to 10' 3-1/2"
4.8",4.8"
Adjustable from 99" to 111" - max 148,Adjustable from 99" to 111" - max 148
And i need the output as below,
output.txt:
3",3
6-position,
7' 4" to 10' 3-1/2",
4.8",4.8
Adjustable from 99" to 111" - max 148,
So basically I need to print the numeric value for the " symbol, other non-numeric text needs to be replaced by a empty line, the problem with awk command is the 2,3 and 5 line is getting removed.
as I commented, the awk one-liner doesn't make sense for your input file. However you could try this sed line:
sed -r '/^[0-9.]+"$/{s/"$//;n;};s/.*//' file
this works for your input file.
kent$ cat file
3"
6-position
7' 4" to 10' 3-1/2"
4.8"
Adjustable from 99" to 111" - max 148
kent$ sed -r '/^[0-9.]+"$/{s/"$//;n;};s/.*//' file
3
4.8
(here, the last line is empty too.)
EDIT
remove -r
option, and adjust the sed to work with new input/output:
sed 's/^\([^,]*,\)\([0-9.]*\)".*$/\1\2/;t;s/^\([^,]*,\).*/\1/' file
This line worked with your new input example:
kent$ cat file
3",3"
6-position,6-position
7' 4" to 10' 3-1/2",7' 4" to 10' 3-1/2"
4.8",4.8"
Adjustable from 99" to 111" - max 148,Adjustable from 99" to 111" - max 148
kent$ sed 's/^\([^,]*,\)\([0-9.]*\)".*$/\1\2/;t;s/^\([^,]*,\).*/\1/' file
3",3
6-position,
7' 4" to 10' 3-1/2",
4.8",4.8
Adjustable from 99" to 111" - max 148,
This works for me:
sed -e 's/^\([0-9]\+\(\.[0-9]\+\)*\)"$/\1/g' \
-e '/^\([0-9]\+\(\.[0-9]\+\)*\)$/! s/^.*$//g' sample.txt
You can do this with both, awk and sed.
Since I have no Linux (shell) on my phone I'll try to help out with a sed solution:
sed -e 's/^([0-9.]+)"$|.*/\1/g' < file
Should do the job