Dividing tab blocks in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dividing tab blocks in bash script
# 1  
Old 01-31-2013
Dividing tab blocks in bash script

Hi everyone,
I have a data.xml file which only contains thousands of data (tag) blocks. A part of the file looks exactly like this;
Code:
<data>
Line
Line
Line
</data>
<data>
Line
Line
Line
</data>

the rest of the file is simply a repetition of this part. Here each data block contains a number of multiple data lines. I need to separate each data lines so that it will look like;
Code:
<data>
Line
</data>
<data>
Line
</data>
<data>
Line
</data>
<data>
Line
</data>
<data>
Line
</data>

I need to write a bash script which reads data.xml and then executes the necessary operations. I guess the script file should have a loop with a conditional statement (if...then...else).

Any help will be highly appreciated.
thanks a lot.
# 2  
Old 01-31-2013
Code:
awk '/<data>/{f=1}/<\/data>/{f=0}f==1&&!/data/{printf "<data>\n%s\n</data>\n",$0;}' xml_filename

OR
Code:
awk '/<data>/{f=1;next;}/<\/data>/{f=0;next;}f==1{printf "<data>\n%s\n</data>\n",$0;}' xml_filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-31-2013
thanks a lot bipinajith, both works flawlessly Smilie
# 4  
Old 01-31-2013
Could you please explain step by step what you did there?
# 5  
Old 01-31-2013
You can do it in sed -- read in second and third lines with N, N and if
open data, line, line
then make it
open data, line, close data, open data, line
then P out and discard three lines, N another line and branch back to if.
Else P out and discard one line and back to second N and if.

Do a $q before each N.
# 6  
Old 01-31-2013
Code:
awk '/<data>/{                          # If line contains pattern: <data>
  f=1;                                  # Set flag variable f = 1
  next;                                 # Skip current record
 } /<\/data>/ {                         # If line contains pattern: </data> - Since / is meta-character I escaped it \/
  f=0;                                  # Set flag variable f = 0
  next;                                 # Skip current record
 } f==1 {                               # If line flag variable is equal to 1 f == 1
  printf "<data>\n%s\n</data>\n",$0;    # Print <data> -- newline -- followed by current record : $0 -- newline -- followd by </data>
}' xml_filename                         # Read filename: xml_filename

# 7  
Old 01-31-2013
I'd think you would need to print '</data>\n<data>\n%s\n",$0 to close out the first and put the second in a new tag. OK, you are discarding the old tags. That works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] Extra tab opens

Hello everyone, This code is working right using mate-terminal but with xfce4-terminal for some reason, it open up an extra tab... Could someone please help me out to understand why this is happening? #!/bin/bash cd "$(dirname "$0")"/files tab=" --tab" title=" --title" options=()... (2 Replies)
Discussion started by: soichiro
2 Replies

2. Shell Programming and Scripting

Shell Script Comment code blocks in a bash source file

Just began to learn on Shell Script. I got an exercise from my friend. I know how to make this happen in C, but I'm not familiar with Shell Script. Hope I can get some help from all of you. I want to write a bash script to comment code blocks in a bash source file. What I mean comment is '#', I... (1 Reply)
Discussion started by: HiFuture0801
1 Replies

3. UNIX for Dummies Questions & Answers

Bash Tab Completion Hanging

Hi, I'm having a problem with tab completion at the bash command line. For some reason, whenever I type g<tab>, the terminal will freeze up for 5-10 seconds before asking me if I want to display all 325 possibilities. I thought that maybe it's because of the high number of commands, but I have... (4 Replies)
Discussion started by: Raz716
4 Replies

4. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

5. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

6. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

7. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

8. Shell Programming and Scripting

Help with Script using Command Blocks

Hello, I am trying to create a shell script that use command block (donīt really know if this is the correct way to say it), but while one version works fine, the other one is not working at all. So let me show an example of this "command block" Iīm using and its working ok: cat << _EOF_ `echo... (7 Replies)
Discussion started by: Alexis Duarte
7 Replies

9. Shell Programming and Scripting

Bash script [Press Tab] Screen Blank..

Dear Member, OLD Question --> disable-completion not solved My bash Menu script ping process problem. If ping still running and users press SCREEN is Blank... Cant Members help me.. kill signal or others scripting for my case, btw i use Linux.. Thanks, Rico My Bash Script : ... (1 Reply)
Discussion started by: carnegiex
1 Replies

10. Shell Programming and Scripting

bash: dividing/multiplying variables resulting in decimals

(4 Replies)
Discussion started by: puddy
4 Replies
Login or Register to Ask a Question