Hi all,i am very new to shell and awk scripting,..hope u guys can help...

I need help in sorting and grouping the data in one flat file.The file look like this:

040171011140820070000000009650244002933170003000000075272
1F921338300506 01082007000014000000027665
1H912279980109 01082007000012000000042420
1S503377200110 01082007000014000000005187
1S503377200110 01082007000688000000005188
3SF98


I need to rearrange the file to become like this:

040171011140820070000000009650244002933170003000000075272 3SF98
1
2S503377200110 01082007000688000000005188
1
2F921338300506 01082007000014000000027665
2S503377200110 01082007000014000000005187
2H912279980109 01082007000012000000042420

The Rules:
i)I need to append the whole line that starts with '3' to first line (that start with 0) -lets say length for first line is 70,so new line start from position 71
ii)i also need to replace fisrt character of '1' to become '2'
iii)and group them according to the position 28 till length 3,'688' will have one group and other than '688' (in this case '012','014' )will be grouped in one group (and need to sort in desc order,means 688 first followed by others)
iv)then i need to add 1 for each new group (excluding line that starts with 0)

i have tried this script but i cannot think how to exclude the first line to not add '1' before the line:

sort -k2 FileLoad.txt.1 |awk '/^1/{sub(/1/,"2")}!x[substr($0,28,3)]++{print 1}1'

The output become like this:

1
3SFVPVKI349009876
1
040171011140820070000000009650244002933170003000000075272
1
2H912279980109 01082007000012000000042420
1
2S503377200110 01082007000014000000005187
2F921338300506 01082007000014000000027665
1
2S503377200110 01082007000688000000005188


Please help....Tq in advance

Recommended Answers

All 3 Replies

Hey There,

If this line of code consistently puts a separate distinct line with 1 as the intro line, you can fix your problem the simplest way by just adding a pipe to the end:

yourCommandString | sed 1d

, Mike

Who is giving you such assignments :)

If the output should be really as the one you show:

sort -t" " -rk2.12,2.14 filename | awk '
/^1/ { sub(/1/, "2") }
/^3/ { saved = $0 }
/^0/ { $NF = ($NF FS saved) }
{ x[FNR] = $0 }END{
for (i=1; i<=FNR; i++)
		if (x[i] ~ /^0/)
			print x[i]
			for (j=1; j<=FNR; j++)
				if (x[j] !~ /^[03]/)
					print (!y[substr(x[j], 28, 1)]++) ? (1 RS x[j]) : x[j]
}'

But if you have to group them according to the position 28 till length 3, 12 and 14 must be separated:

sort -t" " -rk2.12,2.14 filename | awk '
/^1/ { sub(/1/, "2") }
/^3/ { saved = $0 }
/^0/ { $NF = ($NF FS saved) }
{ x[FNR] = $0 }END{
for (i=1; i<=FNR; i++)
		if (x[i] ~ /^0/)
			print x[i]
			for (j=1; j<=FNR; j++)
				if (x[j] !~ /^[03]/)
					print (!y[substr(x[j], 28, 3)]++) ? (1 RS x[j]) : x[j]
}'

thx all;

i really appreacite it

radouluv;
u're the one who help me in unix.com forum rite?
many thx...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.