Scanning Line by Line and Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scanning Line by Line and Replace
# 1  
Old 09-25-2011
Scanning Line by Line and Replace

Hi Everyone,

I have a file which contains the autosys (job scheduler) JIL (Job Information Language) Information.
For Example:
Code:
/* ----------------- TS_QDB_DATA_MASK_RUN ----------------- */

insert_job: TS_QDB_DATA_MASK_RUN   job_type: c
command: ${DATA_BIN}/hrt_data_mask.sh
machine: TS_QDB_MACHINE
#owner: qc_dev
permission: gx,mx
date_conditions: 1
days_of_week: all
start_times: "4:07,5:07,6:07,7:07,8:07,9:07,10:07,11:07,12:07,13:07,14:07,15:07,16:07,17:07,18:07,19:07,20:07,21:07"
description: "TS_QDB_DATA_MASK_RUN for masking data in LFSS2"
term_run_time: 60
std_out_file: ${DATA_ROOT}/logs/TS_QDB_DATA_MASK_RUN.`date +%Y%m%d`.log
std_err_file: ${DATA_ROOT}/logs/TS_QDB_DATA_MASK_RUN_err.`date +%Y%m%d`.log
max_run_alarm: 60
alarm_if_fail: 1
profile: /home/.autosys_profile
timezone: US/Eastern


.....So on

In the above file, I need to replace the word "TS_QDB_" with "KB_OM_1876_" i.e. in the fields "Insert_job:", "std_out_file:, "std_err_file:".

So Basically I need to scan the file line by line and need to replace the word appropriately.

I have started writing a perl scrip for the same but facing the issue while replacing the word.

Code:
#!/usr/bin/perl

$file=$ARGV[0] ;
$old_name=$ARGV[1] ;
$new_name=$ARGV[2] ;

open(FILE,$file)|| die ("could not open file $file: $!");
open(OUT1,'>','goodfile');

my $line;
$line = $_;

while(<FILE>)
{
if ($newline =~ /insert_job:/) {
        $newline =~ s/$old_name/$new_name/g;
        print OF $newline ;
}

elsif < Checking for the other two conditions i.e. std_out , std_err >

else
  {
    print OF $newline ;
  }
}
close FILE;
close OUT1;

where the Arg[0] is the <Input file> , Arg[1] = TS_QDB_" & Arg[2] = "KB_OM_1876_"

Could someone please help me out with some thoughts.

I would really appreciate your thoughts.
# 2  
Old 09-26-2011
Code:
sed '/^insert_job/ s/TS_QDB_/KB_OM_1876_/g' yourfile

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 09-26-2011
Hi rdcwayx,

I would really appreciate your time.

The trick is very good and I have written a shell script with all the sed commands at last that would create the final output file.
But the only question here is, how can I replace the following :
Code:
/* ----------------- TS_QDB_DATA_MASK_RUN ----------------- */

I have two options over here:
1) I need to replace the word "TS_QDB_" with "KB_OM_1876_".
2) I need to remove the entire line which would work for me. i.e.
I need to remove the entire line in the inputfile that would start with
/**** <job name > ****/

Could you please help me how can I figure this out.

Thank you very much.

# 4  
Old 09-26-2011
May be replace command can help you

replace "TS_QDB_" "KB_OM_1876_" -- outputfile

Give a try
# 5  
Old 09-26-2011
Hi
I do find and replace all the time.
In vi do this
:%s/TS_QDB_/KB_OM_1876_
I hope this helps.
# 6  
Old 09-26-2011
may be like this:
Code:
sed -e '/^\/\*/ s!\(.*\)TS_QDB\(.*\)!\1KB_OM_1876\2!' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

Scanning columns for duplicates and printing in one line

Description of data: NC_002737.1 4 F1VI4M001A3IAU F1VI4M001A3IAU F1VI4M001A3IAU F1VI4M001A3IAU NC_006372.1 5 F1VI4M001BH0HY FF1VI4M001BH0HY F1VI4M001C0ZC5 F1VI4M001DOF2X F1VI4M001AYNTS Every field in every record is tab separated There can be "n" columns. Problem: What I want to... (4 Replies)
Discussion started by: Deep9000
4 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question