Search code examples
bashcmdcygwin

reformating a text file


I've looked for existing answers, but am at a loss.

I need to take a plain text file:

1 - round decimals to tenths

2 - last group of numbers need to be four characters long, using zero to fill

3 - remove periods - found sed solution

4 - remove spaces - found sed solution

here is an example of the file I'm working with

5.41   3.08  795
4.82   2.69   19
3.60   2.83    8
4.24   3.10 1559

needs to be changed to:

54310795
48270019
36280008
42311559

Thanks - Andy


Solution

  • you can do it in one-shot with awk:

     awk '{$0=sprintf("%.1f%.1f%04d", $1,$2,$3);gsub(/\./,"")}1' file 
    

    test:

    kent$  cat file
    5.41   3.08  795
    4.82   2.69   19
    3.60   2.83    8
    4.24   3.10 1559
    
    kent$  awk '{$0=sprintf("%.1f%.1f%04d", $1,$2,$3);gsub(/\./,"")}1' file
    54310795
    48270019
    36280008
    42311559