简体   繁体   中英

Compare a string from a csv file againt a lookup file in unix shell scripting

I am new to unix scripting. I have a requirement that I need to check a string from a csv file against a set of strings in another csv file. My file1 will have values like below, file1

task desc
1    network error in there in line one.
2    data error is there in line three.
3    connection is missing from device.
4    new error is there

And file2 will have the standard lookup data file2

Network error
data error
connection is 

The file2 is static and my file1 changes daily. My requirement is to check each line in the file1 and check against the strings in file2 . if there are any new error entry in the file1 , then that entire row needs to be write into a new file called file3 . this kind of daily standard error checking from a log file and report the new error. For example from my above example, the output file3 needs to look like below.

file3

4    new error is there

tail + grep solution:

tail -n +2 file1 | grep -ivf file2 > file3

> cat file3
4    new error is there

  • tail -n +2 file1 - output the last part of file1 starting from the 2nd line

grep options:

  • -f - obtain patterns from file

  • -i - caseinsensitive matching

  • -v - invert the sense of matching, to select non-matching lines

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