UNIX shell scripting programming in files

Create 2 files in unix in 2 different directories, compare them and fetch common words between these 2 files. Print them on the screen and also redirect the output to your home directory in the below format: file 1 | file 2 line no: word 1 | line no: word 1 line no: word 2 | line no: word 2 line no:

#!/bin/sh
#i have created two files in different directories named dir1 and dir2 at command line. files named as file1 and file2.

mv file1 dir2
grep -f file1 file2 
exit

problem i am not able to move the file and how to get given syntax asked in the question

Welcome mounica bijjala ,

Firstly thank you for trying to use the CODE tags. The closing tag needs to be though.

Considering your question, I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far on the bit you are stuck with?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Yes this is homework.
Please provide school name, professor name, course name/number, as said in the top readme article in this forum!

I guess the required output is multi-line?

file 1 | file 2
line no: word 1 | line no: word 1 
line no: word 2 | line no: word 2 

I have wrapped it in code tags.

Yes the output is multiline.
I have a basic knowledge in Unix.
I am not able compare the two files present in two different directories.
I know nl is the command to get the line number.But how to compare the files and how to get the output in the required syntax?

---------- Post updated at 06:05 AM ---------- Previous update was at 05:58 AM ----------

I have created 2 directories named dir1 and a subdirectory dir2. First i used a command diff -r ./dir1 dir2
Because it am in dir2 i dint used ./
As i read -r buffer is used to compare recursively the subdirectories.
I am getting an error said dir1 not found.
Then i used grep command and i got the same error.
I have strucked at comparing the files.

First please provide school name, professor name, course name/number, as said in the top readme article in this forum!

i am from amrita college of engineering,
course unix
professor ashwin

The first task is: have two directories.
Did you create dir2 within dir1 or next to dir1?
And where is your current work directoy? (pwd command)

---------- Post updated at 10:23 AM ---------- Previous update was at 10:21 AM ----------

I created dir2 is the sub directory of dir1
My current working directory is home/dir1/dir2

Assuming your current work directory is dir2 being a subdirectory of dir1, you can refer to dir1 as .. .
Assuming you have file1 in dir1, you can refer to it as ../file1 .
Assuming you have file2 in dir2 that is your current work directory you can refer to it as ./file2 or simply file2 .

So far, so good. Notice, though, that every file has a "short address" (the files name) and a "long address" (the fies name and its full path).

This works similar to telephone numbers: if you give someone your number without a regional area code and country code it will work as long as the person is in the same area as you.

123 456 789

But once outside this area you need to give him your area code too to make it work

0123 / 123 456 789

and to make sure the number works from whereever he is you will have to add the country code too:

+123 (123)  123 456 789

The same is true for files: you can address them by their name only, but then you will only find them if you happen to be in the same directory. If you aren't you won't. To make sure you find them regardless of where you are provide a full pathname instead. In this case the files name is not

file1

but rather

/some/directory/where/to/find/file1

As a general rule: to make sure files are always found regardless of from where you call a script use always the long form of (so-called) "absolute path names" when you address files inside scripts.

I hope this helps.

bakunin

One word to the -r (recurse) options for diff or grep: they can only work if the directories are next to each other, and the strength is if the number of files is unknown. Not the case here, we have two known files.

How do the two input files look like?
Is there one word per line or many words per line?
It's much easier to work on full lines than on parts of the lines.