print line if 2nd field exists in text | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

print line if 2nd field exists in text

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-25-2009
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
print line if 2nd field exists in text

2 files, first one has 3 fields seperated by ||| and 2nd one is plain text.

I want to copy the lines from the first file if the 2nd field is present anywhere in the text file. This is what I've tried, but I'm new to awk and shell scripting in general so it's kinda broken.


Code:
#!/bin/awk -f
BEGIN 
{
   FS=" ||| ";
}

FILENAME=="./input/text" 
{
   # for all lines
   for (i; i < NR; i++) {
   # for all records on the current line
      for (j, j < NF; j++) {
         # if the 2nd field is seen in the text, increase its count
         if ($2 == $j) {
            seen[$2]++;
         }
      }
   }
}


# If the 2nd expression was seen at least once, print its corresponding line
{
   if ( seen[$2] > 0 ) {
      print $1 " \|\|\| " $2 " \|\|\| " $3;
   }
}

Sponsored Links
    #2  
Old 05-26-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 November 2014, 8:24 AM EST
Location: NM
Posts: 10,283
Thanks: 288
Thanked 815 Times in 757 Posts
Please post a few lines from each file.
Sponsored Links
    #3  
Old 05-26-2009
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
1st file
1 ||| abc kek ||| 2
1 ||| def foo ||| 3
2 ||| ghi bleh blah ||| 4
3 ||| jkl ||| 5

2nd file (text)
bla bla bla jkl bla bla bla
def foo test 123. abc kek

Expected output
1 ||| abc kek ||| 2
1 ||| def foo ||| 3
3 ||| jkl ||| 5
    #4  
Old 05-26-2009
funksen funksen is offline Forum Advisor  
Registered User
 
Join Date: Nov 2006
Last Activity: 30 April 2014, 10:11 AM EDT
Location: Austria/Vienna
Posts: 675
Thanks: 21
Thanked 31 Times in 30 Posts
sick command, but it seems to work


Code:
sed 's/ ||| /|/g' file1 | while read line ; do grep -qw "$(echo $line | awk -F "|" '{print $2}')" file2 && echo $line ; done| sed 's/|/ ||| /g'

Sponsored Links
    #5  
Old 05-26-2009
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, it works perfect! I'm just not too sure if I understand how it operates, can you elaborate?
Sponsored Links
    #6  
Old 05-26-2009
funksen funksen is offline Forum Advisor  
Registered User
 
Join Date: Nov 2006
Last Activity: 30 April 2014, 10:11 AM EDT
Location: Austria/Vienna
Posts: 675
Thanks: 21
Thanked 31 Times in 30 Posts
the first sed command just merges " ||| " to "|", to make the awk-statement easier, last sed changes | to ||| again



Code:
$(echo $line | awk -F "|" '{print $2}')

this part stands for the part in the middle of your input file
abc kek
def foo
..
..

an easier way perhaps is, to write that output to a variable, and grep for that variable, it's just shorter


so you grep (-q for quite, so no output from grep) , line per line, the middle part of file1 out of file2, and if the return code of grep is 0, the string was found, and the whole line is printed out, that's


Code:
&& echo $line

and this line per line

cheers
Sponsored Links
    #7  
Old 05-27-2009
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
just awk will do. no need to involve that much piping of various commands that do similar things.

Code:
awk   'BEGIN{FS=" [|][|][|] ";s="";}
FNR==NR{
 for ( i=1;i<=NF;i++){
  s=s" "$i
 } 
 next
}match(s,$2 )' file2 file1

output

Code:
# ./test.sh
1 ||| abc kek ||| 2
1 ||| def foo ||| 3
3 ||| jkl ||| 5

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to cut a field of a line in a text file? bhaskar_m Shell Programming and Scripting 7 03-12-2009 10:08 AM
how to print field n of line m Deanne Shell Programming and Scripting 11 01-30-2009 03:41 AM
Print line if first Field matches a pattern Raynon Shell Programming and Scripting 2 01-08-2009 06:07 AM
Search text from a file and print text and one previous line too kamranjalal Shell Programming and Scripting 6 01-06-2009 03:27 AM
How to print empty line when the a field is changed yahyaaa Shell Programming and Scripting 9 06-17-2008 11:31 AM



All times are GMT -4. The time now is 06:57 PM.