Need help with a script to grep items in one file from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with a script to grep items in one file from another file
# 1  
Old 04-03-2013
Need help with a script to grep items in one file from another file

I have one master file "File1" with all such info in it. I need to grep each object under each list from another file "File2". Can anyone help me with a script for this.

Code:
File 1
------
List 1
     Object 1
     Object 2
List 2
     Object 3 
     Object 1
List 3
     Object 2
     Object 5

Code:
File2
-----
Object 1
Object 3
Object 4

I need to sort out info with the following result
Code:
List 1 has 1 objects from File2
List 2 has 2 objects from File2
List 3 has 0 objects from File2


Thanks in advance
Sam
# 2  
Old 04-03-2013
Sounds like homework.
So where are you stuck?

--ahamed
# 3  
Old 04-04-2013
It's for work actually. Trying to automate few things to make life easier based on the information received from the network switches. Those two files is an example of how the data would be. I am a novice at best in shell programming and wanted to see if there is a simple way this can be done through awk/sed?
# 4  
Old 04-04-2013
Here is an awk program that might work for the posted sample input:
Code:
awk ' BEGIN {
        F = "file1"
        while ( (getline line < F) > 0 )
        {
                gsub(/^[ \t]*|[ \t]*$/, x, line)
                if ( line ~ /List/ )
                        L = line
                if ( line !~ /List/ )
                        A[L] = ( A[L] == "" ? line : A[L] "," line )
        }
        close (F)

        F = "file2"
        while ( (getline line < F) > 0 )
        {
                gsub(/^[ \t]*|[ \t]*$/, x, line)
                for ( k in A )
                {
                        n = split ( A[k], R, ",")
                        for ( j = 1; j <=n; j++ )
                        {
                                if ( line == R[j] )
                                        C[k]++
                        }
                }
        }
        close(F)

        for ( k in A )
                print k " has " (C[k] == "" ? 0 : C[k]) " objects from file2"
} '

Note: Replace file1 & file2 with actual input file names. Use nawk instead if you are on SunOS or Solaris.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-04-2013
Is this how the actual data looks like? with space and tab and all? Cause it will matter while coding. A real data sample would help us better.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 04-04-2013
Here is another one with awk:
Code:
awk '
NR==FNR {obj[$1$2]=1; fn=FILENAME; next}
/^List/ {
if(s!="") printf "%s has %d objects from %s\n",s,cnt,fn
s=$0; cnt=0
next
}
$1$2 in obj {cnt++}
END {
if(s!="") printf "%s has %d objects from %s\n",s,cnt,fn
}' File2 File1

Solaris needs /usr/xpg4/bin/awk or nawk.

Last edited by MadeInGermany; 04-04-2013 at 04:25 PM.. Reason: removed some rubbish
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to process a list of items and uncomment lines with that item in a second file

Hello, I have a src code file where I need to uncomment many lines. The lines I need to uncomment look like, C CALL l_r(DESNAME,DESOUT, 'Gmax', ESH(10), NO_APP, JJ) The comment is the "C" in the first column. This needs to be deleted so that there are 6 spaces preceding "CALL".... (7 Replies)
Discussion started by: LMHmedchem
7 Replies

2. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

3. Shell Programming and Scripting

sed to delete items in an array from a file

I need to create a shell script to delete multiple items (Strings) at a time from a file. I need to iterate through a list of strings. My plan is to create an array and then iterate through the array. My code is not working #!/bin/bash -x declare -a array=(one, two, three, four)... (5 Replies)
Discussion started by: bash_in_my_head
5 Replies

4. UNIX for Dummies Questions & Answers

Grep - Searching for multiple items using one command

I am performing a regular check on UNIX servers which involves logging onto UNIX servers and using the grep command to check if a GID exists in the /etc/group directory e.g. grep 12345 /etc/group I have five to check on each server, is there anyway I can incorporate them into one command and... (2 Replies)
Discussion started by: @MeDaveT
2 Replies

5. Shell Programming and Scripting

Shell Script to grep Job File name and Log File name from crontab -l

Hello, I am new to shell scripting. I need to write a shell script where i can grep the name of file ie. .sh file and log file from crontab -l. #51 18 * * * /home/oracle/refresh/refresh_ug634.sh > /home/oracle/refresh/refresh_ug634.sh.log 2>&1 #40 17 * * * /home/oracle/refresh/refresh_ux634.sh... (1 Reply)
Discussion started by: guptra
1 Replies

6. 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

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

finding missing items in file

I have a need to compare 2 files, then print results to file. Need to find items from file2 that are not found in file 1. thanks in advance! example: file 1: abcde=12 fffff=6 bbbb=35 file2: abcde=12 fffff=6 bbbb=35 ccccc=10 kkkkk=45 (8 Replies)
Discussion started by: discostu
8 Replies

9. Shell Programming and Scripting

Deleting double items in file

Hi, i need a script, which deletes doulbe items in a file. My file looks like: - - - xxx xxx G123 G234 G234 G234 o o ... First i want to sort the file an then i want to delete double items. Can anyone help me. I work under solaris10. (3 Replies)
Discussion started by: free2k
3 Replies

10. Shell Programming and Scripting

removing items from a file with batch

Please assist with awk scirpts: I need to remove items from a file in a batch: The file that I will remove from has the following format: abc00tef:10.81.12.3 abc01tef:10.81.12.3 abc02tef:10.81.12.3 abc03tef:10.81.12.3 abc04tef:10.81.12.3 abc05tef:10.81.12.3 I have a file which... (5 Replies)
Discussion started by: amir07
5 Replies
Login or Register to Ask a Question