Furl.awk: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(Created page with "Your data is like this: <pre> record 1 field 1 record 1 field 2 record 1 field 3 record 1 field 4 record 1 field 5 record 2 field 1 record 2 field 2 record 2 field 3 record 2...")
 
No edit summary
Line 47: Line 47:
</pre>
</pre>


output:
<pre>
record 1 field 1,record 1 field 2,record 1 field 3,record 1 field 4,record 1 field 5
record 2 field 1,record 2 field 2,record 2 field 3,record 2 field 4,record 2 field 5
record 3 field 1,record 3 field 2,record 3 field 3,record 3 field 4,record 3 field 5
</pre>


[[Category:Script]]
[[Category:Script]]

Revision as of 20:24, 16 July 2021

Your data is like this:

record 1 field 1
record 1 field 2
record 1 field 3
record 1 field 4
record 1 field 5
record 2 field 1
record 2 field 2
record 2 field 3
record 2 field 4
record 2 field 5
record 3 field 1
record 3 field 2
record 3 field 3
record 3 field 4
record 3 field 5
  1. no record seprator
  2. fixed number of fields per record
  3. number of field is 5 ( obviously you can change this to suit )

try this:

awk -f furl.awk datafile.txt

ful.awk is:

{
if      ( NR%5 == 1 )
  printf "%s," , $0
else if ( NR%5 == 2 )
  printf "%s," , $0
else if ( NR%5 == 3 )
  printf "%s," , $0
else if ( NR%5 == 4 )
  printf "%s," , $0
else if ( NR%5 == 0 )
  print $0
else
  print "NOPE";
}

output:

record 1 field 1,record 1 field 2,record 1 field 3,record 1 field 4,record 1 field 5
record 2 field 1,record 2 field 2,record 2 field 3,record 2 field 4,record 2 field 5
record 3 field 1,record 3 field 2,record 3 field 3,record 3 field 4,record 3 field 5