Get value from column subsection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get value from column subsection
# 1  
Old 07-24-2013
Get value from column subsection

Please help me with this, I want to collect unique tbc values present in the
2nd col when the 1st col says 'collect' ..Order does not matter.. 2nd col is sub sectioned by ; and tb code values are in the form tbc<val1,val2



Code:
File1


leave	kbl;tbc<A,B;alt<-23
leave	kbl;tbc<B,C,D
collect	kbl;tbc<A,B,C;alt<-34
collect	kbl;tbc<D,B,C,A
leave	lbl;tbc<A;J
leave	lbl;tbc<B;C
leave	kbl;tbc<A;B
leave	tbc<B;C;D

File2

leave	kbl;tbc<A,B;alt<-23
leave	kbl;tbc<B,C,D
collect	kbl;tbc<A;alt<-34
collect	kbl;tbc<C,A
collect	kbl;tbc<B,A
leave	lbl;tbc<A;J
leave	lbl;tbc<B;C
leave	kbl;tbc<A;B
leave	tbc<B;C;D

Output

File1	A	B	C	D
File2	A	B	C

Here is what I tried which doesnt work


Code:
awk ' 
if ($1=='collect')  
gsub(/*tbc*</,//);
gsub(/*;*/,"");
print FILENAME $2'


Last edited by newbie83; 07-24-2013 at 02:56 AM..
# 2  
Old 07-24-2013
Hi, I just corrected a number of things in what you were trying to do, to give you an idea:
Code:
awk ' 
  $1=="collect" {  
     sub(/.*tbc\</,"");
     sub(/;.*/,"");
     print FILENAME, $0
  }
'  file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-24-2013
Might need some performance tuning, but works.

Code:
awk -F";" '/collect/ {sub(/tbc</,"");print $2}' File1 | paste -sd , - | awk -F, '{for(i=1;i<=NF;i++) print $i}' |sort -u | tr "\n" "\t"

This User Gave Thanks to krishmaths For This Post:
# 4  
Old 07-24-2013
thank you for helping me learn, I will ask again if I have issues..

---------- Post updated at 01:38 AM ---------- Previous update was at 01:29 AM ----------

krishmaths, your code uses just 2 files..i have several thousands..can this be extended?
Scrutinizer .. I cant get to multiple collect statements , it just takes the first and exits..should I try some other loop structure?
# 5  
Old 07-24-2013
@ newbie83: I get:
Code:
$ awk ' 
  $1=="collect" {  
     sub(/.*tbc\</,"");
     sub(/;.*/,"");
     print FILENAME, $0
  } ' file1 file2
file1 A,B,C
file1 D,B,C,A
file2 A
file2 C,A
file2 B,A

What do you get?

-- edit --
You can also try something like this and take it from there:
Code:
awk -F '.*tbc<|,' '{sub(/;.*/,x, $NF); for (i=2; i<NF; i++) print FILENAME, $i }' file1 file2 | sort -u

Output:
Code:
file1 A
file1 B
file1 C
file1 D
file2 A
file2 B
file2 C


Last edited by Scrutinizer; 07-24-2013 at 02:22 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-24-2013
Scrutinizer I get the same..got the collect within '' previously..sorry about that

Now for the second step I can do this for getting same rows together..but values will repeat...how do I get them uniquely?

Code:
awk '{ x[$1]=x[$1] "," $2 } 
END { 
   for (k in x) print k,x[k];
}'

---------- Post updated at 02:09 AM ---------- Previous update was at 02:01 AM ----------

thanks Scrutinizer..I just saw your last edit...let me try..
# 7  
Old 07-26-2013
Please help

the code is not appropriately work for the second awk, first example runs just fine...Also I need to condition on the term "collect" win col1 which is missing from the code..Since I added D to fil3.txt.txt, the output should be
Code:
fil1.txt.txt A
fil1.txt.txt B
fil1.txt.txt C
fil1.txt.txt D
fil3.txt.txt A
fil3.txt.txt B
fil3.txt.txt C
fil3.txt.txt D

Code:
$ cat fil1.txt.txt
leave   kbl;tbc<A,B;alt<-23
leave   kbl;tbc<B,C,D
collect kbl;tbc<A,B,C;alt<-34
collect kbl;tbc<D,B,C,A
leave   lbl;tbc<A;J
leave   lbl;tbc<B;C
leave   kbl;tbc<A;B
leave   tbc<B;C;D


$ cat fil2.txt.txt
leave   kbl;tbc<A,B;alt<-23
leave   kbl;tbc<B,C,D
collect kbl;tbc<A;alt<-34
collect kbl;tbc<C,A
collect kbl;tbc<B,A
leave   lbl;tbc<A;J
leave   lbl;tbc<B;C
leave   kbl;tbc<A;B
leave   tbc<B;C;D



$ cat fil3.txt.txt
leave   kbl;tbc<A,B;alt<-23
leave   kbl;tbc<B,C,D
collect kbl;tbc<A,D;alt<-34
collect kbl;tbc<C,A
collect kbl;tbc<B,A
leave   lbl;tbc<A;J
leave   lbl;tbc<B;C
leave   kbl;tbc<A;B
leave   tbc<B;C;D

$ awk -F '.*tbc<|,' '{sub(/;.*/,x, $NF); for (i=2; i<NF; i++) print FILENAME, $i }' fil1.txt.txt fil2.txt.txt | sort -u
fil1.txt.txt A
fil1.txt.txt B
fil1.txt.txt C
fil1.txt.txt D
fil2.txt.txt A
fil2.txt.txt B
fil2.txt.txt C

awk -F '.*tbc<|,' '{sub(/;.*/,x, $NF); for (i=2; i<NF; i++) print FILENAME, $i }' fil1.txt.txt fil3.txt.txt | sort -u
fil1.txt.txt A
fil1.txt.txt B
fil1.txt.txt C
fil1.txt.txt D
fil3.txt.txt A
fil3.txt.txt B
fil3.txt.txt C

---------- Post updated 07-26-13 at 01:17 AM ---------- Previous update was 07-25-13 at 02:54 PM ----------

Code:
$ awk -F '.*tbc<|,' '{sub(/;.*/,x, $NF); for (i=2; i<NF; i++) print FILENAME, $i }' fil1.txt.txt fil2.txt.txt | sort -u


I`m failing to find whats wrong with this ...any comments on why this is not working?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

5. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. UNIX for Advanced & Expert Users

file subsection

Is there any way I can create a virtual file that is a subsection of another file? Similar to the way partitions are subsections of the disk device, but configurable and with a normal file source. (2 Replies)
Discussion started by: Corona688
2 Replies
Login or Register to Ask a Question