Unix file pattern check and replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix file pattern check and replacement
# 1  
Old 10-08-2012
Unix file pattern check and replacement

HI Guys ,
Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp

If the file exist in correct format come out from code .

If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or Access_file_Record_.YYYYMM)

Any different pattern file should be renamed to expected file Access_file_Record.YYYYMM
Every month YYYYMM pattern shoud be identified as 201201 for jan , 201202 for feb etc when script is ran for that month

Please can anyone help me on this.
Thanks
# 2  
Old 10-08-2012
If the script encounters for month 201202 (lets say) 2 files like :
Access_file_Record_201202.txt and Access_file_Record_.201202

What should the script do ?

According to what you say it should rename them with Access_file_Record_201202 but in this case after renaming Access_file_Record_201202.txt in Access_file_Record_201202 when the script encounters Access_file_Record_.201202 it will overwrite the file Access_file_Record_201202 (which has the content of Access_file_Record_201202.txt) with the content of Access_file_Record_.201202 .

this thing will happen in ANY OS. you need to clarify this first, what exactly you want. You cannot have more than 1 file with the same name on the same path.
# 3  
Old 10-08-2012
HI As i said , the correct file pattern should be Access_file_Record.201202 .
If pattern matched exit;

If Not,then check different possible formats Access_file_Record_201202.txt or Access_file_Record_201202 etc (Access_file_Record prefix is common) are present if any . Then rename that pattern to Access_file_Record.201202 and come out . Hope i am clear with requirement.
# 4  
Old 10-08-2012
Quote:
Originally Posted by Perlbaby
HI As i said , the correct file pattern should be Access_file_Record.201202 .
If pattern matched exit;

If Not,then check different possible formats Access_file_Record_201202.txt or Access_file_Record_201202 etc (Access_file_Record prefix is common) are present if any . Then rename that pattern to Access_file_Record.YYYYMM and come out . Hope i am clear with requirement.
My question is "What if you have more than one file with wrong pattern? "
# 5  
Old 10-08-2012
HI , There will be only one wrong file pattern avaliable . Either it may be
Access_file_Record_201202.txt or Access_file_Record_201202
# 6  
Old 10-08-2012
The content of the script :
Code:
#!/bin/bash

target_date=`date +%Y%m`
cd /tmp
the_existing_file=`ls *$target_date*`
the_desired_file="Access_file_Record.${target_date}"

if [ $the_existing_file = $the_desired_file ]; then
echo "The file was correctly named"
else
echo The file had the name "$the_existing_file" and it was renamed "$the_desired_file"
mv $the_existing_file $the_desired_file
fi

Create with vi a script and copy paste the content from above, afterwards add execution rights to the scripts. The script works for Linux environments (because of the command "date +%Y%m") which can have different outputs in different UNIXes . If you have problems with the script tell exactly what kind of Linux you use (use the uname command), usually this should be provided in the first request.
This User Gave Thanks to black_fender For This Post:
# 7  
Old 10-08-2012
Thanks dude ..I tried and it perfectly worked .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern Matching and replacement

Hello Everybody, I need a help in the below pattern matching and replacement issue I have a file : emp.txt 21356 suresh 12/12/2012 23511 ramesh 11/06/2011 31456 biswajit 09/08/2013 53134 archan 06/02/2009 first field:- employee id, 2nd field is name and third field is date of joining ... (10 Replies)
Discussion started by: shellscripting
10 Replies

2. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

3. Shell Programming and Scripting

Check the file if the search pattern is there or not.

I want to check the files if the below pattern is there or not. I have to scan thousands of files. It should start search with "Group" (Case insensitive) and followed by first semicolon ";" . In between these two there should be "partition"(case insensitive). File1.txt a) update... (1 Reply)
Discussion started by: smolakal
1 Replies

4. Shell Programming and Scripting

String replacement when particular pattern matches in a file

I have a file file123.xml which looks like this xmlEntry="username"="josh" <property="never_back_down"> phone="<178652>" apn=property:address="wonderland" xmlEntry="username"="jessica" <property="never_back_down"> phone="<178653>" apn=property:address="wonderland"... (5 Replies)
Discussion started by: poga
5 Replies

5. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

6. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies

7. UNIX for Dummies Questions & Answers

awk pattern replacement

Hi I'm a newbie in unix and I'm having trouble in creating a script. I want to search for a pattern '_good' and insert new lines that contains '_bad', '_med', '_fail' while also ensure that the line contains _good is removed here some of the data UPDATE SCHOOL SET GRADE =... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

8. Linux

matching pattern and replacement

Hi I am trying to look for a view name in create view statement and then replace this view name with VW_ in grants line in my ddl file . cat dim_provider.sql | grep -i "create view" | while read f1 f2 f3 f4 f5 f6 f7 f8 f9 do new_vw=` echo "$f3" | cut -d "." -f2... (32 Replies)
Discussion started by: capri_drm
32 Replies

9. UNIX for Dummies Questions & Answers

Pattern Replacement

There is a requirement that i need to replaced a pattern by another pattern in all the files in my entire file system. there are 1000s of file in the system. let the pattern is "calcuta". i have to replace this pattern by "kolkata" in all those files which contain "calcuta". I am only able to... (12 Replies)
Discussion started by: palash2k
12 Replies

10. Shell Programming and Scripting

how to check for pattern in other file

i hav file1 with contents: welcome this is first file file name is file 1 last line of the file now i want to check if the second line has sentence "this is first file" through "if" statement from another file ... is it possible if so how!!! (6 Replies)
Discussion started by: suri
6 Replies
Login or Register to Ask a Question