简体   繁体   中英

Read a file line by line -> search for each line in another text document, if match, output to another text file

I have a script that takes two parameters.

ls $1 > $1.txt
ls $2 > $2.txt  

I now have a.txt ($1 is directory 'a', $2 is directory 'b') and b.txt each with their contents listed inside them. What I want to do is search line by line in a.txt and see if there is a match in b.txt.

If I have these files in a.txt:

file1 -> search for file1 in b.txt, if match, output to a_match.txt
file2 -> search for file2 in b.txt, if match, output to a_match.txt
file3 -> search for file3 in b.txt, if match, output to a_match.txt  

Vice versa for b.txt. How would I do this? Also, not looking for alternative methods to doing this, there are soooo many other ways to check differences amongst directories, but this is the only acceptable way for my assignment :(

这是我认为可以满足您要求的快速解决方案:

fgrep -x -f b.txt a.txt > a_match.txt

Okay, this is clearly homework so I'm not going to give you a full solution. Here's the outline

while "there are lines left in the file, read a line" do
     grep "the line" "the file"
done

the key for getting lines is the read command. Do help read in bash for details.

Update

Okay, so let's look a little more closely. Start the script with

Let's call the script a.bash. You'll want to run it as

 $ a.bash b.txt < a.txt > a_match.txt  

a.bash is a script that reads line from a.txt, searches in b.txt and sends output to STDOUT which you then direct into your file a_match.txt .

Start the script witha "shebang line'. Usually it's

 #!/usr/bin/bash --

or something similar.

The read primitive simply reads a line into a variable, and by default reads into a variable named REPLY. So "there are lines left in the file, read a line" is simply

 while read

The name of the file to search in will be in the special shell variable $1, representing the first argument. Since the other names are used in redirection, they don't appear as arguments at all.

Now, the command grep(1) searches a file for a string and put's the line on which the string occurs onto STDOUT. So all we need for the search is

 grep $REPLY b.txt

(Read the man page for grep .)

Since grep(1) puts the output on STDOUT anyway, it'll go out and be redirected into a_matches.txt . That's your "grep the line in the file" line.

Now just fit that all together.

Quiz:

  1. Why is it $REPLY ?
  2. How would you change the script so you could call it as:

    $ a.bash a.txt b.txt a_matches.txt

This could help you just go through the tutorial and it bests meet your requirements.Again there is not just one way to read a file line by line and some are faster than others and some are more intuitive than others.

http://www.unix.com/tips-tutorials/18009-12-ways-parse-file.html

This is what I would use:

diff dir1/ dir2/

or

diff "$1" "$2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM