Visit Our UNIX and Linux User Community


searching and displaying help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching and displaying help
# 1  
Old 02-06-2009
searching and displaying help

I have two input files like this:

File-1
-----

a1234 abc town
f2345 def village
t5678 pqr county

File-2
------

123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234

I want to check the lines that match the first field of File-1 in File-2 (where it appears after the "id") and create two output files as follows (based on whether the item is found in File-2 or not):

Output-1 (when the first field of File-1 is found at File-2)
-------------------------------------------------------

a1234 123456
t5678 789012

Output-2 (when the first field of File-1 is not found in File-2)
----------------------------------------------------------

f2345 Not Found

How to do this in a shell script?

Any help will be really appreciated.

Thanks,

Ajay
# 2  
Old 02-07-2009
Something like this?

Code:
awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found"
}' File-2 File-1

Regards
# 3  
Old 02-09-2009
:(

hmm, that didn't work Smilie
# 4  
Old 02-09-2009
Quote:
Originally Posted by ajay41aj
hmm, that didn't work Smilie
Be more precisely, what didn't work? No output, wrong output, warnings, errors?

Regards
# 5  
Old 02-09-2009
I get Output-2 but not Output-1.
# 6  
Old 02-10-2009
To print the output to the files you can redirect the outputs like this:

Code:
 awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1

This is what I get:

Code:
$ cat File-1
a1234 abc town
f2345 def village
t5678 pqr county
$
$ cat File-2
123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234
$
$ awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
$
$ cat Output-1
a1234 123456
t5678 789012
$
$ cat Output-2
f2345 not found
$

Regards
# 7  
Old 02-17-2009
Thanks for the script.

But, I have issues when using it when the input file-2 has "test-2" instead of "test-2" or "test-3" instead of "test3" (the use of "-" in other columns.)

So, I am looking for the script to look only for the field that has the "id-" but must be able to ignore if the "-" presents in any other column.

Any suggestions?

Previous Thread | Next Thread
Test Your Knowledge in Computers #79
Difficulty: Easy
GNU/Linux is directly descended from BSD.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

2. Shell Programming and Scripting

need help with displaying only names using ls -l

Hi I am kind of stuck I need help with printing only the names of the folder in the format LAST NAME, F so last name and the first character of first name using ls -l command in the /home directory currently they are in the format firstname.lastname please advice (4 Replies)
Discussion started by: classic
4 Replies

3. UNIX for Dummies Questions & Answers

displaying $ in data

Hi, I have to append "abc$abc" to the existing data in solaris.When i run my unix script it is writing only "abc" instead of "abc$abc" Please let me know if we can write $ to the data.Please advice! (2 Replies)
Discussion started by: ammu
2 Replies

4. Shell Programming and Scripting

Displaying output from df -k in GB instead of KB

Hello all, Code below: echo "Oracle Filesystems" echo "------------------" echo for j in `df -l -k |grep total|grep ora|grep -v storage|grep -v vg00|awk '{print $1}'` do echo $j is `df -l -k $j |grep total|grep ora|grep -v storage|grep -v vg00|awk -F":" '{print $2}'|awk '{print $1}'` KB... (6 Replies)
Discussion started by: LinuxRacr
6 Replies

5. UNIX for Dummies Questions & Answers

displaying the users

how can i list the users( and only those users) who logged in more than once? thanks in advance... (1 Reply)
Discussion started by: needyourhelp
1 Replies

6. UNIX for Dummies Questions & Answers

searching and displaying most commonly used words

Hi guys, i need to search the most commonly occuring words in a file and display their counts of about 30000 words and the words shud not be of typ specified in file 2 e. words like is,for,the,an,he,she etc... k. file1: ALICE was beginning to get very tired of sitting by... (2 Replies)
Discussion started by: arunsubbhian
2 Replies

7. Shell Programming and Scripting

Displaying files

I am beginner to UNIX. I wanted to display 5 filesname with size from BIN directory. The order of display should be descending order in size. Can help me. (1 Reply)
Discussion started by: giridher2000
1 Replies

8. UNIX for Dummies Questions & Answers

displaying with ls

hi, how can display year parameter also while listing files from a directory?it displyas only if last acces sis more than 1 yr i guess.can it be dispalyed using some option or some method? thanks and regards vivek.s (2 Replies)
Discussion started by: vivekshankar
2 Replies

9. SCO

vi editor not displaying?

Hi guys, I have changed some path in the vi .profile and then i shutdown the system and when i reboot it i was unable to use vi. It is showing vi not found.Likewise for few other commands also. How to solve this problem and make vi work again. Plz. do provide the answer it is... (3 Replies)
Discussion started by: ananthu_m
3 Replies

10. UNIX for Dummies Questions & Answers

Displaying what a command is doing/has done

I would like to see what a command is doing. For example, say I move 10 files using mv * somedir. Can I use some other command to see a verification of each files movement. Like: file1 moved to somedir file2 moved to somedir ... but, i'd to have this capability for all commands. it seems... (1 Reply)
Discussion started by: quantumechanix
1 Replies

Featured Tech Videos