LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Concatenate column 1 and column 2 of related lines (https://www.linuxquestions.org/questions/programming-9/concatenate-column-1-and-column-2-of-related-lines-684489/)

cgcamal 11-19-2008 12:27 AM

Concatenate column 1 and column 2 of related lines
 
Hi everyone,

I have a file containing many lines, some of them begin with 74521 in column 1, others have the word "ATRB" in column 5, an the rest other info that doesnt care to me.

I´ve filtered only lines containing what I´ve explained above using

Code:

awk '/74521/||/ATRB/' source.txt > output.txt
The filtering result is

Code:

74521019143      515097017017121  COMPLETE      AVAILABLE
74521019149      515097017017161  COMPLETE      AVAILABLE
74521956785      515097011027927  COMPLETE      AVAILABLE
    2                            1    NO    X-1      ATRB    1
    3                            5    NO    X-1      ATRB    2
    1                            3    NO    X-1      ATRB    3
74521956787      515097011027929  COMPLETE      AVAILABLE
    2                            1    NO    X-1      ATRB    1
    3                            2    NO    X-1      ATRB    2
    1                            1    NO    X-1      ATRB    3
74521900844      515097014501216  COMPLETE      AVAILABLE
    2                            0    NO            ATRB    1
    1                            0    NO            ATRB    2
    3                            0    NO            ATRB    3
    5                            1    NO            ATRB    4

Every line containing "ATRB" after one particular line that begin with 74521
is part of that line. 2 consecutive lines that begin with "75421", may dont have
lines containing the word "ATRB" between them, in this case that lines
are not interesting either.


Based on this info and the filtered file I´ve got at the moment I´like to obtain
a last arrangement using colum 1 & column 2 of lines that begin with 74521 and the
correspondent column 1 & column 2 of lines that contain "ATRB". This four columns
in the same line as follow


Code:

74521956785 515097011027927 2 1 3 1 5 3
74521956787 515097011027929 2 1 3 1 2 1
74521900844 515097014501216 2 1 3 5 0 0 0 1

Maybe somebody knows or can help me with this, some programming advice using awk.

Many thanks in advance

burschik 11-19-2008 03:23 AM

Use an associate array to store all values related to a particular key. Then print out the array.

radoulov 11-19-2008 05:24 AM

Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:

awk 'END {
  print k, f1, f2
  }
/^7/ {
  if (f1) print k, f1, f2
  f1 = f2 = ""
  k = $1 FS $2
  }
/ATRB/ {
  f1 = (f1 _) ? f1 FS $1 : $1
  f2 = (f2 _) ? f2 FS $2 : $2
  }' infile


cgcamal 11-19-2008 11:45 PM

Hey radoulov,

It works very nice, really thanks for your help.

Now, I only trying to include analysis of some other cases that could happen, I hope be succesful in that thask, but with the code you´ve gave me it´s almost 99% of what I needed.

Thanks again.

schneidz 11-20-2008 10:43 AM

would a combination of grep , cut and paste work ?


All times are GMT -5. The time now is 01:00 AM.