Recently, in order to complete the work, two files A.old and a were compared, then the new additions in a were saved to a, and the other parts were deleted. After finding the relevant data, it is found that there are two better methods.
1. Use the diff command
diff old. file new. file | grep " ^> | sed s/>//g > Temp. file MV temp. file new. file # overwrites the contents of the Temp.file with new. file
sed ' s/^>//g ' # s means to replace, meaning to replace all symbols beginning with "" sed's/ The string to be replaced/the new string/g'
2. Using the awk command
awk ' nr==fnr{a[$0]}nr>fnr{if (! ( $ in a)) Print +}' old. file new. file > Temp. file MV Temp. file new. file
awk built-in variables
Variable name |
Representative meaning |
$ |
Represents a whole row of data |
Nf |
Total number of fields per row ($) |
Nr |
What awk is currently dealing with is the "number of rows" of data |
Fs |
The current split character, by default, is the space key |
The difference between NR and FNR
NR: Current Record Count
FNR: The number of records for the current file, its scope within a file, reopening the file, FNR starting from 1.
The difference between the two is mainly manifested in the number of awk operation files greater than 1 o'clock.
Reference: http://bbs.chinaunix.net/thread-1155924-1-1.html
Http://bbs.chinaunix.net/thread-2309494-1-1.html
Linux text comparison-diff&awk