Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sed or awk solution to merge files

Posted on 2007-08-01
12
Medium Priority
?
2,459 Views
Last Modified: 2013-12-26
i have a fortran header file:

junk.h

I want to insert this file into another file:

bigfile.f

whenever i see the line

#     include "junk.h"

Is there a way to do this using sed or awk?
0
Comment
Question by:rabroersma
  • 6
  • 6
12 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 19614125
perl -i -pe 's/#\s*include\s+"(.+)"$/`cat $1`/e' bigfile.f

0
 
LVL 1

Author Comment

by:rabroersma
ID: 19614150
Where is the expression for junk.h?
0
 
LVL 85

Expert Comment

by:ozo
ID: 19614154
awk '/# *include *"junk.h"/{system("cat junk.h");next}{print}' < bigfile.f
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rabroersma
ID: 19614161
okay, this works in one place only.  but the line
#    include "junk.h"
 is in multiple subroutines.

is there a global command for this?

0
 
LVL 1

Author Comment

by:rabroersma
ID: 19614167
the perl command worked, the awk command didn't, but the perl command worked in one place only... se above comment.
0
 
LVL 85

Expert Comment

by:ozo
ID: 19614186
What do you mean "in one place only"?
0
 
LVL 1

Author Comment

by:rabroersma
ID: 19614195
i have a file with multiple subroutines.  many of these subroutines include the header file. not just one.  
when i run the perl command, it replaced the include comment with the junk.h file in only one of the subroutines.  but it did not replace all of the include statements.  however, it did replace include statements that were not supposed to be replaces.

for example


subroutine sub1

#    include "junk.h"

#    include "morejunk.h"

-----

end subroutine sub1

subroutine sub2

#     include "junk.h"

-----

end subroutine sub2
 

in the subroutine: sub1, the perl command replaced the line
#   include "junk.h"

with the contents of the file junk.h.

however, it also  deletes the line
#   include "morejunk.h"

which is not good.

also, it does not insert the contents of junk.h into
sub2
even though it has the include statement in it.
0
 
LVL 85

Expert Comment

by:ozo
ID: 19614199
Is there anything after the end of "junk.h" in sub2?
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 19614205
perl -i -pe 's/\s*#\s*include\s+"junk\.h"\s*$/`cat junk.h`/e' bigfile.f
0
 
LVL 1

Author Comment

by:rabroersma
ID: 19614224
it missed 9 times out of 100,  but that is because some of the include statments are like

#     include "junk.f"  !  a bunch of useless comments


can you get that one.
0
 
LVL 1

Author Comment

by:rabroersma
ID: 19614277
thanks 91 out of 100 is great.
0
 
LVL 85

Expert Comment

by:ozo
ID: 19614280
perl -i -pe 's/\s*#\s*include\s+"junk\.h".*/`cat junk.h`/e' bigfile.f
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question