Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The sed FAQ
Prev Home Next

4.13. How do I handle fixed-length, columnar data?

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\}\)/\3\2\1/;  # Change the fields
^^^^^^^^^^^~~~~~~~~~~~========== # from 1,2,3 to 3,2,1 field #1 field #2 field #3

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})/\3\2\1/;          # 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.

The sed FAQ
Prev Home Next

 
 
   Reprinted courtesy of Eric Pement. Also available at https://sed.sourceforge.net/sedfaq.html Design by Interspire