I have following string in a variable:
str="sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage"
I want to filter "word.word" pattern from it. I assume the pattern won't occur in start or end of the string. Output shoud be:
tab.col1 tab2.col2 tab2.col4
Can this be done through sed? My UNIX flavor does not have any GNU extensions:
Thanks in advance.
rdcwayx
2
echo "sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage" |grep -o "[[:graph:]]*\.[[:graph:]]*"
I don't know about sed, but here's one way to do it with Perl -
$
$
$
$ echo "sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage" | perl -ne '
while(/[^.]* ([^. ]+)\.([^. ]+)/g){print "$1.$2 "} END {print "\n"}'
tab.col1 tab2.col2 tab2.col4
$
$
tyler_durden
for i in $str; do case $i in *?.?*) echo $i; esac; done
---------- Post updated at 10:27 ---------- Previous update was at 10:17 ----------
I think sed becomes too complicated for this purpose, e.g.:
pat="[^ \t]\{1,\}\.[^ \t]\{1,\}"
echo $str | sed "s|$pat|&\n|g" | sed -n "s|.*[ \t]\($pat\).*|\1|p"
Another one with awk:
echo $str | awk '/\./' RS=" "
Nice and short, but that would also filter out the dot at the end of a sentence, no?
IMHO the OP wants words with a dot between 2 words, no?
Yes.. Well I was able to do through sed by replacing the spaces in the string with \n and then applying the search pattern. Thanks for the help.