append the column in another text file

Viewed 274

Hii i have a text file having 3 column

2.0  44.8  789.3 
3.0  58.4  453.0
4.0  97.2 -489.1
5.2  35.3  458.6

i want to select the columns from the above text file and want to append the selected column in another text file .The file where i want to append the above columns are given below

> > > > 
    10.0    8.5
    20.0    8.5
    30.0    8.5
    40.0    8.5
> > > >
    10.0    8.0
    20.0    8.0
    30.0    8.0
    40.0    8.0
> > > >
    10.0    9.0
    20.0    9.0
    30.0    9.0
    40.0    9.0
> > > >

and my expected output is

> > > > 
    10.0    8.5   2.0
    20.0    8.5   3.0
    30.0    8.5   4.0
    40.0    8.5   5.2
> > > >
    10.0    8.0   44.8
    20.0    8.0   58.4
    30.0    8.0   97.2
    40.0    8.0   35.3
> > > >
    10.0    9.0   789.3
    20.0    9.0   453.0
    30.0    9.0  -489.1
    40.0    9.0   458.6
> > > >

i tried the script but after that not getting more idea, i need experts help.Thanks in advance.

 #!/bin/sh
 for file in inp.txt
 do
 awk '{print $1}' > colone
 done
2 Answers

Based on OP's shown samples, could you please try following. This will print reset value of count after 3rd occurrence of > > in text file and again starts printing from 1st column values onwards from Input_file.

awk '
FNR==NR{
  for(i=1;i<=NF;i++){
    value[FNR,i]=$i
  }
  next
}
/^> >/{
  count=0
  print
  if(col==3){ col=0 }
  col++
  next
}
{
  print $0"    "value[++count,col]
}
' Input_file text_file

Explanation: Adding detailed explanation for above.

awk '                       ##Starting awk program from here.
FNR==NR{                    ##Checking condition FNR==NR when Input_file is being read.
  for(i=1;i<=NF;i++){       ##Traversing through all fields here. 
    value[FNR,i]=$i         ##Creating value with index of FNR,i here with value $i
  }
  next                      ##next will skip all further statments from here.
}
/^> >/{                     ##Checking condition if line starts from > > then do following.
  count=0                   ##Nulliffying count here.
  print                     ##Printing current line.
  if(col==3){ col=0 }       ##Checking if col is 3 then make its value 0 here. Why because OP sample has only 3 blocks and
                            ##if there are more than 3 then it will start printing from very 1st values onwards after every 3 blocks.
  col++                     ##Increasing value of col with 1 here.
  next                      ##next will skip all further statments from here.
}
{
  print $0"    "value[++count,col]  ##Printing current line with space and array value here.
}
' Input_file text_file      ##Mentioning Input_file and text_file names here.
$ cat tst.awk
NR==FNR {
    for (numBlocks=1; numBlocks<=NF; numBlocks++) {
        vals[numBlocks,NR] = $numBlocks
    }
    next
}
/^>/ {
    blockNr++
    rowNr = 0
    print
    next
}
{ printf "%s %7s\n", $0, vals[blockNr,++rowNr] }

$ awk -f tst.awk file1 file2
> > > >
    10.0    8.5     2.0
    20.0    8.5     3.0
    30.0    8.5     4.0
    40.0    8.5     5.2
> > > >
    10.0    8.0    44.8
    20.0    8.0    58.4
    30.0    8.0    97.2
    40.0    8.0    35.3
> > > >
    10.0    9.0   789.3
    20.0    9.0   453.0
    30.0    9.0  -489.1
    40.0    9.0   458.6
> > > >
Related