Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new to awk programming. I have a question on manipulating text file, which is required to draw certain network based images in a visualization software (Circos http://circos.ca)

I have input data for which I want to manipulate values using awk/grep/sed. There are 9 pairs (18 lines). 5 pairs (first 10 lines) are for "from=ABCB11", and 4 pairs (next 8 lines) are for "from =ABCC8". What I want is extract the value from the first line of the first pair and replace it in each alternate line of the rest of the other pairs. So value for group-2 is 9 10, which should replace all the occurrence of value in group2. The next value for group-2 is 28 29, which should be replaced by 9 10.

The stop should be determined by "from=name" which is "from=ABCB11". It is not necessary that the rows that have to captured expression from and replace in its next occurrence will belong to group-2 as in this instance. It could be group-3 or group-4 until group-10. So second set ("from =ABCC8") could have been belonged to group-4/5/6 not necessary group-2. It is just a coincidence here, but all group-num's will be same for each "from=" alternate line.

group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 28 29 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-5 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-2 29 30 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-5 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-2 10 11 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-3 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-2 11 12 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-3 2 3 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 12 13 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-1 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-2 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-1 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-2 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
group-1 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1

Below is the FINAL output,I am looking for:

group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-5 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-5 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-3 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-3 2 3 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 12 13 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-1 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-1 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
group-1 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1

Also,this is just a sample data. So many pairs would have group-1,group-4,group-5 up to group 10. Here, only pairs from lesser groups are mentioned.

Basically, these are the modifications that should be seen while printing complete lines, until "from=ABCB11" ends, in each alternate line. Similarly for "from=ABCC8".

group-2 9 10
group-3 0 1
group-2 9 10
group-5 0 1
group-2 9 10
.........
.........
.........

Here is what I tried:

I want to loop through the lines until the value in "from=name" remains same,so that I can change all occurrences in each alternate line.Code:

awk -F, 'NR%2==1 {split($2,a,"="); print a[2]}' file.txt

The above code is able to extract the alternate lines and the "name" in "from=name".

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Try something like:

awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file

This tests if $2 has changed and substitutes $1 on every odd line with the same value for every $2


Added an explanation..

awk '
  NR%2 {            # On every line where NR (recordnr) mod 2 equals 1 (odd line)
    if(p!=$2) {     # If the variable p (previous) is different from the 2nd field then
      v=$1          # The new value v should become $1 (1st field)
      p=$2          # The new variable p becomes the current field 2
    }               #
    $1=v            # On every odd line field 1 becomes the previously set value
  }
  1                 # 1 means true. The default action is to print the record (line)
' FS=, OFS=, file   # Set the input and output field separators to a comma.
share|improve this answer
    
the command works awesome!! can you please explain the code if possible..what are variables :p,v and purpose of 1 at the end.Is it for checking the odd lines? –  Ron May 1 at 14:29
1  
@Ron . Good to hear :) I added an explanation, hope it helps.. –  Scrutinizer May 1 at 16:51
    
Quick question: which line specifies that "from=name" has changed,and it should not change the first alternate line of that,instead store it to compare with the alternate lines of that particular "from=name". –  Ron May 1 at 19:23
    
@Ron . from=name is the second field so that would be if(p!=$2) { it changes the first field or all grouped alternate lines with that particular from=name –  Scrutinizer May 1 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.