LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-30-2012, 11:13 AM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
Copying hastag comments recursively within <blocks>


Hi experts

I have two files, one with blocks like

Code:
<record>
line1
line2
line3
#model bla bla bla
</record>
while the other file has exactly the same number of <record> blocks, but without the hastag comments , ie like
Code:
<record>
line4
line5
line7
line8
line9
line10
</record>
So, what I want to do, is to copy from the 1rst <record> block of the first file the hashtag to the 1rst block of the second file so that my other file also looks like

Code:
<record>
line4
line5
line7
line8
line9
line10
#model bla bla bla
</record>
thanks in advance

Last edited by Alkass; 11-30-2012 at 11:17 AM.
 
Old 12-01-2012, 05:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
#!/bin/bash

sed -n '/^#/p' in1 > /tmp/in1.tmp

awk 'BEGIN { while (( getline < "/tmp/in1.tmp" ) > 0 )  _[cntr_1++] = $0 }
! /<\/record>/ { print $0 }
/<\/record>/   { print _[cntr_2++]"\n"$0 }
' in2

rm /tmp/in1.tmp
Code:
# content of in1
$ cat in1
<record>
line1 - 1
line2 - 1
line3 - 1
#model bla bla bla - 1
</record>
<record>
line1 - 2
line2 - 2
line3 - 2
#model bla bla bla - 2
</record>
# content of in2
$ cat in2
<record>
line4 - 1
line5 - 1
line7 - 1
line8 - 1
line9 - 1
line10 - 1
</record>
<record>
line4 - 2
line5 - 2
line7 - 2
line8 - 2
line9 - 2
line10 - 2
</record>
Code:
# running the above mentioned script
./insertline.sh
<record>
line4 - 1
line5 - 1
line7 - 1
line8 - 1
line9 - 1
line10 - 1
#model bla bla bla - 1
</record>
<record>
line4 - 2
line5 - 2
line7 - 2
line8 - 2
line9 - 2
line10 - 2
#model bla bla bla - 2
</record>
This does assume that the amount of records is the same and are in the same order.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying jpegs recursively while preserving the directory structure jsmith54 Linux - Newbie 1 08-09-2010 08:17 PM
doubt in cp command for copying recursively directories and files mq15 Linux - Newbie 3 04-19-2010 11:47 AM
copying directories recursively with find command nanda22 Linux - Newbie 1 09-01-2008 08:39 AM
RHEL 5 Kickstart copying files from DVD & inserting comments with sed ilo Linux - Enterprise 0 01-31-2008 01:01 PM
Copying hidden files recursively? mrd Linux - General 1 09-30-2005 08:51 AM


All times are GMT -5. The time now is 10:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration