LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-03-2013, 07:28 AM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
help on awk contain between two columns


file.txt
AD615 13J04084706 53H04094706 0-000-0000 HUWSAFRA-615-J:1-0-13-53
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6
AD417 08H11114504 20Z06044504 0-000-0000 IP417SULT-H:1-1-6-20
AD106 05P02123801 09Z02133801 0-000-0000 IP106ULYA-P:1-1-3-9


output should be
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6


take $1 as main data then serach contain on $5

$1 have
ANOZM
MDM1G
ABAH9


already contain on $5
HUWM219-00-209_ANOZM:1-0-15-1
HUWM301-00-411_MDM1G:1-0-9-27
MRGDNBKAA_ABAH9-A:1-1-15-6


awk '$1~/$5/ {print $0}' file.txt

but it does not show me what i need ?
anybody can help me
 
Old 04-03-2013, 08:06 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Try this ...
Code:
awk '$5~$1' $InFile >$OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 04-03-2013, 12:08 PM   #3
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks Daniel
 
Old 04-16-2013, 02:18 AM   #4
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
another question in same think
if I have two files need to make comparison between them based on contain text how i can do such as this

file 1

13J04084706 53H04094706 0-000-0000 HUWSAFRA-615-J:1-0-13-53
15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6
08H11114504 20Z06044504 0-000-0000 IP417SULT-H:1-1-6-20
05P02123801 09Z02133801 0-000-0000 IP106ULYA-P:1-1-3-9


file 2
ANOZM
MDM1G
ABAH9


output should be like this
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6

Last edited by phpshell; 04-16-2013 at 02:19 AM.
 
Old 04-16-2013, 02:44 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this:
Code:
#!/bin/bash

awk '
BEGIN { 
  while ( ( getline < "file2" ) > 0 )
    { _[$1] = $1 }
}
{
for ( item in _ )
     if ( $0 ~ _[item] ) { print _[item], $0}
}' file1
Example run with input shown in post #4:
Code:
 ./awk.phpshell.sh
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6
 
1 members found this post helpful.
Old 04-16-2013, 07:12 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by phpshell View Post
another question in same think
if I have two files need to make comparison between them based on contain text how i can do such as this

file 1

13J04084706 53H04094706 0-000-0000 HUWSAFRA-615-J:1-0-13-53
15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6
08H11114504 20Z06044504 0-000-0000 IP417SULT-H:1-1-6-20
05P02123801 09Z02133801 0-000-0000 IP106ULYA-P:1-1-3-9


file 2
ANOZM
MDM1G
ABAH9


output should be like this
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6
Try this ...
Code:
grep -Ff $InFile2 $InFile1 >$OutFile
Daniel B. Martin

Last edited by danielbmartin; 04-16-2013 at 07:36 AM. Reason: Minor code improvement
 
Old 04-16-2013, 07:39 AM   #7
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Have a look at this:
Code:
#!/bin/bash

awk '
BEGIN { 
  while ( ( getline < "file2" ) > 0 )
    { _[$1] = $1 }
}
{
for ( item in _ )
     if ( $0 ~ _[item] ) { print _[item], $0}
}' file1
Example run with input shown in post #4:
Code:
 ./awk.phpshell.sh
ANOZM 15A01011902 01H01011902 0-000-0000 HUWM219-00-209_ANOZM:1-0-15-1
MDM1G 09A01010812 27H01010812 0-000-0000 HUWM301-00-411_MDM1G:1-0-9-27
ABAH9 15A07010408 06S02010308 0-000-0000 MRGDNBKAA_ABAH9-A:1-1-15-6


thanks for your help ...
but this very difficult to trace the code for me .any other way


Dear Daniel
i try grep -f but it show me these messages

grep: Memory exhausted
grep: memory exhausted: Cannot allocate memory

Last edited by phpshell; 04-16-2013 at 07:42 AM.
 
Old 04-16-2013, 07:53 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by phpshell
thank you but this very difficult to trace the code for me
Here's an explanation of the code:
Code:
awk '
BEGIN { 
  while ( ( getline < "file2" ) > 0 )
    { _[$1] = $1 }
}
{
for ( item in _ )
     if ( $0 ~ _[item] ) { print _[item], $0}
}' file1
The blue part is done once when awk starts. It puts the entries found in file2 into an array (array name is _).
Once file2 is processed awk will start reading file1, one line at the time.

Each line from file1 is checked against the array entries, if it matches it prints both the content of the array and the line from file1

Quote:
Originally Posted by phpshell
any other way
danielbmartin's edited solution works on my side.

But I'm only testing with the examples given by you. Looking at the error you posted I can only assume that your real files are much bigger and may cause a memory problem. But that's an assumption on my side, we won't know for sure until you give some extra info.
 
1 members found this post helpful.
Old 04-16-2013, 09:40 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by phpshell View Post
Dear Daniel
i try grep -f but it show me these messages

grep: Memory exhausted
grep: memory exhausted: Cannot allocate memory
In your small example files every match is preceded by an underscore and followed by a colon. Will this always be true?

Daniel B. Martin
 
Old 04-16-2013, 10:42 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You can even make the 'if' a little simpler if you check against the index:
Code:
awk 'FNR==NR{_[$0];next}{for(i in _)if($NF ~ i)print i,$0}' file2 file1
 
1 members found this post helpful.
Old 04-17-2013, 12:33 AM   #11
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
I am really appreciated your support all of you guys


Marked this thread as solved
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
creating columns with awk? verse123 Linux - Newbie 6 11-09-2011 01:50 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
extracting columns with awk gav251 Programming 7 03-10-2011 08:40 AM
printing multiple columns with awk kdelover Programming 16 12-16-2009 09:10 AM
Using awk to switch columns bioinformatics_guy Linux - Newbie 3 10-30-2008 09:50 AM

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

All times are GMT -5. The time now is 08:30 AM.

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