swich column in text.file

Viewed 91

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 ((

10 Answers

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

Another where the two last fields are appended to the second field:

$ awk '{
    $2=$2 OFS $(NF-1) OFS $NF  # append the 2 last fields to $2
    NF-=2                      # remove the 2 last fields
    # $0=$0                    # rebuild the record if you need to process further
}1' file                       # output

Selected recods of output:

4142 2019-01-08T.002:00 080592Kl Developer JrNewman
2125 2016-11-455.+02:00 180784SRE1 Maintainer Jackson John
2033 201615:33:28.00:00 pDbissness Developer Allen Robert JR

You may use this sed command:

sed -E 's/^(([^[:blank:]]+[[:blank:]]+){2})([[:alnum:][:blank:]]+)[[:blank:]]+([[:alnum:]]+[[:blank:]]+[[:alnum:]]+)/\1\4 \3/' file

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 autotestbit Maintainer Walker Tom JR
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

RegEx Demo

Using sed

$ sed 's/\(\([^ ]* \)\{2\}\)\(.* \)\(\([^ ]* \)\{2\}\)$/\1\4\3/' input_file
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 autotestbit Maintainer Walker Tom JR
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

Yet another alternative, with perl:

perl -lane 'print "@F[0,1,-2,-1,2..@F-3]"' file.txt

Your data is too irregular, the name has two columns and three columns, not very good handling ,you can test this script

cat  file.txt|awk -F ' ' '{print $1" "$2" "$4" "$5" "$3}'

the script tested only the first line is correct

The example data seems to have at least 5 columns, where you want to keep the first 2 in the same order, then append the last 2 columns after it and end with a variable amount of columns in between.

You might also check for example that the first column has 4 digits, the second column starts with 4 digits and that there are at least 5 columns in total.

Then print the columns in order where the columns for the name are the concatenation of the columns starting from the 3rd field until the second to last field.

awk '
/^[0-9]{4}[[:space:]+][0-9]{4}/ && NF > 4  {
  for(i=3;i<NF-1;i++) {s = s $i OFS}
  print $1, $2, $(NF-1), $NF, s
  s = ""
}
' file

Output

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 autotestbit Maintainer Walker Tom JR 
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
$ awk '
      {
        o=$1" "$2" "$(NF-1)" "$NF; 
        $1=$2=""; NF-=2; 
        print o,gensub(/^ */,"",1) 
}' file

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 autotestbit Maintainer Walker Tom JR  
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  

awk '
    NR==FNR { 
       n=$0; 
       $1=$2=""; NF-=2;
       a[n]=gensub(/^ */,"",1,$0); 
       next
    }
    {
       print $1,$2,$(NF-1),$NF,a[$0] 
}' file file

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 autotestbit Maintainer Walker Tom JR  
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  
]# sed -nr 's/(\S+)\s(\S+)\s(.*?)\s(\S+\s\S+)/\1 \2 \4 \3/p' a 
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 autotestbit Maintainer Walker Tom JR 
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 
{m,g}awk '(_=$NF)sub("$"," "$--NF" "_,$(_+=_^=_<_))^!--NF'

=

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 autotestbit Maintainer Walker Tom JR
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
Related