Add columns by merging rows in a text file using bash -


i have tab delimited text file 222 rows , 7752 columns. here first 4 columns , 5 lines example:

individual var1 var2 var3 persona          persona    t    t    t personb    g    g    g personb    c    c    c 

i need merge 2 rows representing each person one. is, need move value first column in second line each person, second column on first line each person, repeat across rows , columns. end 1 line per person (111 rows) , double columns (15,504). first 7 columns , 3 lines therefore this:

individual var1 var1 var2 var2 var3 var3 persona       t       t       t personb    g    c    g    c    g    c 

i appreciative if suggest solution, perhaps using bash? have not yet attempted solution quite new coding.

regardless language use, logic same. while bash isn't speediest type of thing, work fine. need handle ordering reads of header , line1 , line2 each person , validate have same person , number of fields match , interleave lines single line.

you following:

#!/bin/bash  test -r "$1" || {   ## validate first argument readable file     printf "error: file not given or not readable.\n"     exit 1 }  while read -ra line;             ## read each line array     if test -z "$headings" ;   ## if headign not set, set         headings=1                  ## set heading used flag         printf "%s" "${line[0]}"    ## print individual         ## duplicate each heading tab separated         ((i = 1; < ${#line[@]}; i++));             printf "\t%s\t%s" "${line[i]}" "${line[i]}"         done         echo "" ## tidy newline     elif test -z "$line1" ;    ## if line1 not set, set         line1=( "${line[@]}" )      ## save line line1 , next line     else         ## test persons match         if test "${line1[0]}" = "${line[0]}" ;             ## test number of fields in each line match             if test "${#line1[@]}" != "${#line[@]}" ;                 printf "error: field mismatch - person '%s'.\n" "${line[0]}" >&2             fi             printf "%s\t" "${line[0]}"    ## print person (w/tab)             ## interleave data person line1 line2 tab separated             ((i = 1; < ${#line[@]}; i++));                 printf "\t%s\t%s" "${line1[i]}" "${line[i]}"             done             echo "" ## tidy newline             line1=  ## unset line1 , repeat         fi     fi done < "$1" 

note: additional \t after printing personx spacing. should check length of person , adjust required. can adjust spacing use tabs or spaces desired.

example input

$ cat data.txt individual var1 var2 var3 persona          persona    t    t    t personb    g    g    g personb    c    c    c 

example use/output

$ bash cmblines.sh data.txt individual      var1    var1    var2    var2    var3    var3 persona               t             t             t personb         g       c       g       c       g       c 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -