To find similar items in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To find similar items in a column
# 1  
Old 04-23-2013
To find similar items in a column

HI,
I have a long file which looks like
Code:
 "1xxx_0_1"	"1xxx"	500	5	"ABC*3-DEF*3-LL"
 "2yyy_0_1"	"2yyy"	600	10	"ABC*2-DEF*2-LL"
 "3ddd_0_1"	"3ddd"	150	52	"ABC*3-DEF*3-LL"
 "1xxx_0_1"	"1xxx"	500	5	"ABC*3-DEF*3-LL"
 "2yyy_0_1"	"2yyy"	600	10	"ABC*2-DEF*2-LL"
 "3ddd_0_1"	"3ddd"	150	52	"ABC*3-DEF*3-LL"

Now i would like to print first column and last column which contains "ABC*2-DEF*2-LL"
from the long list. which should looks like
Code:
 2yyy_0_1     ABC*2-DEF*2-LL
 2FFF_0_1    ABC*2-DEF*2-LL
 2ZZZ_0_1    ABC*2-DEF*2-LL

Thank you in advance.

Regards,

Last edited by joeyg; 04-23-2013 at 08:59 AM.. Reason: Please wrap data and commands in CodeTags
# 2  
Old 04-23-2013
Code:
$ cat long
"1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL"
"2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL"
"3ddd_0_1" "3ddd" 150 52 "ABC*3-DEF*3-LL"
"1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL"
"2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL"
"3ddd_0_1" "3ddd" 150 52 "ABC*3-DEF*3-LL"

Code:
$ awk '$5 ~ /"ABC\*2-DEF\*2-LL"/ { print $1, $5 }' long | tr -d '"'
2yyy_0_1 ABC*2-DEF*2-LL
2yyy_0_1 ABC*2-DEF*2-LL

Or for tabbed output:
Code:
$ awk '$5 ~ /"ABC\*2-DEF\*2-LL"/ { print $1, $5 }' OFS='\t' long | tr -d '"'
2yyy_0_1        ABC*2-DEF*2-LL
2yyy_0_1        ABC*2-DEF*2-LL

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-23-2013
So if the 5th column contains "ABC*2-DEF*2-LL"
then print the 1st and 5th columns?

Your example returned values not in your sample file.
# 4  
Old 04-23-2013
Use gsub function instead of piping output to tr to remove all double quotes:
Code:
awk '$NF ~ /ABC\*2-DEF\*2-LL/{gsub(/"/,X); print $1, $NF} ' OFS='\t' file

This User Gave Thanks to Yoda For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. Shell Programming and Scripting

Find odd numbered items from a csv

Hi, This question might seem bit confusing. Sorry for that. I have tried to express myself as clear as possible. I have a text file in following format: abc_456_def,abc_457_def,123_458_def,abc_3_459_def,zbc_123_456_def ybc_123_457_def,xbc_3_458def,ybc_124_457_def I want to check the... (3 Replies)
Discussion started by: ctrld
3 Replies

3. Shell Programming and Scripting

Add all 4 column entries for similar column ids

Hi, I want to write a script which will add the entries in all columns for the same column id. I can do it in excel, but I need to do this for 384 columns which will come down to 96 (384/4). How can I do this iteratively A A A A B B B B C C C C 1 0 1 0 2 1 4 5 3 4 5 6 2 0 0 2 3 5 70 100 1... (7 Replies)
Discussion started by: Diya123
7 Replies

4. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

5. UNIX for Advanced & Expert Users

find/grep two items and display both

I have a file against which I can grep a string for. I can also check for that string count using wc -l (or grep -c). I need to display the results of both in one output i.e. 'line containing string' and 'count' - what would be the most efficient way of managing this? Thanks in advance. (3 Replies)
Discussion started by: haider1
3 Replies

6. Shell Programming and Scripting

Merge two files with similar column entries

Hi , I have few files which contains user name and data transfer rate in MBs and this data is collected for year and for each month report is saved in 12 different files I have to merge all the files to prepare the final report Files are as below Similarly I have 10 more files ... (5 Replies)
Discussion started by: pratapsingh
5 Replies

7. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

8. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies
Login or Register to Ask a Question