pattern matching in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users pattern matching in unix
# 1  
Old 09-24-2008
pattern matching in unix

Task is to identify files like code.1 , code.23 and so on ... (the files which are ending with a number) but it should not match files like code.123abc. So the search will normally search for files with "code." and at the end we should extract for the correct match. Now I have to remove these files interactively i.e 'rm -i '. Now please let me know any oneliner in unix or perl to perform this task.
I tried : find . -name "code.[0-9]* -exec rm -i {} \; (but this will not work ..??)
# 2  
Old 09-24-2008
As find is not using Regular Expressions, you might want to use something like:

Code:
find . -print| grep code.[0-9]*$| xargs -p rm -i

# 3  
Old 09-24-2008
I see that grep is not a correct match .. even if that grep works ... I am unable to parse files and remove them interactively.
# 4  
Old 09-24-2008
Show your error message please. Also I don't understand what you mean by "grep is not a correct match". xargs -p and rm -i should ensure it will be interactively:

Code:
root@isau02:/data/tmp/testfeld> ls -la
insgesamt 8
drwxr-xr-x 2 isau users 4096 2008-09-24 11:11 .
drwxr-xr-x 7 isau users 4096 2008-07-09 14:36 ..
-rw-r--r-- 1 root root     0 2008-09-24 11:11 code.123
-rw-r--r-- 1 root root     0 2008-09-24 11:11 code.123abc
-rw-r--r-- 1 root root     0 2008-09-24 11:11 code.123.abc
root@isau02:/data/tmp/testfeld> find . -type f -print| grep code.[0-9]*$| xargs -p rm -i
rm -i ./code.123 ?...n
root@isau02:/data/tmp/testfeld>

# 5  
Old 09-24-2008
You could use several "-name" clauses for find to exclude the files you do not want:

Code:
find /some/dir -name "code.[0-9]*" ! -name "*[a-z]" -exec ....

The exclamation mark is a logical NOT. "! -name x" find everything except a file named "x".

I hope this helps.

bakunin
# 6  
Old 09-24-2008
Code:
ls -1 code.[0-9]* | egrep -E ".[0-9]+$" | xargs  rm

# 7  
Old 09-24-2008
Thanks bakunin this is working ... !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

2. UNIX for Beginners Questions & Answers

Binary pattern matching in UNIX

I think what I'm trying to do is pretty straightforward but I just can't find a way to do it. I'm trying to run a double pattern match in a three column file. If the first two columns match, I need to output the third. So in the file AAA BBB 1 BBC CCC 5 CCC DDD 7 DDD EEE 12 If the... (4 Replies)
Discussion started by: matthewndavies1
4 Replies

3. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

Issue with Pattern Matching in Unix

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK,CARE_OF 1,Robert,74538,12,RICHARD JOHNSON, P.C 2,Sam,07564,13,% R.S MIKE, V.K.S 3,Kim, Ed,12345,14,@90 KMS, %TK Desired Output: SNO,NAME,ZIPCODE,RANK,CARE_OF... (1 Reply)
Discussion started by: techmoris
1 Replies

7. Shell Programming and Scripting

Perl Pattern Matching:Unix Vs. Window (Is it OS dependent??)

Hi All, Below I am discussing the problem I am facing while using pattern matching in Unix & Window. Plz hv a look into it. ================ my $s="UPDATE A SET s="klkkk' ;" ; if ( $s =~ m/^*UPDATE+/i ) { print $s; } else { print "no match";} =================== Both should... (6 Replies)
Discussion started by: Niroj
6 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies

10. Shell Programming and Scripting

Pattern Matching in UNIX

Hello, I have a pattern like "XXXXXX XXXXXX" which i need to make search in a input file and Replace the matched pattern to a another pattern. This is the code i tried .. #!/usr/bin/perl print "Enter a File name :"; chomp ($file = <STDIN>); print "\n Searching file :"; if (system ("ls... (2 Replies)
Discussion started by: maxmave
2 Replies
Login or Register to Ask a Question