Search code examples
awksedpastecut

Copy end of string and paste to new column


Which UNIX command do I use to copy the end of a string in column 1 and paste it to a new column?

For example,

> $ head -3 test1.bed 
> HLA:HLA00001_A_01:01:01:01_3503  1 
> HLA:HLA02169_A_01:01:01:02N_3291 1 
> HLA:HLA14798_A_01:01:01:03_2903  1 

Becomes,

> $ head -3 test1.bed 
> HLA:HLA00001_A_01:01:01:01_3503  1 3503
> HLA:HLA02169_A_01:01:01:02N_3291 1 3291
> HLA:HLA14798_A_01:01:01:03_2903  1 2903

Solution

  • awk to the rescue!

    $ awk '{n=split($1,a,"_"); print $0, a[n]}' file
    
    HLA:HLA00001_A_01:01:01:01_3503  1  3503
    HLA:HLA02169_A_01:01:01:02N_3291 1  3291
    HLA:HLA14798_A_01:01:01:03_2903  1 2903