Results 1 to 2 of 2

Thread: awk help

  1. #1
    Join Date
    Feb 2004

    Red face Unanswered: awk help

    I have a file that looks like this ...

    93459;217800;American Company;LF
    238929;2800;Test Company;LF

    I need to format it to look like ..

    2800;238929;LF;Test Company
    217800;93459;LF;American Company

    I ran the command

    awk -F; '{print $2";"$1";"$4";"$3}'

    This command however formats my data as such..

    2800;238929;LF ;Test Company
    217800;93459;LF ;American Company

    I believe the reason this is happening is because there are some blank spaces after "LF". Is there anyway to get it to format the way I want it without going through the whole file and deleting all the blank spaces.

  2. #2
    Join Date
    Jun 2002
    You could substitute the trailing spaces of $4 with an empty string. Also, to use ";" as the Output Field Separator, you may aswell set OFS.
    awk 'BEGIN(FS=";"; OFS=";"}{sub(/ *$/,"",$4); print $2,$1,$4,$3}' yourFile

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts