Moving part of Text in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving part of Text in a file
# 1  
Old 02-03-2006
Moving part of Text in a file

Hi,

I have this text in a file where I need to move part of the text....

<Relation1 OriginatingObjectID="Holding_1" RelatedObjectID="Party_1" id="Relation_1">
<OriginatingObjectType tc="4">Holding</OriginatingObjectType>
<RelatedObjectType tc="6">Party</RelatedObjectType>
<RelationRoleCode tc="32">Insured</RelationRoleCode>
</Relation1>
<Relation2 OriginatingObjectID="Holding_2" RelatedObjectID="Party_2" id="Relation_2">
<OriginatingObjectType tc="4">Holding</OriginatingObjectType>
<RelatedObjectType tc="6">Party</RelatedObjectType>
<RelationRoleCode tc="32">Insured</RelationRoleCode>
</Relation2>

I want only the first line of each block in this text to be modified to

<Relation1 id="Relation_1" OriginatingObjectID="Holding_1" RelatedObjectID="Party_1">
similarly for second block ..
<Relation1 id="Relation_2" OriginatingObjectID="Holding_2" RelatedObjectID="Party_2">
and so on....
I tried it this way but could not get the complete solution...
grep "Relation1 " New_Text.txt | awk '{ print "<Relation " $4" "$2" "$3">"}'
but The result is ..
<Relation id="Relation_1"> OriginatingObjectID="Holding_1" RelatedObjectID="Party_1">
I do not want "id="Relation_1"> " instead I want id="Relation_1" This ">"should not appear after "Relation_1"...and how do I implement it in a text file where I have many blocks to be modified...and also have other blocks which does not need any change..?
# 2  
Old 02-04-2006
>cat ty.awk

Code:
BEGIN {
FS=" "
}
{
if( $4 ~ /Relation_/ )
{
	print $1" "$4" "$2" "$3">"
}
else
{
	print $0">"
}
}

Code:
sed 's/>$//' file | awk -f ty.awk

# 3  
Old 02-06-2006
That works perfectly Thanks a lot......
# 4  
Old 02-06-2006
Why the sed and the awk ?

Just the sed would do as well.

Code:
sed -e 's_^\(<Relation[0-9]*\) \(.*\) \(.*\) \(.*\)>$_\1 \4 \2 \3>_g' input.txt

# 5  
Old 02-06-2006
This works with slight modification...
sed -e 's_\(<Relation[0-9]*\) \(.*\) \(.*\) \(.*\)>$_\1 \4 \2 \3>_g' input.txt

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Renaming part of a text file ?

I have several files that are named like this "DotP_D14 - Verknüpfung" They all have the " - Verknüpfung" in common. I'd like to rename all of them to get rid of that last part. Is this possible with DOS on windows ? (1 Reply)
Discussion started by: pasc
1 Replies

2. Shell Programming and Scripting

To display the selected part in text file of unix

0400903071220312 20120322 20:21 1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363 12122011AUS 182644 000C2 8122011 0000 000 1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363 12122011AUS ... (6 Replies)
Discussion started by: rammm
6 Replies

3. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

4. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

5. Shell Programming and Scripting

Remove part of a column in a text file

I have a text file with delimiter "|" and sometimes the zipcode is in "5th" column or "6th" column. I want to scan the file and remove the "-2323" from the zipcode which has zip+4 digits From blah|blah|foo|bar||blah|945523-232|USA blah|blah|foo|bar||blah|foo|94555-2323|USA To... (8 Replies)
Discussion started by: gubbu
8 Replies

6. Shell Programming and Scripting

moving text within file

I want to mvoe lines around in a file. Say I have 30 lines in a file and want to move the last 5 lines to the top of the file..how can this be done? i thought of awk and sed but was not sure context. please assist thanks (5 Replies)
Discussion started by: sigh2010
5 Replies

7. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

8. Shell Programming and Scripting

Moving a part of the text in a file

*************** #some other text ***************** *************** #some other text ***************** address1=1.1.1.1 address2=2.2.2.2 address3=3.3.3.3 I have a file where i need to push all the text starting from address1 till end of file to, below . Can anyone of you... (6 Replies)
Discussion started by: srikanthgoodboy
6 Replies

9. Shell Programming and Scripting

Get the latest added part of a text file?

Hi, I want to get the latest added part of a text file sent over the network to for analysis. Is there a tool to use to keep track of which part of a text file that has already been analysed so only the "new" lines will be sent and handled? I have checked a few tools but I still don´t know how to... (3 Replies)
Discussion started by: pcrs
3 Replies

10. UNIX for Dummies Questions & Answers

moving apache part 2

Hi everybody..... please help me in moving apache 2.2 over to another server remotely............. still having problems thank you (12 Replies)
Discussion started by: marinob007
12 Replies
Login or Register to Ask a Question