Solved

Use BASH script to parse between two points in a file and out to two files

Posted on 2008-06-18
3
570 Views
Last Modified: 2013-12-26
Boss has an odd one for me today and for some reason I just can't get my head around such a simple request.

I have a requirement for a single file which contains the contents of two different config files put together and the script part at the start.  Basically an analyst would fill out all the params and then run the file.  The result would be two config files would be output from the script.  I've attached a sample below.

So the file would basically 'parse' itself into two seperate files (called whatever) between the two pointers.

So any questions?


#!/bin/bash
# $Revision: 1.1 $
# 
 
some code etc.. blah blah
 
### BEGIN Config 1 ###
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
### END Config 1 ###
 
 
### END Config 2 ###
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
### END Config 2 ###

Open in new window

0
Comment
Question by:london_mccourt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Hanno P.S. earned 500 total points
ID: 21814652
It would be pretty easy if you could use "here code":
#!/bin/bash
# $Revision: 1.1 $
# 
 
some code etc.. blah blah
 
### BEGIN Config 1 ###
cat << 'EOF' > /path/to/config1.file
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
EOF
### END Config 1 ###
 
 
### END Config 2 ###
cat << 'EOF' > /path/to/config2.file
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
EOF
### END Config 2 ###

Open in new window

0
 
LVL 14

Expert Comment

by:agriesser
ID: 21854183
What about something like this?
#!/bin/bash
 
LINENO=$(wc -l $0 | cut -d ' ' -f 1)
 
# small function to splitup parts of this configuration file
# parameters: $part (can be 1 to extract part 1 or 2 to extract part 2)
splitup()
{
  grep -B 0 -A $LINENO "^### BEGIN Config $1 ###" $0 | grep -A 0 -B $LINENO "^### END Config $1 ###" > part$1.conf
}
 
 
 
echo Hello world
echo "I'm going to split myself up now."
echo -n Splitting up part 1...
splitup 1
echo "done"
echo
echo -n "Splitting up part 2..."
splitup 2
echo "done"
echo
echo "I create two new files, part1.conf and part2.conf, have fun."
 
exit 0
 
### BEGIN Config 1 ###
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
### END Config 1 ###
 
### BEGIN Config 2 ###
config info line 1
config info line 2
config info line 3
config info line 4
config info line 5
config info line 6
### END Config 2 ###

Open in new window

0
 
LVL 14

Expert Comment

by:agriesser
ID: 21924138
*bump*
Already had time to try one of the suggestions?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
NTP Server in VMware 5 394
mapBully challenge 6 178
How to check the PVU´s on AIX TSM servers? 3 121
SCO 5.0.7 on HP Proliant DL380 G5 will not shutdown properly. 6 55
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

730 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