Search code examples
awksedtext-processing

swich column in text.file


i have file.txt like this


4142 2019-01-08T.002:00 JrNewman 080592Kl Developer 
2125 2016-11-455.+02:00 Jackson John 180784SRE1 Maintainer 
2293 2016-12-4100+02:00 Jackson John AZbissness Developer 
1938 2015T12:1600+02:00 Jackson John AZ19043A4A Developer 
2126 2016-123:46.003:00 Jackson John OZ40784SA3 Maintainer 
2033 201615:33:28.00:00 Allen Robert JR pDbissness Developer 
4846 202212:48:34+02:00 Walker Tom JR autotestbit Maintainer 
5362 2022-12:440+302:00 Walker Tom Jr autotestbit Maintainer 
2131 2012T21:.000+03:00 Allen Robert Jr Z1EW843V1 Maintainer 
2128 2013T206.000+02:00 Administrator LL23042DE Guest 

need something like this

4142 2019-01-08T.002:00 080592Kl Developer JrNewman
2125 2016-11-455.+02:00 180784SRE1 Maintainer Jackson John
2293 2016-12-4100+02:00 AZbissness Developer Jackson John
1938 2015T12:1600+02:00 AZ19043A4A Developer Jackson John
2126 2016-123:46.003:00 OZ40784SA3 Maintainer Jackson John
2033 201615:33:28.00:00 pDbissness Developer Allen Robert JR
4846 202212:48:34+02:00 JR autotestbit Maintainer Walker Tom
5362 2022-12:440+302:00 autotestbit Maintainer Walker Tom Jr
2131 2012T21:.000+03:00 Z1EW843V1 Maintainer Allen Robert Jr
2128 2013T206.000+02:00 LL23042DE Guest Administrator

I understand that I can use awk print $column and write to a file . but I can't do it ((


Solution

  • With your shown samples, please try following awk code. Written and tested in GNU awk, should work in any awk. Simple explanation would be using match function to get first 2 fields first in val1 variable and get its length in len variable. Then using another match to match last 2 fields and then printing 1st 2 fields, last 2 fields and in between all rest of the name values(could be single value or 2 value or so on).

    awk '
    match($0,/^[^[:space:]]+[[:space:]]+[^[:space:]]+[[:space:]]+/){
      val1=substr($0,RSTART,RLENGTH)
      len=RSTART+RLENGTH
    }
    match($0,/[^[:space:]]+[[:space:]]+[^[:space:]]+$/){
      print val1 substr($0,RSTART,RLENGTH),substr($0,len,RSTART-len)
    }
    ' Input_file