Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

method to increase column widths 3

Status
Not open for further replies.
Jun 2, 2004
24
US
I am having issues getting awk to give me what I want.
All fields are pipe delimitted.
All first fields contain 7 chars.

I need:
1. column 2 to have a set width of 20 chars
2. column 4 to have a set width of 30 chars

BEFORE:

1829101|CHRYSLER|1961|WINDSOR|008|383|06277|6.3L|2BL| 20G
1815904|HONDA|1998|INTEGRA Type R|008|413|06769|6.8L|4BL| 40G


AFTER:

1829101|CHRYSLER |1961|WINDSOR |008|383|06277|6.3L|2BL| 20G
1815904|HONDA |1998|INTEGRA TYPE R |008|413|06769|6.8L|4BL| 40G


Much appreciated,
 
Code:
BEGIN {
  FS="[|]"
  OFS="|"
}
{
   $2 = sprintf("%-20s", $2);
   $4 = sprintf("%-30s", $4);
}
1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Good work, Vlad. I slightly altered your code to suit my tastes.

Code:
BEGIN { FS = OFS = "|" }

{ $2 = sprintf( "%-20s", $2 )
  $4 = sprintf( "%-30s", $4 )
  print
}
Although [tt]|[/tt] is a regular expression meta-character, it isn't necessary
to say [tt]FS="[|]"[/tt], because when FS is a single character it is treated
literally.

And [tt]print[/tt] seems a bit more clear to me than [tt]1[/tt].

For those who may be wondering, [tt]sprintf( "%-20s", $2 )[/tt] produces a
string that is the second field left-aligned in a width of 20.
 
a
star.gif
to futurelet!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top