How to implement scenario?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to implement scenario?
# 1  
Old 10-20-2010
How to implement scenario?

hi,
i am having three files which is having following data
file1:
Code:
field1  field2    field3
1        A           B
2        C           D
3        E           F

file2:
Code:
4       G           H
1       I           J
5       K           L

file3:
Code:
4      M           N
6      O           P

here i am reading three different files in the order File3,file2,file1 .The logic i needed is , for example if the record corresponding to '1' is present in multiple files ,then i need to write the record which is present in the first file and discard the records corresponding to 1 in the rest rest of the files and writing all data in a single file

output
Code:
6    O    P
4    G    H
5    K    L
1    A    B
2    C    D
3    E    F

please suggest a way to do this..I am a beginner in unix so please explain the logic clearly as well
regards
Angel

Moderator's Comments:
Mod Comment Use [code] and [/code] tags when posting code, data or logs to preserve formatting and enhance readability, thanks.

Last edited by angel12345; 10-20-2010 at 03:04 AM..
# 2  
Old 10-20-2010
If the files names are in alphabetic order, you can start with something like this:
Code:
awk '{if (! a[$1] ) {a[$1]++;print}}' file*

# 3  
Old 10-20-2010
MySQL

Code:
# ./justdoit
4       G           H
6      O           P
1        A           B
5       K           L
2        C           D
3        E           F

Code:
 ## justdoit @ygemici ##
 
#!/bin/bash
for file in file3 file2 file1
 do
  while read -r l
   do
     isthis=$(echo $l | sed 's/^\(.\).*/\1/')
     status=write
     for i in ${arr[@]}
      do
       if [ "$i" = "$isthis" ] ; then
        status=notwrite
       fi
      done
     if [ "$status" != "notwrite" ] ; then
        if grep $isthis file1 ; then
           grep ${isthis} file1>/dev/null
           arr=("$arr $isthis" )
        elif grep ${isthis} file2 ; then
           grep ${isthis} file2>/dev/null
           arr=("$arr $isthis" )
        else
           grep ${isthis} file3
           arr=("$arr $isthis" )
        fi
     fi
   done <$file
 done

# 4  
Old 10-20-2010
Quote:
Originally Posted by Klashxx
If the files names are in alphabetic order, you can start with something like this:
Code:
awk '{if (! a[$1] ) {a[$1]++;print}}' file*

A condensed version of this approach:
Code:
awk '!a[$1]++' file*

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

FTP failure with different scenario

Hi Gurus the below FTP code works fine, but the issue is from past 1 week couldn't able to send the files to external system. #--------------------------------------------------------------------# #Start the ftp session #--------------------------------------------------------------------#... (3 Replies)
Discussion started by: SeenuGuddu
3 Replies

2. Shell Programming and Scripting

Logic help with Scenario

Hello Folks I am looking for logic help for below scenerio with respect to AIX n unix script 1) We need to get the date of all the saturday in yr 2) L_o left over days is weeks left over days for previous month for eg. first sat of feb is 4th of feb in that week we have 29 - 30 - 31 from Jan... (2 Replies)
Discussion started by: joshiamit
2 Replies

3. Emergency UNIX and Linux Support

Help in below scenario

Hi, my file has the data like below: 11,231,ABCVAV 22,AAHJHAj22,hdsjkhdls 22,dhskjhdkshd 22,gdgkdkadh 11,232,dgsjgdjh 22,ghdskahdkja 22,shdkajshs 11,233,ddjs 22,dhjkahkd 22,hsajhaah 11,231,sjkjsjj 22,ahkh 22,hsakh From the above i need only the records which starts as 11,231... (5 Replies)
Discussion started by: pandeesh
5 Replies

4. Shell Programming and Scripting

Challenging scenario

Hi, My input file contains 1,2 2,4 3,6 4,9 9,10 My expected output is 1,10 2,10 3,6 4,1 9,10 (6 Replies)
Discussion started by: pandeesh
6 Replies

5. UNIX for Advanced & Expert Users

Is it possible to write script for this scenario??

Hi all, We are hosting Web server using Apache -tomcat , in our company we are following this way for our every new enhancement in production server. for example if the developers move any new code moved to the server means that time we'll do 1.first check the time / date 2. mod_jk... (0 Replies)
Discussion started by: anishkumarv
0 Replies

6. Shell Programming and Scripting

How to Script This Scenario

hi all, i have to schedule an email containing the information about some orphan connections existing on the server depending upon the system date. the format of the info to be sent in email is : Process id username servername time when connection... (0 Replies)
Discussion started by: Priyanka S
0 Replies

7. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

8. Shell Programming and Scripting

SFTP scenario

#!/usr/bin/ksh Archive_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR/Archive' Source_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR' cd $Source_Dir HOST='xyz.abc.com' USER='abcOUT' PSW='xyzOUT' file="Request*.pgp" for i in 1 2 3 4 5 6 do sftp $USER@$HOST <<END_SCRIPT $PSW bin if ] ; then ... (3 Replies)
Discussion started by: alfredo123
3 Replies

9. Shell Programming and Scripting

Sorting a column in this scenario

Hi, I need to sort the 3rd column in ascending order , each and every column are seperated by ~. sample input file : 2~Lead Time Metrics~jennife1 2~Lead Time Metrics~mmullis 2~Lead Time Metrics~lisah 2~Lead Time Metrics~pros 2~Lead Time Metrics~kenward can any one help me out . i... (1 Reply)
Discussion started by: krishnan_6015@y
1 Replies

10. UNIX for Advanced & Expert Users

Weird scenario with Awk

Guys, this one is rather odd. I've got an array of numbers, and I'm trying to select only the records with the string "Random" in the 4th column. I'm using awk in this format: awk '{ if (( $6 -eq Random )) print $0 }' For some odd reason, this is simply giving me the list of all the entries... (4 Replies)
Discussion started by: Khoomfire
4 Replies
Login or Register to Ask a Question