pattern searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pattern searching
# 15  
Old 09-19-2012
now it print out extra characters after the date

Code:
$ for i in `ls -1 *.jar`;do echo $i|sed -n "s/.*_\([0-9]\{8\}.*\).jar$/\1/p" ;done|more
20120727
20120711
20120711_1
20120712
20120712_1
20120713
20120713_1
20120714
20120714_1
20120715
20120715_1
20120716
20120716_1
20120717
20120717_1
20120718
20120718_1
20120719
20120719_1
20120720
20120720_1
20120721
20120721_1
20120722
20120722_1
20120723
20120723_1
20120724
20120724_1
20120725
20120725_1
20120726

sorry for troubling you Smilie

---------- Post updated at 10:17 PM ---------- Previous update was at 10:12 PM ----------

cant I say extract the date part from the last characters of the string:


awk lets me substr characters from the start of the string..how about substr characters from the end of the string, with the last character as the first character

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 09-19-2012 at 01:57 PM..
# 16  
Old 09-19-2012
I thought you wanted that extra character. Well all you have to do is tweak the regular expression:
Code:
sed -n "s/.*_\([0-9]\{8\}\).*\.jar$/\1/p"

# 17  
Old 09-19-2012
Quote:
Originally Posted by gary_w
I thought you wanted that extra character. Well all you have to do is tweak the regular expression:
Code:
sed -n "s/.*_\([0-9]\{8\}\).*\.jar$/\1/p"

thank you thank you thank you..please explain to me the regular expression..i have been trying to get hold of this since long..but have only managed to bang my head
# 18  
Old 09-19-2012
From left to right:
Code:
sed -n "s/.*_\([0-9]\{8\}\).*\.jar$/\1/p"

Code:
sed   - Stream editor
-n    - Suppress normal action which is to print all lines
"s/   - Start search/replace pattern
.     - Match any character
*     - and any number of any character
_     - followed by an underscore
\(    - Start first group (this portion of the search pattern, if found, can be referred to later)
[0-9] - followed by a single number in the range of 0-9
\{8\} - followed by 8 instances of the previous pattern (a number)
\)    - End the first group
.*    - followed by any character and any number of any character
\.    - followed by a period (escaped with a backslash since the period
        has special meaning in a regex)
jar   - followed by the string "jar"
$     - followed by the end of the line
/     - Start replace pattern
\1    - Refers to the first group as defined inside the \(  \)
/     - End replace string
p     - Don't replace, but print the match instead
"     - End the sed pattern

This User Gave Thanks to gary_w For This Post:
# 19  
Old 09-19-2012
ok!, so basically what comes inside the
Quote:
\( and \)
gets printed on the screen. great thanks for letting it out so cleanly. thanks a lot
# 20  
Old 09-19-2012
Technically it gets printed to standard output (STDOUT). That's how you are able to capture it in a variable.

You're welcome!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

2. UNIX for Dummies Questions & Answers

searching pattern in VI

in my file i have somthing likre kpk_12 kpk_1 kpk_1.tcl kpk_3.tcl kpk kpk kpk i want search only kpk i am using this cmd /kpk ...results it is showing all . any cmd is ther other then this to search exactword in this example kpk it shoulsnot show kpk_* etc Thanks in Advance ... (2 Replies)
Discussion started by: prakumar
2 Replies

3. UNIX for Dummies Questions & Answers

Pattern searching

Hi, I need small help from you people. In a directory there are around 150 odd files and few them contain the word "TRACK" and few are not. How can I find out the the list of those files which doesn't contain the word "TRACK"? Thanks, Siba (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

4. Shell Programming and Scripting

Searching for a pattern

How do I search for a pattern - N/A in a particular column using awk? (11 Replies)
Discussion started by: rabiu
11 Replies

5. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

6. Shell Programming and Scripting

searching for a pattern

can anybode tell me ? I want to search for a pattern present in a whole directory and subdirectories's files containg " crat" I tried grep -r "crat" */* ; is it right ? (3 Replies)
Discussion started by: pranabrana
3 Replies

7. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies

8. Shell Programming and Scripting

Regarding Searching Pattern

Hi Guys, Can you help with the shell script: I would like to search a fixed width pattern from a file say for each line from a fixed position and lenght it has to return all rows from the file. Example: To search the third column for "def" it has to return 1 and 4th rows only ... (2 Replies)
Discussion started by: sbasetty
2 Replies

9. Filesystems, Disks and Memory

Searching for a pattern in a Directory

How to search a given pattern in the files which are present in my current working directory and its subdirectories recursively (1 Reply)
Discussion started by: gandhevinod
1 Replies

10. Programming

pattern searching using C

i think grep can only find char in files matching a pattern without any wildcards like ? correct? It works with * but not ?. how can i write a small c program to find words in a file, like a list, that matches a pattern like ma?y, b??con, etc if grep doesn't understand ? in a pattern search. if... (1 Reply)
Discussion started by: giannicello
1 Replies
Login or Register to Ask a Question