Issue
Part of input file with many lines:
.class xyz.com class.name_1 and xxx.co .class class.man_3 'abc.name_4' #id
#id .class abc.name class.name and class.name_2 class.name_3 .class class.dragon class.xyz
Desire Output:
.class 'xyz.com' 'class.name_1' and 'xxx.co' .class 'class.man_3' 'abc.name_4' #id
#id .class 'abc.name' 'class.name' and 'class.name_2' 'class.name_3' .class 'class.dragon' 'class.xyz'
should handle this input as well:
.xxx-xxxx.xxxx-xxxx
.xxxx.xxxx
xic.aco-eiopw-ejek.cojdj
output
.xxx-xxxx.xxxx-xxxx
.xxxx.xxxx
'xic.aco-eiopw-ejek.cojdj'
I tried using this sed "s/^\(.* \)\(.*\.\)\(.* \)/\'1\2\3'/g"
It gives following output:
'1class.name_4 #id '
'1class.name_4 '
....
Solution
Here is an awk
version
awk -v s="'" '!/^#/{for(i=1;i<=NF;i++) if($i~"^[^.].*[.]" && $i!~"^"s) $i=s$i s}1' file
.class 'xyz.com' 'class.name_1' and 'xxx.co' .class 'class.man_3' 'abc.name_4' #id
#id .class abc.name class.name and class.name_2 class.name_3 .class class.dragon class.xyz
s="'"
# set s to singe quote (simpler to handle this way)
!/^#/
# run next part only if line does not start with #
{for(i=1;i<=NF;i++)
# Loop trough all fields on the line
if($i~"^[^.].*[.]"
# Test if field has an dot, but not as first character
&& $i!~"^"s
# And it also does not start with single quote (the value of s)
$i=s$i s
# Add single quote on both side of field
1
# print the line (always true)
Answered By - Jotne Answer Checked By - Candace Johnson (WPSolving Volunteer)