Monday, April 4, 2022

[SOLVED] print specific value from 7th column using pattern matching along with first 6 columns

Issue

file1

1 123 ab456 A G PASS AC=0.15;FB=1.5;BV=45; 0|0  0|0  0|1  0|0  
4 789 ab123 C T PASS FB=90;AC=2.15;BV=12; 0|1 0|1 0|0 0|0  

desired output

1 123 ab456 A G PASS AC=0.15  
4 789 ab123 C T PASS AC=2.15  

I used

awk '{print $1,$2,$3,$4,$5,$6,$7}' file1 > out1.txt
sed -i 's/;/\t/g' out1.txt
awk '{print $1,$2,$3,$4,$5,$6,$7,$8}' out1.txt

output generated

1 123 ab456 A G PASS AC=0.15  
4 789 ab123 C T PASS FB=90  

I want to print first 6 columns along with value of AC=(*) from 7th column.


Solution

With your shown samples, please try following awk code.

awk '
{
  val=""
  while(match($7,/AC=[^;]*/)){
    val=(val?val:"")substr($7,RSTART,RLENGTH)
    $7=substr($7,RSTART+RLENGTH)
  }
  print $1,$2,$3,$4,$5,$6,val
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                             ##Starting awk program from here.
{
  val=""                          ##Nullifying val here.
  while(match($7,/AC=[^;]*/)){    ##Running while loop to use match function to match AC= till semi colon all occurrences here.
    val=(val?val:"")substr($7,RSTART,RLENGTH)  ##Creating val and keep adding matched regex value to it, from 7th column.
    $7=substr($7,RSTART+RLENGTH)  ##Assigning rest pending values to 7th column itself.
  }
  print $1,$2,$3,$4,$5,$6,val     ##Printing appropriate columns required by OP along with val here.
}
' Input_file                      ##Mentioning Input_file name here.


Answered By - RavinderSingh13
Answer Checked By - Senaida (WPSolving Volunteer)