Change of fields order inline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change of fields order inline
# 1  
Old 11-11-2015
Change of fields order inline

Hi everyone,

What is the best solution to check every line in the xml file and change order of found field along with its value without touching value. Pattern will be given i.e. one line can look like this one:
Code:
<widget position="value,value" size="value,value" name="value" foregroundColor="value" zPosition="value" transparent="value" />

but I want to have always the same order i.e.
Code:
<widget name="value" position="value,value" size="value,value" foregroundColor="value" transparent="value" zPosition="value" />

I have to also remember that not every line has all of above fields so if line does not contain foregroundColor="value" it has to omit it but still follow field which suppose to be next.

Thank you for any help.
# 2  
Old 11-11-2015
Heya

Any attempts/tries from your side yet?
# 3  
Old 11-11-2015
Not yet cos not sure if better to use awk (I will have to go through docs) or script with regex and loop but I do not scripting on daily basis so it will take a lot of time to make it work.

ps. I also trying to achieve it over notepadd++ macro.
# 4  
Old 11-11-2015
with some assumptions:
Code:
 awk '{$2=$4 OFS $2;$4="";$(NF-2)=$(NF-1) OFS $(NF-2); $(NF-1)=""}1' myFile.xml

# 5  
Old 11-11-2015
I am not a notepad++ user, but the following seems to do what you want at the shell prompt level:
Code:
awk '
BEGIN {	n = split("name position size foregroundColor transparent zPosition", order)
}
{	for(i = 2; i < NF; i++)
		f[substr($i, 1, index($i, "=") - 1)] = $i
	printf("%s ", $1)
	for(i = 1; i <= n; i++)
		if(order[i] in f) {
			printf("%s ", f[order[i]])
			delete f[order[i]]
		}
	print $NF
}' file.xml

assuming (as in your example) that there aren't any spaces or tabs between your input pairs of double quotes.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 11-11-2015
Try also
Code:
awk  -v SEQ="name position size foregroundColor transparent zPosition" '
BEGIN   {CNT = split (SEQ, SQ)
        }

        {delete OUT
         for (i=2; i<NF; i++)   {split ($i, T, "=")
                                 OUT[T[1]]=T[2]
                                }
         printf "%s", $1
         for (i=1; i<=CNT; i++) printf " %s=%s", SQ[i], OUT[SQ[i]]
         printf " %s\n", $NF
        }
' file
<widget name="value" position="value,value" size="value,value" foregroundColor="value" transparent="value" zPosition="value" />
<widget name="value" position="value,value" size="value,value" foregroundColor="value" transparent="value" zPosition="value" />
<widget name="value" position= size="value,value" foregroundColor="value" transparent="value" zPosition="value" />

You'll need to specify what should happen with missing fields: omit entirely, print the label with either empty double quotes or totally empty.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change row by order nr?

Hello, I have a file with thousands of rows and I need to change sequence of lines. Sample file: #NAME #SERVICE 112233 #DESCRIPTION AABBCCDD #SERVICE 738292 #DESCRIPTION FFYYRRTT ... ... ... Desired output: #NAME (5 Replies)
Discussion started by: baris35
5 Replies

2. UNIX for Dummies Questions & Answers

change service order

hi guys I have a service that depends on some shares (NFS shares ) that need to be mounted before before the service start so the service-app finds the NFS shares and starts correctly... I am confused here this is what I found but I am not sure what to do in order to change it BTW is Suse... (2 Replies)
Discussion started by: karlochacon
2 Replies

3. Shell Programming and Scripting

change order

I have inside a file 22 25 80 111 631 694 861 875 I need this in the form using awk or sed 22,25,80,111,631,694,861,875 (4 Replies)
Discussion started by: anil510
4 Replies

4. UNIX for Dummies Questions & Answers

printing fields in reverse order

command/script(apart from awk) to print the fields in reverse order that is last field has to come first and so on and first field has to go last Input store-id date sale ............. ............. ... (3 Replies)
Discussion started by: tsurendra
3 Replies

5. Shell Programming and Scripting

How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe) abc|xyz 123|567 i have to get the above in reverse order xyz|abc 567|123 Pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Change order

Good evening I have a file as below and want to change the order, as in the second column, sed awk Pearl Thanks aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa cccccccc bbbbbbbbb aaaaaaaaaa cccccccc bbbbbbbbb (8 Replies)
Discussion started by: Novice-
8 Replies

7. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

8. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies

9. UNIX for Dummies Questions & Answers

change order of fields in header record

Hello, after 9 months of archiving 1000 files, now, i need to change the order of fields in the header record. some very large, space padded files. HEADERCAS05212008D0210DOMEST01(spacepadded to record length 210) must now be 05212008HEADERCASD0210DOMEST01(spacepadded to record length 210) ... (1 Reply)
Discussion started by: JohnMario
1 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question