Pattern find and delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern find and delete
# 1  
Old 12-19-2014
Pattern find and delete

Hi Team,

Could someone please help me on this ..

While doing my day to day activity I use to delete manually the repeated ones ..

For example 05 & 06 are my hosts.. Now I need to print only 05 and 06, 05-06 is not required.

This is how I generate my report daily ..

Code:
DBG-STG1-05
DBG-STG1-05-06
DBG-STG1-06

I need it as

Code:
DBG-STG1-05
DBG-STG1-06


Here are the list of hosts..

My Current o/p

Code:
DBG-STG1-05
DBG-STG1-05-06
DBG-STG1-06
LNX-DEV-113
LNX-DEV-114
LNX-DEV-115
LNX-DEV-113-114-115
LNX-STG-322
LNX-STG-322-323
LNX-STG-322
SAN-MGR2
DMZ-001-L
DMZ-002-L
DMZ-001-L-002-L


Required o/p

Code:
DBG-STG1-05
DBG-STG1-06
LNX-DEV-113
LNX-DEV-114
LNX-DEV-115
LNX-STG-322
LNX-STG-322
SAN-MGR2
DMZ-001-L
DMZ-002-L

Thanks in advance ..

-Nalla

Last edited by Don Cragun; 12-19-2014 at 11:40 PM.. Reason: Add CODE tags.
# 2  
Old 12-20-2014
I'm lost.

You say: This is how I generate my report daily ..

Code:
DBG-STG1-05
DBG-STG1-05-06
DBG-STG1-06

But I don't see anything here that could be used to generate a report??? All I see are three strings on separate lines.

Are these the names of files in a directory? Are these strings contained in another file?

You say: While doing my day to day activity I use to delete manually the repeated ones ..
What does this mean???
Do you want to remove all files in the current directory that have three or more hyphens in their name? Do you want to remove every file in the current directory that has another filename in the same directory that is an initial substring of that name? Do you want to remove every line in a file if that line has three or more hyphens in the line? Do you want to remove every line from a file for which some other line is an initial substring of that line? (If this is what you're trying to do, is your input file sorted?) Do you want to print the lines in a file that have fewer than three hyphens (without changing the contents of the file)? Do you to print the lines in a file that do not have any other line in the file that is an initial substring of that line (without changing the contents of the file)? (If this is what you're trying to do, is you input file sorted?)
# 3  
Old 12-20-2014
Hi Don,

Sorry of the confusion..

The input specified by me will be in a single file.

Strings in the file ..
Code:
LNX-DEV-113
LNX-DEV-114
LNX-DEV-115
LNX-DEV-113-114-115

How to sort\grep as below
Code:
LNX-DEV-113
LNX-DEV-114
LNX-DEV-115

Need to remove the strings which are having hyphens 3 or more in the file .

Thanks in advance ..

-Nalla

Last edited by Scrutinizer; 12-20-2014 at 12:40 PM.. Reason: code tags
# 4  
Old 12-20-2014
Try:
Code:
grep -v '.*-.*-.*-.*' file

# 5  
Old 12-20-2014
test this

Code:
awk -F- '{if (NF<4)print}' yourFile

# 6  
Old 12-20-2014
Quote:
Originally Posted by protocomm
test this

Code:
awk -F- '{if (NF<4)print}' yourFile

On most systems, grep is smaller (and faster) than awk for something like this. But, if you want to use awk, it can be simplified to:
Code:
awk -F- 'NF<4' yourFile

But, if you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find to delete lines with pattern and even or odd number

In the below directory I am trying to delete all lines with a .bam extention that have the pattern IonCode_ followed by an even number. I am also trying to delete all lines with a .fastq extention that have the pattern IonCode_ followed by an odd number. I was going to use find but can see all... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Multiple pattern find and delete line

I have a file # cat /tmp/user_find.txt /home/user/bad_link1 /home/user/www /home/user/mail /home/user/access_logs /home/user/bad_link2 I need to delete lines where there are patterns /home/user/www, /home/user/mail and /home/user/access_logs. I used below method, but its throwing error... (8 Replies)
Discussion started by: anil510
8 Replies

4. Shell Programming and Scripting

Find command to delete a pattern

Hi all i have a directory where it has files as shown below.Using find command how can i delete files which were modified more than 20 days ago and having the pattern jnhld15231 or jnhld15232. find ./ -name "jnhld15231^" -type f -mtime +20 -exec rm {} \; find ./ -name "jnhld15232^" -type f... (2 Replies)
Discussion started by: morbid_angel
2 Replies

5. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

6. Shell Programming and Scripting

sed to find pattern and delete line before and after

I have the following file: line1 line2 MATCH line3 line4 I need to find the pattern, "MATCH" and delete the line before and after MATCH. So the result should be line1 MATCH lline4 I have to use sed because it is the only utility that is common across my environments. I... (1 Reply)
Discussion started by: craftereric
1 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

9. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

10. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question