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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to filter the words, if that word contains the expected letter
# 1  
Old 03-18-2008
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 expecting is :
123abc456 23134abchfhj43 gc32abc abc1
2abc3

Regards
Venu
# 2  
Old 03-18-2008
Your example is not very clear. You mean, extract and print those tokens which contain the string "abc"?

Code:
vnix$ perl -lane 'print join (" ", grep { /abc/ } @F)'
foo fabci canbc cabca fnabca 123ab23c babco swill <- input
fabci cabca fnabca babco
snort abc foo <- input
abc
^D

Do you want to preserve newlines? (This solution does.) What about spaces between the tokens? (This solution normalizes them to one space between each.)

Last edited by era; 03-18-2008 at 12:26 PM.. Reason: Show which lines are input
era
# 3  
Old 03-18-2008
With Z-Shell:

Code:
print ${(M)$(<file)##*abc*}

# 4  
Old 03-19-2008
Hi era,

Your solution is in perl, I need it in unix.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove repeated letter words

Hi, I have this text file with these words and I need help with removing words with repeated letter from these lines. 1 ama 5 bib 29 bob 2 bub 5 civic 2 dad 10 deed 1 denned 335 did 1 eeee 1 eeeee 2 eke 8... (4 Replies)
Discussion started by: crepe6
4 Replies

2. Shell Programming and Scripting

Counting all words that start with a capital letter in a string using python dictionary

Hi, I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key. #!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. UNIX for Dummies Questions & Answers

Delete all words not containing letter /s/

I have a word file that looks like: pens binder spiral user I want to delete all the words without the letter /s/, so output looks like: pens spiral user I tried using sed: sed '//d' infile.txt > out.txt (5 Replies)
Discussion started by: pxalpine
5 Replies

4. Shell Programming and Scripting

Make all words begin with capital letter?

I need to use bash to convert sentences where all words start with a small letter into one where all words start with a capital letter. So that a string like: are utilities ready for hurricane sandy becomes: Are Utilities Ready For Hurricane Sandy (10 Replies)
Discussion started by: locoroco
10 Replies

5. Shell Programming and Scripting

Script to compare 2 words (first and last letter only)

Hello, I need a script to do the following: I have a file filled of lines like: valu -> value confirmaton -> confirmation I need a script to compare the first and last letters of the words, for example for the line: valu -> value compare "v" to "v" and "u" to "e" and print the line... (7 Replies)
Discussion started by: bojomojo
7 Replies

6. Shell Programming and Scripting

Trying to capitalize first letter of every word in Variable

Total Bash noob, have been successful in doing my script by searching and looking at examples, but I need some assitance with this one, just can't figure it out. In the Bash script I am trying to capitalize the first letter of every word in a string, ideally not changing other capitalization. ... (5 Replies)
Discussion started by: randyharris
5 Replies

7. Shell Programming and Scripting

Taking letter from a word

Hi All, I am new to UNIX and i am trying to write a script.My requirement is that from the following logs i need to get the following outputs: abc_lifecycle.log bcde_enjoy.log abc_twinkle.log Output expecting: lifecycle enjoy twinkle Could you please help me in getting this? (9 Replies)
Discussion started by: JeiPrakash
9 Replies

8. Shell Programming and Scripting

matching a letter in a word

hi, if i have a string of letters and seperatly i have a single letter. how do i check whether that specific letter is in my string aswell? any ideas? (2 Replies)
Discussion started by: Furqan_79
2 Replies

9. Shell Programming and Scripting

First letter of each Word from a line

Hello All, I am new to UNIX, how do i get the First letter of each Word from a line in shell scripting. For Example line = "The Jack In The Box" I want to reterive The letters T for The, J from Jack, I from In, T from The and B from Box. and store in another string. Can anyone... (5 Replies)
Discussion started by: maxmave
5 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question