replace lines in a file by corresponding lines from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace lines in a file by corresponding lines from another file
# 1  
Old 06-14-2011
replace lines in a file by corresponding lines from another file

Hi all,

I have an EPS file header, with the first lines pasted below. I would like to replace lines, such as the "BoundingBox" field line 9, by the corresponding line from a similar EPS figure (so that the bounding boxes and other parameters are the same and my figures look nice!).

I have seen how to replace a specific line with sed, such as:
Code:
sed '9s/.*/new contents/' < dummy.txt

but I would like to be able to grab the "BoundingBox" line from another file, and paste its content in the current file.

It seems like the line where this field is stays the same, but optimally I would be able to do it by looking for a field and not a line. I would like to do this with an incantation of awk/sed but I'm not very familiar with it.
Hopefully if I understand how to do it for this line I could apply it for other fields.

Thanks for the help.

Code:
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: MATLAB, The Mathworks, Inc. Version 7.10.0.499 (R2010a). Operating System: Linux 2.6.32-32-generic #62-Ubuntu SMP Wed Apr 20 21:52:38 UTC 2011 x86_64.
%%Title: /home/tblum/Documents/papers/source_fracture/figs/field-all_b.eps
%%CreationDate: 06/14/2011  11:04:07
%%DocumentNeededFonts: Helvetica-Bold
%%DocumentProcessColors: Cyan Magenta Yellow Black
%%LanguageLevel: 2
%%Pages: 1
%%BoundingBox:   89   148   502   631
%%EndComments


Last edited by pludi; 06-14-2011 at 05:34 PM..
# 2  
Old 06-14-2011
Code:
awk 'NR==FNR{if (/%%BoundingBox/)x=$0;next}/%%BoundingBox/{$0=x}1' another_file current_file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-14-2011
Awesome, thanks a lot bartus11.

Now I just need to start learning awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string with lines stored in a file

OS version: RHEL 6.7 Shell : Bash I have a file like below $ cat pattern.txt 'T_PKT_HEADER' 'T_ORD_ITM_LOYX' 'T_ORDERITM_TRMS' 'T_ORDER_ITEM' 'T_ORDER_ITM_PRI' 'T_ORDER_ITEM_OM' 'T_ORDER_ITEM_XA' 'T_ORDER_ATT' 'T_ORDER_ACTNSET' 'T_ORDER_XM' 'T_ORDER_X' 'T_ORDER_TNTX'... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

3. Shell Programming and Scripting

Replace first 3 characters in a unix file in all lines

Replace first 3 characters in a unix file (say replace "A&B" with "C&D") in all lines of the file. Need a sed or awk script to do this. Kindly help! -Kumar (4 Replies)
Discussion started by: vasan2815
4 Replies

4. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

5. Shell Programming and Scripting

Replace lines in a file

Hi everybody, I am a newbie in shell scripting and I'm trying to write a script which reads lines from a file, searching some of this lines to change a specified number. I want to replace the line for another in the file. I have to replace multiples lines, so I have a for. Now I am trying with... (1 Reply)
Discussion started by: alex82
1 Replies

6. Shell Programming and Scripting

Replace lines from one file to another

Hello, I have 8 lines containing these unique words in both files 645147468537 673962863160 673962864957 691717701950 707917019907 790085591726 792975507744 852174812753 file.dat.orig (has 1000 lines) and file.dat(has only 8 lines) I want to replace those lines in file.dat.orig by... (1 Reply)
Discussion started by: jakSun8
1 Replies

7. Shell Programming and Scripting

Replace whole lines in the file from different files

Hi all, I have two parameter blocks in a configuration file, file1.conf. First parameter block starts with PWAT-PARM1 and ends with END PWAT-PARM1. Second parameter block starts with PWAT-PARM2 and ends with END PWAT-PARM2 (please see below in file1.conf). file1.conf is fixed width... (8 Replies)
Discussion started by: Jae
8 Replies

8. Shell Programming and Scripting

replace lines in a file

I've got a file full of numbers, example: cat test.file 60835287 0 51758036 40242437 0 32737144 0 24179513 0 4131489957 I want to replace those numbers (4 Replies)
Discussion started by: TehOne
4 Replies

9. Shell Programming and Scripting

replace multiple lines in file

Hello I am a beginner in shell script. I was trying to find a way to replace multiple lines of a file with different set of multiple line. sed -n '/begin/,/end/p' < sample1.txt >test.txt UNEDITED=`cat test.txt` vi test.txt EDITED=`cat test.txt`... (2 Replies)
Discussion started by: nox
2 Replies

10. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies
Login or Register to Ask a Question