How Not to Delete Words that matches a PATTERN


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How Not to Delete Words that matches a PATTERN
# 1  
Old 04-25-2012
How Not to Delete Words that matches a PATTERN

Hi,
I have a test file name test.txt with its contents
Code:
string
21345
qwee
strinn
strriin
striin

i need to delete all the words except the word STRING
I used the command
Code:
cat test.txt | sed 's/^.[^tr].*[^g]$/**/g'

but the output entries still contain
Code:
strinn
strriin
striin.

Plz Help me out.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 04-25-2012 at 06:25 AM.. Reason: Please use code tags
# 2  
Old 04-25-2012
careful with the mv command, you are overwriting the test.txt
Code:
 
grep "string" test.txt > output.txt
mv output.txt test.txt

# 3  
Old 04-25-2012
Are u looking for this ?
Code:
 
sed -n '/string/p' test.txt

# 4  
Old 04-25-2012
Try this,
Code:
awk '/string/{print}' test.txt

Regards,
Indu
# 5  
Old 04-25-2012
Quote:
Originally Posted by Indumathy
Try this,
Code:
awk '/string/{print}' test.txt

Regards,
Indu
just for a information.

no need to use {print}

Code:
 
awk '/string/' test.txt

# 6  
Old 04-25-2012
This will not match words like "stringent" or "outstring".

Code:
grep -xF "string" infile

The -F is not really necessary, here but it will speed up grep and allow for characters like "." to retain its usual meaning.


These will also allow for white space before and after the word:
Code:
grep -wF "string" infile

Code:
awk '$1==s' s="string" infile


Last edited by Scrutinizer; 04-25-2012 at 07:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete row if column matches

Hi, I have a long file in the format below. I want to delete the consecutive lines that contain the same value in column 1. I have tried awk '!x++' FS="," filename This has not worked. 14,MM709_BHP_DM,BHP,BHP_MC709_DM 19,OFFLINE,CHE,CHEV_MC773_DM 20,M33,BP,BP_MIM775_NS_DM ... (2 Replies)
Discussion started by: ndnkyd
2 Replies

2. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

3. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

4. 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

5. Shell Programming and Scripting

script to delete lines from a txt file if pattern matches

File 6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04 42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36 6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies

6. UNIX for Dummies Questions & Answers

Vim help - delete words in a file or characters after pattern

I have a file with words that begin with character #. Whenver that character is found that word should be deleted throughout the file. How do I do that in VIM. e.g: afkajfa ladfa ljafa #222222 kjafad ljl afajkj kjlj uouu #44444 jlkj lkjl Output should be afkajfa ladfa ljafa kjafad... (1 Reply)
Discussion started by: osbourneric
1 Replies

7. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

8. Web Development

Retrieve all matches of occurences of words

Hi, I have rows like this. Retractor muscle (PRM) of Helix pomatia Genetic characterization of a prm- mutant Values of PRM data. Expression pattern of prmt5 in adult fish. PRMT-5 negatively regulates DNA genetic fusions of prM-E protein Methylation of Xilf3 by Xprmt1b... (1 Reply)
Discussion started by: vanitham
1 Replies

9. UNIX for Advanced & Expert Users

I am trying to find pattern between two words but unable to get that pattern..

HI.... It's fallow up file .. #./show.sh click enter button.. i am gettng the fallowup file. its keep on running every time why because there are lots of users working on it. In that file i want to search pattern between two words for ex: SELECT DISTINCT... (7 Replies)
Discussion started by: ksr.test
7 Replies

10. Shell Programming and Scripting

Extract if pattern matches

Hi All, I have an input below. I tried to use the awk below but it seems that it ;s not working. Can anybody help ? My concept here is to find the 2nd field of the last occurrence of such pattern " ** XXX ccc ccc cc cc ccc 2007 " . In this case, the 2nd field is " XXX ". With this "XXX" term... (20 Replies)
Discussion started by: Raynon
20 Replies
Login or Register to Ask a Question