Operating System - HP-UX
1834914 Members
2245 Online
110071 Solutions
New Discussion

sed or awk on flat file with no separators swap data on line

 
SOLVED
Go to solution
Ratzie
Super Advisor

sed or awk on flat file with no separators swap data on line

I have a flat file the is 773 charecters long.

What I need to do is swap two data fields around.
Both are 38 characters, so I do not need to pad. (blank spaces)

I need to swap char 1-38 data field with char 87-124 data field for each line.

I loop will do, but will I need to store these in variables?
Appreciate the help.

11 REPLIES 11
Mel Burslan
Honored Contributor

Re: sed or awk on flat file with no separators swap data on line

Not elegant but simple solution:

f1=`cat myfile | cut -c 1-38`
f2=`cat myfile | cut -c 39-86`
f3=`cat myfile | cut -c 87-124`
f4=`cat myfile | cut -c 125-`

echo ${f3}${f2}${f1}${f4} > mynewfile
________________________________
UNIX because I majored in cryptology...
H.Merijn Brand (procura
Honored Contributor

Re: sed or awk on flat file with no separators swap data on line

Is perl an option?

# perl -pe'@f=unpack"a38a48a3a*",$_;$_=join"",@f[2,1,0,3]' file

Many other options available. TMTOWTDI

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: sed or awk on flat file with no separators swap data on line

Hi:

# perl -nle 'print $3.$2.$1.$4 if m/(.{38})(.{48})(.{38})(.{649})/' filename

Regards!

...JRF...
Jonathan Fife
Honored Contributor

Re: sed or awk on flat file with no separators swap data on line

cat file | sed -e '/\(......................................\)\(....................................................\)\(......................................\)\(.*\)/\3\2\1\4/g'
Decay is inherent in all compounded things. Strive on with diligence
Ratzie
Super Advisor

Re: sed or awk on flat file with no separators swap data on line

Here is a twist...
Thanks for the perl works great, how about only changing the lines that at the 206 & 207 char = XG?

Thanks
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed or awk on flat file with no separators swap data on line

Hi (again):

# perl -nle 'if (m/.{205}XG/ and m/(.{38})(.{48})(.{38})(.{649})/) {print $3.$2.$1.$4} else {print}' filename

Regards!

...JRF...
Ratzie
Super Advisor

Re: sed or awk on flat file with no separators swap data on line

Awesome expect on the else, if adds a ^M at the end to increase the line to 774.
James R. Ferguson
Acclaimed Contributor

Re: sed or awk on flat file with no separators swap data on line

Hi (again):

Perhaps:

# perl -ne 'chomp;if (m/.{205}XG/ and m/(.{38})(.{48})(.{38})(.{649})/) {print $3.$2.$1.$4} else {print}' filename

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: sed or awk on flat file with no separators swap data on line

Not that I disapprove of the regex solutions, note however that pack/unpack is both easier and about 23% faster:

# perl -MBenchmark=:all -le'$a="a"x800;cmpthese(-2,{re=>q{$a=~m/(.{38})(.{48})(.{38})(.{649})/},pu=>q{unpack"a38a48a3a*",$a}})'
Rate re pu
re 263161/s -- -19%
pu 323570/s 23% --

worse for the .{238} thing, which is wrong anyway, as it ix not anchored. Please use substr there:

# perl -MBenchmark=:all -le'$a="aXG"x300;cmpthese(-2,{re=>q{$a=~m/^.{205}XG/},pu=>q{(unpack"x205a2",$a)[0]eq"XG"},ss=>q{substr($a,205,2)eq"XG"}})'
Rate re pu ss
re 782184/s -- -10% -66%
pu 868848/s 11% -- -63%
ss 2327702/s 198% 168% --

Regular expressions are powerful, and fun, but not the right tool for the given problem

Enjoy, Have FUN! H.Merijn



Enjoy, Have FUN! H.Merijn
Ratzie
Super Advisor

Re: sed or awk on flat file with no separators swap data on line

So how would I take what you did and combine the two for use?
Hein van den Heuvel
Honored Contributor

Re: sed or awk on flat file with no separators swap data on line

It's hidden in the benchmark code;

# perl -pe'if (substr($a,205,2)eq"XG") {@f=unpack"a38a48a3a*",$_;$_=join"",@f[2,1,0,3]}' file

>> flat file with no separators

seperators are overrated and a waste of space, cpu time, and flexibility.


The extra carriage return may have been an artifact of the subsequent transfer to OpenVMS

Please consider looking arounf for the right tools on OpenVMS to do the job 'in place'.

That field in position 205 may well have been an 'alternate key' and so you might have been able to jump straight to those target records withou reading any others... on OpenVMS.