Linux text comparison-diff&awk

Source: Internet
Author: User
Tags diff

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.