Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert pattern from current line in next line

Tags:

awk

I have this file :

>AX-899-Af-889-[A/G]
GTCCATTCAGGTAAAAAAAAAAAACATAACAATTGAAATTGCATGA
>AX-899-Af-889-[A/G]
GCAAACTATTTTCATGAATGAACTTCAGTTGATTGTGAGATG
>AX-899-Af-889-[G/T]
AAGGTAGAATGACACCATTAAACAGTAGGGAATTGGTCACAGAACTCT

I need to insert the pattern [X/X] present in the lines starting by > in the next line at the 10th position and replace this 10th character :

>AX-899-Af-889-[A/G]
GTCCATTCA[A/G]GTAAAAAAAAAAAACATAACAATTGAAATTGCATGA
>AX-899-Af-889-[A/G]
GCAAACTAT[A/G]TTCATGAATGAACTTCAGTTGATTGTGAGATG
>AX-899-Af-889-[G/T]
AAGGTAGAA[G/T]GACACCATTAAACAGTAGGGAATTGGTCACAGAACTCT

I can extract the pattern :

awk  'match($0, /^>/) {split($0,a,"-");  print; getline; print a[5]}1' file 

Also replace the 10th character by a pattern ("N" for example) : sed 's/^\([ATCG].\{8\}\)[ATCG]/\1N/' file

like image 565
Paillou Avatar asked Aug 07 '26 06:08

Paillou


1 Answers

With your shown samples, please try following awk.

awk '
BEGIN{ FS=OFS="-" }
/^>/ {
  val=$NF
  print
  next
}
{
  print substr($0,1,9) val substr($0,11)
  val=""
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                      ##Starting awk program from here.
BEGIN{ FS=OFS="-" }        ##Starting BEGIN section from here and setting FS and OFS as - here.
/^>/ {                     ##Checking condition if line starts from > then do following.
  val=$NF                  ##Setting last field($NF) to val here.
  print                    ##printing current line here.
  next                     ##next will skip all further statements from here.
}
{
  print substr($0,1,9) val substr($0,11)  ##printing substring from 1st to 9 chars of current line.
                           ##Followed by val and rest of values from 11th char to till last of current line.
  val=""                   ##Nullifying val here.
}
'  Input_file              ##Mentioning Input_file name here. 
like image 95
RavinderSingh13 Avatar answered Aug 12 '26 21:08

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!