script to fetch from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to fetch from file
# 1  
Old 04-03-2012
script to fetch from file

Hi Guys,

I have a file with around 1000 strings and i need to fetch the group to which each string belongs to from another file. Example is seen below.

File 1:

Code:
A
B
D

File 2:

Code:
START
A
B
C
END

START
M
K
L
END

START
A
C
D
END

Now the requirement is i need to search for the words in File 1 in File 2 and if available print it from START till END. But the set already fetched must not be repeated again. So my output will be like below.

Code:
START
A
B
C
END

START
A
C
D
END

Please help me out in scripting this.

Moderator's Comments:
Mod Comment Please use code tags for code and output

Last edited by Corona688; 04-03-2012 at 06:29 PM..
# 2  
Old 04-03-2012
Try:
Code:
awk -vRS= 'NR==FNR{for (i=1;i<=NF;i++) a[$i]=1;next}{for (i=1;i<=NF;i++) if ($i in a){print;next}}' file1 file2

# 3  
Old 04-03-2012
I need to fetch it from START till END in File 2, but the same set must not be repeated again.
# 4  
Old 04-03-2012
It is working for me:
Code:
[root@rhel ~]# cat f1
A
B
D
[root@rhel ~]# cat f2
START
A
B
C
END

START
M
K
L
END

START
A
C
D
END
[root@rhel ~]# awk -vRS= 'NR==FNR{for (i=1;i<=NF;i++) a[$i]=1;next}{for (i=1;i<=NF;i++) if ($i in a){print;next}}' f1 f2
START
A
B
C
END
START
A
C
D
END

What system are you using?
# 5  
Old 04-03-2012
Code:
$ cat set.awk

NR==FNR { SET[$1]++; next }

/START/ { for(X in ISET) delete ISET[X]; next }

/END/ {
        UNIQ="";        F=0

        for(X in ISET)
        if(SET[X])
        {
                F=1
                UNIQ = UNIQ "|" X
        }

        # If any of the input set is contained, and
        # this set hasn't been found before, print and record
        if(F && !U[UNIQ])
        {
                U[UNIQ]++;
                print "START"
                for(X in ISET)  print X;
                print "END\n"
        }

        next
}

$1 { ISET[$1]++ }

$ awk -f set.awk input data

START
A
B
C
END

START
A
C
D
END

$

# 6  
Old 04-03-2012
Code:
awk 'NR==FNR{$1=$1;s=$0;next}{p=$0;gsub($1 "|" $NF,x,p)}p~s' RS= ORS='\n\n' OFS="\n|\n"  file1 file2


Last edited by Scrutinizer; 04-03-2012 at 10:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to fetch values in csv

I need to fetch below values from this file. (1 Reply)
Discussion started by: nit42
1 Replies

2. Shell Programming and Scripting

Fetch data from file

Hi, I am new to scripting. I have a log file and need to fetch specific logs and copy to another file. A copy of the log is like this: =============================================================== = JOB : server123#jobs1.jobstream1 = USER : andyc = Tue 08/01/17... (3 Replies)
Discussion started by: Prngp
3 Replies

3. Shell Programming and Scripting

Fetch the latest filename shell script

Hi I want to fetch the latest file form the list example example= filename RATE_STATE_SETUPS.20151222.ccyymmdd.hhmmss.txt File pick which have latest ccyymmdd.hhmmss list of file in directory are RATE_STATE_SETUPS.20151222.20151222.170101.txt... (5 Replies)
Discussion started by: MOHANP12
5 Replies

4. Shell Programming and Scripting

How can I fetch few parameters in Shell script?

Hi All, I want to fetch few details out of a huge output of AWS CLI tools : I am using this command : ec2-describe-instances (6 Replies)
Discussion started by: Palak Sharma
6 Replies

5. Shell Programming and Scripting

make the name of file and fetch few things from log file

Hello All, I am working on a script where I need to fetch the value from a log file and log file creates with different name but few thing are common DEV_INFOMGT161_MULTI_PTC_BLD01.Stage_All_to_stp2perf1.042312114644.log STP_12_02_01_00_RC01.Stage_stp-domain_to_stp2perf2.042312041739.log ... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

6. Shell Programming and Scripting

fetch data between two timestamp using script

Hi Guys, I have the data in below format. 25 Dec 2011 03:00:01 : aaaaaaaaaaaaaaa 25 Dec 2011 04:23:23 : bbbbbbbbbbbbbbb 25 Dec 2011 16:12:45 : ccccccccccccccc 26 Dec 2011 04:45:34 : ddddddddddddddd 26 Dec 2011 17:01:22 : eeeeeeeeeeeeeee 27 Dec 2011 12:33:45 : ffffffffffffffffffffffff 28... (13 Replies)
Discussion started by: jaituteja
13 Replies

7. Shell Programming and Scripting

Script to fetch data from HTML

Hi All, There is a link from were I usually search somthing and fetch the data from. Is there any way to automate it through a script if I mention search criteria in a note pad. I mean the script to search the content on the notepad and resutls should be placed into another file. ... (2 Replies)
Discussion started by: indradev
2 Replies

8. Shell Programming and Scripting

Need a script to fetch file from shared folder to unix box

Hi All, We have a requirement of pulling a file from shared folder to our unix box(Our application Servers). The Shared Folder and the servers are in teh same domain for eg UK domain For this purpose I got information like if in both source and destination ftp is enabled we can fetch the... (2 Replies)
Discussion started by: dhivya.enjoy
2 Replies

9. Shell Programming and Scripting

Fetch lines from a file matching column2 of another file

Hi guys, Please help me out in this problem. I have two files FILE1 abc-23 : 4529675 cde-42 : 9824532 dge-91 : 1245367 gre-45 : 9824532 fgr-76 : 4529675 FILE2 4529675 : Gal Glu house-2-be 9824532 : cat mouse 1245367 : sirf surf-2-beta where FILE2 is a static file with fixed... (5 Replies)
Discussion started by: smriti_shridhar
5 Replies

10. Shell Programming and Scripting

fetch hostname and instance name using shell script

Hi All, Requirement is to fetch hostname and instance name using shell script from all configuration files on a server R12 on IBM AIX... could anyone please share such an experience encountered before.Is there such a script available in this forum or any other site.. Thanks for your time!... (0 Replies)
Discussion started by: a1_win
0 Replies
Login or Register to Ask a Question