Break a file into separate files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break a file into separate files
# 1  
Old 10-27-2006
Break a file into separate files

Hello I am facing a scenario where I have a file with XML content and I am running shell script over it. But the problem is the XML is getting updated with new services. In the below scenario, my script takes values from the xml file from one service name say ABCD. Since there are multiple, it is erroring out.

<service name="ABCD">
<enabled>true</enabled>
<settings>
<setting name="Process Archive">
</setting>
</settings>
...
...
...
</service>
<service name="EFGH">
<enabled>true</enabled>
<settings>
<setting name="Process Archive">
</setting>
</settings>
...
...
...
</service>
<service name="IJKL">
<enabled>true</enabled>
<settings>
<setting name="Process Archive">
</setting>
</settings>
...
...
...
</service>

I need help in breaking the XML file into separate files. I need it to be
<service name="ABCD">
<enabled>true</enabled>
<settings>
<setting name="Process Archive">
</setting>
</settings>
...
...
...
</service>
I am using sed to break it, but the problem is, we don't know the service name "ABCD" to be used in the script and I am not able to find a string to dynamically get from <service to </service>.

Really appreciate your ideas..Thanks
Chiru
# 2  
Old 10-27-2006
Code:
awk '{ 
       if( $1 == "<service" ) 
       { 
           n=split($2,arr,"\""); 
           file=arr[n-1]".txt"
       }
       print > file }' inputfile

# 3  
Old 10-29-2006
Hello Anbu,
I didn't understand the code you gave, so I wasn't able to know what the error I am getting when I exactly ran what you gave me.
Can you please look into it and help me out...
awk: cmd. line:5: file=arr[n-1]".txt
awk: cmd. line:5: ^ unterminated string

awk '{
> if( $1 == "<service" )
> {
> n=split($2,arr,"\"");
> file=arr[n-1]".txt
> }
> print > file }' temp_1029.txt

Please help...Thanks
chiru
# 4  
Old 10-30-2006
Try to add a space between ] and "
Code:
file=arr[n-1] ".txt"


Jean-Pierre.
# 5  
Old 10-30-2006
I still get the same error ?? Smilie

awk '{
> if ( $1 == "<bw" )
> {
> n=split($2,arr,"\"");
> file=arr[n-1] ".txt
> }
> print > file }' temp_1030.txt
awk: cmd. line:5: file=arr[n-1] ".txt
awk: cmd. line:5: ^ unterminated string
# 6  
Old 10-30-2006
I figured out..
there needs to be a space in the split command...
n=split($2,arr,"\"");
--> n=split($2, arr, ,"\"");

Thanks guys..
-Chiru
# 7  
Old 10-30-2006
Quote:
Originally Posted by chiru_h
I still get the same error ?? Smilie

awk '{
> if ( $1 == "<bw" )
> {
> n=split($2,arr,"\"");
> file=arr[n-1] ".txt
> }
> print > file }' temp_1030.txt
awk: cmd. line:5: file=arr[n-1] ".txt
awk: cmd. line:5: ^ unterminated string

file=arr[n-1] ".txt"
You missed the double quotes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break output file into three files

Help! :) I am getting an output file that looks similar to below. EMAIL_ADDR ----------------------------------------------------------------------------------- user@gmail.com DATABASENAME ----------------------------------------------------------------------------------- db1 db2 db3... (6 Replies)
Discussion started by: cpolikowsky
6 Replies

2. UNIX for Dummies Questions & Answers

Script to break up file (write new files) in bash

Hello experts, I need help writing individual files from a data matrix, with each new file being written every time there is a blank line: From this cat file.txt col1 col2 col3 6661 7771 8881 6661 7771 8881 6661 7771 8881 col1 col2 col3 3451 2221 1221... (6 Replies)
Discussion started by: torchij
6 Replies

3. Shell Programming and Scripting

Print the overlapping entries in 2 files to separate file

I have two files that contain overlapping positions. i want to put them together each overlapping entries in both files in to a new file (the entries of first file first and the entries of second file next) followed by blank line then next overlapping entries and so on. input1 chr1 22 ... (10 Replies)
Discussion started by: raj_k
10 Replies

4. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

5. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

6. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

7. Shell Programming and Scripting

Splitting text file into 2 separate files ??

Hi All, I am new to this forumn as well to the UNIX, I have basic knowledge of UNIX which I studied some years ago, now I have to do some shell scripting to load data into Oracle database using sqlldr utility, whcih I am able to do. I have a requirement where I need to do following operation. I... (10 Replies)
Discussion started by: shekharjchandra
10 Replies

8. Shell Programming and Scripting

Renaming files by matching info from a separate file

Hi All, I could use a bit of help with this as I'm at a loss. I have a number of files all named accordingly: I have a separate text file that is as follows: What I want to do is end up with: I don't know how to do this, though I'm certain it can be done, and would rather learn how... (14 Replies)
Discussion started by: Demosthenes
14 Replies

9. UNIX for Dummies Questions & Answers

to break a file into 2 files after matching a pattern.

Hi, i need to break a file into 2 files afetr matching a pattern for ex. there is a fil, file .txt which contains here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line... (3 Replies)
Discussion started by: manit
3 Replies

10. Shell Programming and Scripting

compare two .dat files and if there is any difference pulled into a separate file

Hi, compare two .dat files and difference will be moved into separate file.if anybody having code for this please send asap. using diff command, i don't know how to write shell programming. and my first file is like this including Header and trailer 10Ç20060323Ç01(Header) 01ÇIÇbabuÇ3000 01ÇIÇbaluÇ4000... (1 Reply)
Discussion started by: kirankumar
1 Replies
Login or Register to Ask a Question