Replacing Block of lines in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing Block of lines in a text file
# 1  
Old 02-03-2009
Replacing Block of lines in a text file

Dear All,

Regards of the Day.

I have a text file with some functions:

Function1
{
parameter 1
parameter 2
parameter 3
}
end

Function2
{
parameter 1
parameter 2
parameter 3
}
end

Function3
{
parameter 1
parameter 2
parameter 3
}
end

Function4
{
parameter 1
parameter 2
parameter 3
}
end


My objective is to replace the parameter 1,2 and 3 withing the Function3 { } end block with parameter4,5 and 6

Please help
# 2  
Old 02-03-2009
One possible way:
Code:
awk '/^Function3$/ { x=1 } /^param/ && x == 1 {print $1,($2+3); next} /^Function4$/ { x=0 } {print}' infile
Function1
{
parameter 1
parameter 2
parameter 3
}
end

Function2
{
parameter 1
parameter 2
parameter 3
}
end

Function3
{
parameter 4
parameter 5
parameter 6
}
end

Function4
{
parameter 1
parameter 2
parameter 3
}
end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

3. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

4. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

5. Shell Programming and Scripting

sed: Replacing two lines of text

I just created a file in vi, looks like this: Hello nobody nobody What I need is to have sed change the file so it looks like this: Hello everybody In other words, the sed query needs to look for both instances of "nobody" before it puts the word "everybody" on the first line. ... (2 Replies)
Discussion started by: calrog
2 Replies

6. Shell Programming and Scripting

replacing text in a file, but...

Hi all, Very first post on this forums, hope you can help me with this scripting task. I have a big text file with over 3000 lines, some of those lines contain some text that I need to replace, lets say for simplicity the text to be replaced in those lines is "aaa" and I need it to replace it... (2 Replies)
Discussion started by: Angelseph
2 Replies

7. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies

8. Shell Programming and Scripting

Replacing lines in text files

Hi, I have 2 sets of text files. I need to take a field from a certain line in set 1 and put it in the same place in set b. The line appears once per file, in different places but is a set format and has the unique word "ANTENNA" in it and is always 81 characters long. Example from set a: ... (7 Replies)
Discussion started by: Jonny2Vests
7 Replies

9. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question