Find & replace --> create a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find & replace --> create a new file
# 1  
Old 06-22-2012
Find & replace --> create a new file

Hi All,

I have a unix shell script file as below.
Quote:


. get_dates.sh
DD=$(printf $LASTREPDATE | cut -c1-2)
MM=$(printf $LASTREPDATE | cut -c3-4)
YY=$(printf $LASTREPDATE | cut -c7-8)
YYYY=$(printf $LASTREPDATE | cut -c5-8)
LASTREPDATE="$DD$MM$YYYY"
LASTREPDATE_I="$DD$MM$YYYY"

DD=$(printf $CURRREPDATE | cut -c1-2)
MM=$(printf $CURRREPDATE | cut -c3-4)
YY=$(printf $CURRREPDATE | cut -c7-8)
CURRREPDATE_I="$DD$MM$YYYY"

FILENAME248="BW3_*248S"


. $APPAREA/check_file.sh "$FILENAME248*_$CURRREPDATE_I" 1>>$JOBOUTFILE 2>>$JOBOUTFILE
RETCODE=$?
if [ $RETCODE -ne 0 ]
then
FILENAME="$FILENAME248*_$LASTREPDATE_I*.CSV*"
cp $BACKUPAREA/$FILENAME $INBOUNDAREA 1>>$JOBOUTFILE 2>>$JOBOUTFILE
if [ $? -ne 0 ]
then
FILENAME=$FILENAME248"_RD"$LASTREPDATE.TXT.gz
FILENAME1=$FILENAME248"_RD"$LASTREPDATE.TXT
cp $BACKUPAREA/$FILENAME $INBOUNDAREA/ 1>>$JOBOUTFILE 2>>$JOBOUTFILE
if [ $? -ne 0 ]
then
write_log.sh "BW3_248-RESTORE" "FAILED 1"
exit 0
fi
gunzip $INBOUNDAREA/$FILENAME 1>>$JOBOUTFILE 2>>$JOBOUTFILE
My task is

a)to replace 248 to 350 and need to create a new file as BW3_350.sh
b)to replace 248 to 380 and need to create a new file as BW3_380.sh
c)to replace 248 to 320 and need to create a new file as BW3_320.sh

there is no change in the file apart from the above mentioned one. I have to create around 250 files. So i need a to script to do this Smilie
# 2  
Old 06-22-2012
You can create a file with the parameters. This will have all the values that need to be used in place of 248. Read this file in another script using while loop and use sed to replace and create corresponding files.

Parameter File
Code:
350
380
320

Script To Replace And Create New Files
Code:
while read line
do
        echo "Replacing 280 with $line"
        sed "s/248/${line}/g" file1 > file_${line}.sh
done < parafile

# 3  
Old 06-22-2012
Quote:
Originally Posted by karthi_mrkg
Hi All,

I have a unix shell script file as below.


My task is

a)to replace 248 to 350 and need to create a new file as BW3_350.sh
b)to replace 248 to 380 and need to create a new file as BW3_380.sh
c)to replace 248 to 320 and need to create a new file as BW3_320.sh

there is no change in the file apart from the above mentioned one. I have to create around 250 files. So i need a to script to do this Smilie


Where the variable $JOBOUTFILE comes from?
# 4  
Old 06-24-2012
Quote:
Originally Posted by Sheel
You can create a file with the parameters. This will have all the values that need to be used in place of 248. Read this file in another script using while loop and use sed to replace and create corresponding files.

Parameter File
Code:
350
380
320

Script To Replace And Create New Files
Code:
while read line
do
        echo "Replacing 280 with $line"
        sed "s/248/${line}/g" file1 > file_${line}.sh
done < parafile


WHILE READ LINE --> CAN YOU TELL ME HOW SHOULD I OPEN THE FILE AND INCLUDE IN THE WHILE LOOPE ? CAN YOU GIVE ME THE EXACT CODE PLS?
# 5  
Old 06-24-2012
Having 250 different versions of this script is quite bad form. It will be a maintenance nightmare.

Why not pass the file number as a parameter to the script this example takes suffix (default 250) and prefix (default BW3_) parameters.

It would be called like this:
Code:
$ proc_file.sh 250 BW3_
$ proc_file.sh 380 BW3_

proc_file.sh
Code:
PREFIX=${2:-BW3_}
SUFFIX=${1:-250}
...
FILENAME248="${PREFIX}*${SUFFIX}S"
...
write_log.sh "${PREFIX}${SUFFIX}-RESTORE" "FAILED 1"
...

# 6  
Old 06-25-2012
I have to do some more thing along with this...yes..i have to get the file permission too in a variable...

say for example...

i have a file with 755 permission...i would like to store this permission in a local variable to use it further for other files.
# 7  
Old 06-25-2012
Quote:
Originally Posted by karthi_mrkg
WHILE READ LINE --> CAN YOU TELL ME HOW SHOULD I OPEN THE FILE AND INCLUDE IN THE WHILE LOOPE ? CAN YOU GIVE ME THE EXACT CODE PLS?

That is the actual code. Did you try it ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find & Replace

Hi I am looking to rename the contents of this dir, each one with a new timestamp, interval of a second for each so it the existing format is on lhs and what I want is to rename each of these to what is on rhs..hopefully it nake sense CDR.20060505.150006.gb CDR.20121211.191500.gb... (3 Replies)
Discussion started by: rob171171
3 Replies

2. UNIX for Dummies Questions & Answers

Find & Replace command - Fasta file

Hi all ! I have a fasta file that looks like that: >Sequence1 RTYIPLCASQHKLCPITFLAVK (it's just an example, obviously in reality I have several pairs of lines like that) Using UNIX command(s), would it be possible to replace all the characters except the "C" of the second line only by... (7 Replies)
Discussion started by: Cevin21
7 Replies

3. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

4. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

5. Shell Programming and Scripting

find & replace comma in a .csv file.

HI, Please find the text below. I receive a .csv file on server. I need the comma(,) in the second column to be replaced by a semi-colon( ; ). How to do it. Please help. Sample text: "1","lastname1,firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"... (2 Replies)
Discussion started by: libin4u2000
2 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. Shell Programming and Scripting

find & replace with user input

Trying to create a script/executable to replace "abc" text string in "myfile.htm" with input from a pop-up field. For example, launch this thing and a prompt is popped up asking the user to input what "abc" should be replaced with, then it inserts what the user inputs in place of abc in the... (3 Replies)
Discussion started by: mike909
3 Replies

8. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

9. UNIX for Dummies Questions & Answers

find, mv and create unknown parent & subfolders

I searched the forum rather thoroughly but still could not find the answer. Hopefully the solution is right under my nose. Here what I need to do, move older data to a Archive folder that is 18 months old and older. I would like to use the following command, find departmentx/* -mtime 530... (5 Replies)
Discussion started by: cheeba
5 Replies

10. Shell Programming and Scripting

Find & Replace

I get a text file with 70+ columns (seperated by Tab) and about 10000 rows. The 58th Column is all numbers. But sometimes 58th columns has "/xxx=##" after the numeric data. I want to truncate this string using the script. Any Ideas...:confused: (3 Replies)
Discussion started by: gagansharma
3 Replies
Login or Register to Ask a Question