LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2008, 12:27 AM   #1
cgcamal
Member
 
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78

Rep: Reputation: 16
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
 
Old 11-19-2008, 03:23 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Use an associate array to store all values related to a particular key. Then print out the array.
 
Old 11-19-2008, 05:24 AM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
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
 
Old 11-19-2008, 11:45 PM   #4
cgcamal
Member
 
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78

Original Poster
Rep: Reputation: 16
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.
 
Old 11-20-2008, 10:43 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would a combination of grep , cut and paste work ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Transpose info between blocks from column to lines arragement cgcamal Programming 1 11-16-2008 08:48 PM
cut column ust Linux - Newbie 10 01-09-2008 03:28 AM
Column into rows bharatbsharma Programming 1 10-25-2007 02:23 AM
ls -> Column Order daletaylor Linux - Newbie 3 06-22-2007 10:59 AM
add new column Eddie9 Linux - General 2 04-09-2002 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration