How do i switch two columns around using sed?



  • Man page for sed:

    Sample file used:

    cat hostlist 
    11.11.20.5   hostA
    11.11.20.6   hostB
    11.11.20.7   hostC
    11.11.20.8   hostD
    

    Here we're matching switching numbers and letters, number in first column, letters in the second.

    Use this; the switch happens with the \2 and \1

    $ sed 's/^\([0-9][0-9].*\) \([a-Z]*\)/\2 \1/' hostlist
    

    Result:
    hostA 11.11.20.5
    hostB 11.11.20.6
    hostC 11.11.20.7
    hostD 11.11.20.8



© Lightnetics 2024