InterviewSolution
| 1. |
How Do I Handle Fixed-length, Columnar Data? |
|
Answer» Sed handles fixed-length fields via (grouping) and backreferences (1, 2, 3 ...). If we have 3 fields of 10, 25, and 9 CHARACTERS per FIELD, our sed script might look like so: s/^(.{10})(.{25})(.{9})/321/; # Change the fields This is a bit hard to read. By using GNU sed or ssed with the -r switch ACTIVE, it can look like this: s/^(.{10})(.{25})(.{9})/321/; # Using the -r switch To delete a field in sed, use grouping and omit the backreference from the field to be deleted. If the data is long or difficult to work with, use ssed with the -R switch and the /x flag after an s/// command, to insert comments and remarks about the fields. For records with many fields, use GNU AWK with the FIELDWIDTHS variable set in the top of the script. For EXAMPLE: awk 'BEGIN{FIELDWIDTHS = "10 25 9"}; {print $3 $2 $1}' file This is much easier to read than a similar sed script, especially if there are more than 5 or 6 fields to manipulate. Sed handles fixed-length fields via (grouping) and backreferences (1, 2, 3 ...). If we have 3 fields of 10, 25, and 9 characters per field, our sed script might look like so: s/^(.{10})(.{25})(.{9})/321/; # Change the fields This is a bit hard to read. By using GNU sed or ssed with the -r switch active, it can look like this: s/^(.{10})(.{25})(.{9})/321/; # Using the -r switch To delete a field in sed, use grouping and omit the backreference from the field to be deleted. If the data is long or difficult to work with, use ssed with the -R switch and the /x flag after an s/// command, to insert comments and remarks about the fields. For records with many fields, use GNU awk with the FIELDWIDTHS variable set in the top of the script. For example: awk 'BEGIN{FIELDWIDTHS = "10 25 9"}; {print $3 $2 $1}' file This is much easier to read than a similar sed script, especially if there are more than 5 or 6 fields to manipulate. |
|