Visit Our UNIX and Linux User Community


Shell program in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell program in unix
# 8  
Old 02-13-2008
Iam having the following situation:

Iam reading one file and taking that line one by one and searching that word in file2 using grep command

read_file1()
{
cat file1| while read line
do
grep $line file2 >> outputfile.dat
done
}

now iam having file 1
q
q
a
s
d

file2
q
q
q
a
s
d

Iam reading file1 and greping that word in file2

Since the file 1 has duplicates, output file is like

q
q
q
q
q
q
a
s
d

I want to avoid this duplicates in output file.


Can anybody help??
nivas
# 9  
Old 02-13-2008
what should be your expected o/p ??????
# 10  
Old 02-13-2008
output should be

q
q
q
a
s
d
nivas
# 11  
Old 02-13-2008
u r o/p and the requirement are not matching????
let file1
q
q
a
s
d
and file 2
file2
q
q
q
a
s
d
as per u r requirement 1st line of file 1 came i.e. q and matches with file 2 then goes to o/p file so o/p file has q
then 2nd q came from file 1 and matches with file2 so o/p file have 1 more q
then 3rd line a came and matches with file 2 so o/p have a also
then s , matched => o/p have s
then d came , matched => o/p have d

so u r o/p sud look like
q
q
a
s
d

please correct me if i am wrong and/or not understood u r question in right way ......
# 12  
Old 02-13-2008
Yes you are right.

i should get only 1 q in the output file along with the other data.

sorry for the confusion.
nivas
# 13  
Old 02-13-2008
okay, so just run uniq on your final output from your original script.
# 14  
Old 02-13-2008
Quote:
Originally Posted by nivas
how we can append???

i triyed this
grep $line old_extract.dat append | head -1 > outputfile

This doesn't worked.
Remove the word "append".

Anyway, this won't work because you need the output to be uniq. So just pipe the whole script through "uniq". You can do this:

Code:
cat file1| while read line ; do
grep $line file2 >> outputfile.dat
done | uniq

But then you might have output like:
q
a
c
q
d
q

If you need to avoid that, you'll need to run it through sort and then uniq.

Previous Thread | Next Thread
Test Your Knowledge in Computers #344
Difficulty: Medium
In February 2005, Sun announced the Sun Grid, a grid computing deployment on which it offered utility computing services priced at US$2 per CPU/hour for processing and per GB/month for storage.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to execute a unix shell script from a java program

Hi All, well , i am facing this problem.. i have tried a few sample codes but there isn't any solution . could anyone please give a sample code as of how to do this... Please see the below details...and read the details carefully. I have written some code, logic is 1)from... (4 Replies)
Discussion started by: aish11
4 Replies

2. Homework & Coursework Questions

Shell program in C

Hi all, I have an assignment from school to write a shell program in linux. the idea is to exercise fork() and execv() functions.. the shell program is supposed to be the master and every command that the user prints will run in a new process. we also need to try running the command in every... (1 Reply)
Discussion started by: r3vive
1 Replies

3. UNIX for Dummies Questions & Answers

Unix shell program that reverse a string...

Thanks so much vivekraj (0 Replies)
Discussion started by: andrew1400
0 Replies

4. Shell Programming and Scripting

Shell Program , need help!!

Hi all, I am trying to get a file from an ftp server and i have the list of files which needs to be get from the ftp server. grep unix_prg*.* log.txt > log1.txt log1.txt (which has the list of files) 06-29-09 00:00AM 3550258 unix_prg090629 06-28-09 07:00PM ... (7 Replies)
Discussion started by: raghav1982
7 Replies

5. Shell Programming and Scripting

Shell program in unix

Hi, Iam having file 1 and file 2 as follows: file1 file2 a a a b a c b c I want to get an output file which contains a a (4 Replies)
Discussion started by: nivas
4 Replies

6. Programming

Returning Strings from C program to Unix shell script

Hi, I'm having a requirement where I need to call a C program from a shell script and return the value from the C program to shell script. I refered a thread in this forum. But using that command in the code, it is throwing an error clear_text_password=$(get_password) Error: bash:... (24 Replies)
Discussion started by: venkatesh_sasi
24 Replies

7. Shell Programming and Scripting

help with shell program

I want to print the value of variables a1, a2, a3 in for loop in the following program: a1=this a2=is a3=printed for((i=1;i<4;i++)) do var=a$i #w=`echo $var` e=${var} echo $e done But actually I get a1,a2,a3 as the output not the "this is printed" So the main question is if I... (3 Replies)
Discussion started by: adgarg
3 Replies

8. Programming

Returning Strings from C program to Unix shell script

Hi, Iam calling a C program from a Unix shell script. The (C) program reads encrypted username/password from a text file , decrypts and returns the decrypted string. Is there any way i can return the decrypted string to Unix shell program. My shell script uses the output of the program to... (11 Replies)
Discussion started by: satguyz
11 Replies

9. Shell Programming and Scripting

shell program

How to write a shell script which takes 3 strings as positional parameters,first and second are file names and third is a directory.if the two files exist in `pwd` and they contain a specific pattern and their size is greater than 32 bytes,moves these files into directory? (1 Reply)
Discussion started by: rameshparsa
1 Replies

10. Shell Programming and Scripting

ftp files from Unix to Windows through shell program

Hi, I made a shell script to allow user to ftp file to windows shared drive. Here is part of my code within my shell script: /usr/bin/ftpmtc $usr $pswd $jobno Within 'ftpmtc': #ARGUMENT: USER,PASWD,JOBNO,VER,LOG_DATE,$$ $UCB/echo "user $1 $2" > $inst_file $UCB/echo "cd prod" >> $inst_file... (7 Replies)
Discussion started by: whatisthis
7 Replies

Featured Tech Videos