Script to replace lines in ksh Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace lines in ksh Script
# 1  
Old 03-30-2015
Script to replace lines in ksh Script

Hi All,

I am novice to Unix and I need your expert advice for the below task.

There is a KSH script file in which I need to replace few line as per the below expectations. So my file look like as

Code:
# Host Setup Command:
Line 1
Line 2
Line 3
Line 4
Line Any
_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$

So in between of # Host Setup Command: and _AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$ there can be N number of lines and I need to replace those lines as per below format

Code:
# Host Setup Command:
Syntax 1
Syntax 2
Syntax 3
_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$

Please let me know if you experts can help me here.

Thanks in Advance.

Rupali

Last edited by Don Cragun; 03-30-2015 at 02:52 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-30-2015
Is this homework/classwork?
# 3  
Old 03-30-2015
This is the solution I need to implement in Production at one shot.

not sure about the class/home work.
# 4  
Old 03-30-2015
How is Syntax 1 etc. supplied? In a file? Calculated? A constant pattern?
# 5  
Old 03-31-2015
Syntax 1 is a simple text I need to replace with the particular line.
# 6  
Old 03-31-2015
Try:
Code:
sed s,"Syntax 1","Particular Line",g "$WORKFILE"

When it looks as you want it to, add -i before the workfile.

hth
# 7  
Old 03-31-2015
The following works with without external programs like sed.
It deletes the block between start marker and end marker, and where it's at the end marker puts a replacement text.
Each iteration of the while loop reads a line from "./file" into the variable $line.
The variable $block is non-null if inside the block.
The output in the while loop is redirected to a new file, that is finally copied back to "./file".
Code:
block=""
f="./file"
while IFS= read -r line
do
  if [ -n "$block" ] && [ "$line" = '_AB_Proxy_DIR=Out_ABC_For_Bussiness.xml-$$' ]
  then
    block=""
    printf "%s" "\
Syntax 1
Syntax 2
Syntax 3
"
  fi
  if [ -z "$block" ]
  then
    printf "%s\n" "$line"
    [ "$line" = '# Host Setup Command:' ] && block=1
  fi
done < "$f" > "$f.new"
[ -s "$f.new" ] &&
cp "$f.new" "$f" &&
rm "$f.new"


Last edited by MadeInGermany; 03-31-2015 at 05:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line. File 1: hgacdavd sndm,ACNMSDC msgid "Rome" msgstr "" kgcksdcgfkdsb... (4 Replies)
Discussion started by: madira
4 Replies

3. Shell Programming and Scripting

Script to replace/delete lines in C program

I am under the gun on a project and am not very good at scripting. I have to make a modification to thousands of C programs to basically replace the #pragma statement. I don't want to have to do it manually. Here is an example of what I need done. Any help would be greatly appreciated. I... (5 Replies)
Discussion started by: jclanc8
5 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

6. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

9. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

10. Shell Programming and Scripting

how to convert unix .ksh script to windows .batch script

I am using awk in my .ksh script but when I am trying to run in windows its not recognising awk part of the ksh script , even when I changed it to gawk it does not work, this is how my .ksh and .bat files look like. thanx. #!/bin/ksh egrep -v "Rpt 038|PM$|Parameters:|Begin |Date: |End... (1 Reply)
Discussion started by: 2.5lt V8
1 Replies
Login or Register to Ask a Question