How to replace rows from...to in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace rows from...to in a file?
# 1  
Old 09-15-2009
How to replace rows from...to in a file?

Here is a description what i need:

Document1:

start...
aaa
bbb
ccc
...end

=======================
Document2:

start...
<paste the copied lines here>
...end


All rows of document1 between "start...end" should be copied into the empty section "start...end" of document2.
The strings "start...end" are in all documents.

Smilie Sorry for my english...Thx.

Last edited by smitty11; 09-15-2009 at 10:00 AM..
# 2  
Old 09-15-2009
Hi,

Please find the command

awk '/^start/,/^end/' a1(Filenmae1) > a2(filename2)

example:

$more a1
start
one two
three four
end
five sixe
severn eight
start
chennai
salem
trichy
end
$awk '/^start/,/^end/' a1 > a2
$more a2
start
one two
three four
end
start
chennai
salem
trichy
end
# 3  
Old 09-15-2009
Extract from f1 and insert in f2 and f3:

Code:
$
$ cat f1
start...
aaa
bbb
ccc
...end
$
$ cat f2
THIS IS FILE F2
start...

...end
END OF FILE F2
$
$ cat f3
THIS IS FILE F3
start...
...end
END OF FILE F3
$
$ ##
$ perl -ne 'BEGIN{undef $/; open(F1,"f1"); $str = <F1>;
>           $str =~ s/start...|\n...end//g; close(F1)}
>           s/(start...).*?(...end)/$1$str$2/msg; print' f2
THIS IS FILE F2
start...
aaa
bbb
ccc
...end
END OF FILE F2
$
$
$ perl -ne 'BEGIN{undef $/; open(F1,"f1"); $str = <F1>;
          $str =~ s/start...|\n...end//g; close(F1)}
          s/(start...).*?(...end)/$1$str$2/msg; print' f3
THIS IS FILE F3
start...
aaa
bbb
ccc
...end
END OF FILE F3
$
$

tyler_durden
# 4  
Old 09-15-2009
Thx for reply.
I have written the code in my script file:

#!C:/perl/bin/perl -w

BEGIN {
undef $/;
open(F1,"f1.txt");
$str = <F1>;
$str =~ s/start...|\n...end//g;
close(F1)
}
s/(start...).*?(...end)/$1$str$2/msg;
print "f2.txt";


I always get the Message:
"Use of uninitialized value in substitution (s///) at C:\Test\doit.pl line 12."

Please help!

Last edited by smitty11; 09-16-2009 at 03:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace in different rows

Hello everyone, I'm a beginner in shell scripting and try to solve my issues myself, but now I am at a point where I need your help. Below is an excerpt from an xml file. <Position> <SKU>A/370269/10432/32D</SKU> <Batch>00320160501</Batch> ... (1 Reply)
Discussion started by: Jacko
1 Replies

2. UNIX for Dummies Questions & Answers

Replace space in column with letter for several rows

I have a pbd file, which has the following format: TITLE Protein X MODEL 1 ATOM 1 N PRO 24 45.220 71.410 43.810 1.00 0.00 ATOM 2 H1 PRO 24 45.800 71.310 42.000 1.00 0.00 TER ENDMDL Column 22 is the chain... (5 Replies)
Discussion started by: Egy
5 Replies

3. Shell Programming and Scripting

Replace rows to column

I have a csv file and i want to convert its rows into columns sample file like this Row1,1,2,3,......,n row2,4,5,6,.......,n . . . . rown,7,8,9,........,n i want it like this row1,row2,....,rown 1,4,.............,7 (4 Replies)
Discussion started by: sagar_1986
4 Replies

4. Shell Programming and Scripting

Replace a character of specified column(s) of all rows in a file

Hi - I have a file "file1" of below format. Its a comma seperated file. Note that each string is enclosed in double quotes. "abc","-0.15","10,000.00","IJK" "xyz","1,000.01","1,000,000.50","OPR" I want the result as: "abc","-0.15","10000.00","IJK" "xyz","1,000.01","1000000.50","OPR" I... (8 Replies)
Discussion started by: njny
8 Replies

5. Shell Programming and Scripting

Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file. Any help would be great Thanks (6 Replies)
Discussion started by: kylle345
6 Replies

6. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

7. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

9. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

10. Shell Programming and Scripting

count numbers of matching rows and replace its value in another file

Hello all, can you help me in this problem, assume We have two txt file (file_1 and file_3) one is file_1 contains the data: a 0 b 1 c 3 a 7 b 4 c 5 b 8 d 6 . . . . and I need to count the lines with the matching data (a,b,..) and print in new file called file_2 such as the... (4 Replies)
Discussion started by: GoldenFalcon10
4 Replies
Login or Register to Ask a Question