pattern searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pattern searching
# 8  
Old 09-19-2012
nopes. that doesnt give me the output..basically what I am trying is using the first command I will check the existence of the directory by the outputstring1 and if present I will create a directory with the value of outputstring2
# 9  
Old 09-19-2012
Quote:
Originally Posted by danish0909
nopes. that doesnt give me the output..basically what I am trying is using the first command I will check the existence of the directory by the outputstring1 and if present I will create a directory with the value of outputstring2
I guess what you have asked and what you want to achieve are two diff things Smilie command which i gave will get u the file names the way you wanted but how u wanna use that is completely at your stake..
# 10  
Old 09-19-2012
Quote:
Originally Posted by danish0909
nopes. that doesnt give me the output..basically what I am trying is using the first command I will check the existence of the directory by the outputstring1 and if present I will create a directory with the value of outputstring2
If I get it right, you could do something like this

Referencing solution from #6,

Code:
for file in *.jar
do
 outstring1=$(echo "$file" | awk -F"[_.]" '{print $1,$2,$3}' OFS=_)
 outstring2=$(echo "$file" | awk -F"[_.]" '{print $(NF-1)}') 
done

Efficiency is highly depends on what you actually trying to achieve. It's possible that everything could be done in single commands without so-many external calls/pipes.
Please post your requirements in details with expected input and output sample.
# 11  
Old 09-19-2012
basically, I have tens of files like the below:
$ls -1 *.jar
antispam_live_dec_20120727.jar
credit_transfer_live_bln_20120711.jar
credit_transfer_live_bln_20120711_1.jar
credit_transfer_live_bln_20120712.jar
credit_transfer_live_bln_20120712_1.jar
credit_transfer_live_bln_20120713.jar
credit_transfer_live_bln_20120713_1.jar
credit_transfer_live_bln_20120714.jar
credit_transfer_live_bln_20120714_1.jar
credit_transfer_live_bln_20120715.jar
credit_transfer_live_bln_20120715_1.jar
credit_transfer_live_bln_20120716.jar
credit_transfer_live_bln_20120716_1.jar
credit_transfer_live_bln_20120717.jar
credit_transfer_live_bln_20120717_1.jar
credit_transfer_live_bln_20120718.jar
credit_transfer_live_bln_20120718_1.jar
credit_transfer_live_bln_20120719.jar
credit_transfer_live_bln_20120719_1.jar
credit_transfer_live_bln_20120720.jar
credit_transfer_live_bln_20120720_1.jar
credit_transfer_live_bln_20120721.jar
credit_transfer_live_bln_20120721_1.jar
credit_transfer_live_bln_20120722.jar
credit_transfer_live_bln_20120722_1.jar
credit_transfer_live_bln_20120723.jar
credit_transfer_live_bln_20120723_1.jar
credit_transfer_live_bln_20120724.jar
credit_transfer_live_bln_20120724_1.jar
credit_transfer_live_bln_20120725.jar
credit_transfer_live_bln_20120725_1.jar
credit_transfer_live_bln_20120726.jar
ericsson_msc_live_bln_central1_20120725_1.jar
ericsson_msc_live_bln_central1_20120726.jar
ericsson_msc_live_bln_central1_20120726_1.jar
ericsson_msc_live_bln_central1_20120727.jar
ericsson_msc_live_bln_central1_20120727_1.jar
ericsson_msc_live_bln_central1_20120728.jar

......

what i want is:

1 to extract xx_xx_live from the string

2 store it in a variable A

3 check whether a directory of the same variable is created or not, if not the create it, and then go inside

4 extract the date part from the original string, that would be the following part: (credit_transfer_live_bln_20120726.jar) and store it in a variable B

5 check if a directory of same date as B is present under A or not, if not, then create it and move the original string file under that date.


If it helps, I can provide the whole list of file names

Thanks a lot everyone
# 12  
Old 09-19-2012
Code:
#!/bin/ksh

## Operate on .jar files in the current directory.
for filename in *.jar
do
  ## Extract the directory name and date using regular expressions.
  dirname=$( print "$filename" | sed -n "s/^\(.*_live\).*/\1/p" )
  dirdate=$( print "$filename" | sed -n "s/.*_\([0-9]\{8\}.*\).jar$/\1/p" )

  ## If directory does not exist, create it.
  if [[ ! -d "$dirname" ]]; then
    mkdir $dirname
  fi

  ## Directory exists so test for date directory.
  if [[ ! -d "$dirname/$dirdate" ]]; then
    mkdir "$dirname/$dirdate"
  fi

  ## date dir exists so move file.
  mv "$filename" "$dirname/$dirdate"

done

exit 0


Last edited by gary_w; 09-19-2012 at 01:22 PM.. Reason: Updated based on above post
# 13  
Old 09-19-2012
@gary_w

i tried out the individual sed expressions on the command line just to see the output and here is what I am getting the for the date part:

$for i in `ls -1 *.jar`;do echo $i|sed -n "s/.*_\([0-9]*\).jar$/\1/p" ;done
20120727
20120711
1
20120712
1
20120713
1
20120714
1
20120715
1
20120716
1
20120717
1
20120718
1
20120719
1
20120720
1
20120721
1
20120722
1
20120723
1
20120724
1
20120725
1
20120726
1
20120727
1
20120728
1
20120729
1
20120730
1
20120731
1
20120801
1
20120802
1
20120803
20120804
1
20120805
1
20120806
20120807
1
20120808
1
20120809
20120721
20120729
20120801
20120804
20120711
1
20120712
1
20120713

Smilie
# 14  
Old 09-19-2012
Check out the amended version that allows for the different naming convention.

Code:
sed -n "s/.*_\([0-9]\{8\}.*\).jar$/\1/p"

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