Sed filter words from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed filter words from string
# 1  
Old 09-09-2010
Sed filter words from string

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.
# 2  
Old 09-09-2010
Code:
echo "sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage" |grep -o "[[:graph:]]*\.[[:graph:]]*"

# 3  
Old 09-09-2010
I don't know about sed, but here's one way to do it with Perl -

Code:
$ 
$ 
$ 
$ 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
# 4  
Old 09-09-2010
Code:
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.:
Code:
pat="[^ \t]\{1,\}\.[^ \t]\{1,\}"
echo $str | sed "s|$pat|&\n|g" | sed -n "s|.*[ \t]\($pat\).*|\1|p"

# 5  
Old 09-09-2010
Another one with awk:
Code:
echo $str | awk '/\./' RS=" "

# 6  
Old 09-09-2010
Quote:
Originally Posted by Franklin52
Another one with awk:
Code:
echo $str | awk '/\./' RS=" "

Nice and short, but that would also filter out the dot at the end of a sentence, no?
# 7  
Old 09-09-2010
Quote:
Originally Posted by Scrutinizer
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?

Quote:
Originally Posted by amicon007
I want to filter "word.word" pattern from it. I assume the pattern won't occur in start or end of the string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to filter string between specific string in ksh

My argument has data as below. 10.9.9.85 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr56scr 127869538 ....... (note all these between lines will start with hyphen '-' ) -rwxr-xr-x user1 2019-10-15 17:40... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Filter all the lines with minimum specified length of words of a text file

Hi Can someone tell me which script will work best (in terms of speed and simplicity to write and run) for a large text file to filter all the lines with a minimum specified length of words ? A sample script with be definitely of great help !!! Thanks in advance. :) (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Perl script to filter the string

Hi folks, I have a log file with the lines in the below format. Jul 1 23:00:51 10.212.3.251 SS: %SYS-7-CLI_SCHEDULE: some error occured I want to split the line based on the " %SYS-7-CLI_SCHEDULE: " value. The criteria is the should store the word that starts with % i.e., ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. UNIX for Dummies Questions & Answers

Filter logs for key words and create an index

well, im a total novice of unix commands and shell scripting, i never made anything. But yesterday in my work i was working with a lot of log files and i was wondering if there is a command for doing something like this: i have a bunch of files with text like this: blablabla errorcode1... (11 Replies)
Discussion started by: matius_88
11 Replies

5. UNIX for Dummies Questions & Answers

Filter a particular string

Hi, I have a long log file. Out of which I want to filter particular occurrences where APC=4-033-0. Please help how it can be done. Input : +++ ELEMENT 12-05-27 23:15:06 CC 3482 #040185 > REPT APB TPE=C7NTL SM=22 OPC=4-003-7 APC=4-033-4 inaccessible END OF REPORT #040185 ++- ... (7 Replies)
Discussion started by: vanand420
7 Replies

6. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

7. Shell Programming and Scripting

[sed] extract words from a string

Hi, One of the scripts creates logs in the format: progname_file1.log.20100312020657 where after file the number could be from 1 to 28 and after log. the date is attached in the format YYYYMMDDHHMISS progname_file<1-28>.log.YYYYMMDDHHMISS. Now I want to discard the .20100312020657... (7 Replies)
Discussion started by: dips_ag
7 Replies

8. Shell Programming and Scripting

Filter NOT words

Hi, I have string like this (for example) $str="orange AND grapes NOT pineapple"; $str="orange AND grapes NOT pineapple AND papaya"; $str="orange AND grapes NOT pineapple AND jackfruit NOT "fruit juice""; I have to filter out NOT terms. How can i do that? I used this... (3 Replies)
Discussion started by: vanitham
3 Replies

9. UNIX for Advanced & Expert Users

How to filter the words, if that word contains the expected letter

Hi, I am trying to filter the words from a file which contain 'abc'. But I am unable to. Could any one help me. For eg: The file contents are 123ab 12hnj1 123abc456 123cgbcahjkf23 23134abchfhj43 gc32abc abc1 2abc3 sd uiguif fhwe 21242 uh123 jkcas124d123 u3hdbh23u ffsd8 Output... (3 Replies)
Discussion started by: venu_eie
3 Replies

10. Shell Programming and Scripting

filter the string from a file ??

I have a file in which there are strings given below: inode 1138932c(mqsiadm) inode 1257904c(mqsiadm) I want to get the nos. only form these. The o/p should be 1138932 1257904 How to filter this out ?? (11 Replies)
Discussion started by: varungupta
11 Replies
Login or Register to Ask a Question