前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【小码匠自习室】让错误成为孩子进步的阶梯

【小码匠自习室】让错误成为孩子进步的阶梯

作者头像
小码匠
发布2022-08-08 13:06:59
3540
发布2022-08-08 13:06:59
举报
文章被收录于专栏:小码匠和老码农

碎碎念

  • 今天梳理了这篇文章,同一个地方只能跌一次跟头,再重复错误肯定被老码农敲脑袋
  • 梳理这篇文章源于3月份参加NOI Online测试赛没有全文比较输出文件内容,只是对比了几个值,导致爆零( ̄﹏ ̄;)
  • 标题是老码农起的,心灵鸡汤太多了,以后想管他叫”唐鸡汤“了。(*^_^*)

准备测试文件

  • mode_ex1.ans
代码语言:javascript
复制
4
1
5
1
4
2
4
8
2
1
2
3
4
5
  • mode_ex1-2.ans
    • 修改了第13行数据:4 -> 6
代码语言:javascript
复制
4
1
5
1
4
2
4
8
2
1
2
3
6
5
  • mode_ex1-3.ans
    • 和第一个文件相同
代码语言:javascript
复制
4
1
5
1
4
2
4
8
2
1
2
3
4
5
  • mode_ex1-4.ans
    • 第二行:1 -> 4
    • 倒数第二行:4 -> 6
    • 修改了第二行和倒数第二行数据
代码语言:javascript
复制
4
2
5
1
4
2
4
8
2
1
2
3
6
5

Windows

文件内容比较命令:fc

  • 查看fc帮助
代码语言:javascript
复制
D:\day1\mode>fc /?
比较两个文件或两个文件集并显示它们之间
的不同


FC [/A] [/C] [/L] [/LBn] [/N] [/OFF[LINE]] [/T] [/U] [/W] [/nnnn]
   [drive1:][path1]filename1 [drive2:][path2]filename2
FC /B [drive1:][path1]filename1 [drive2:][path2]filename2

  /A         只显示每个不同处的第一行和最后一行。
  /B         执行二进制比较。
  /C         不分大小写。
  /L         将文件作为 ASCII 文字比较。
  /LBn       将连续不匹配的最大值设置为指定
             的行数。
  /N         在 ASCII 比较上显示行数。
  /OFF[LINE] 不要跳过带有脱机属性集的文件。
  /T         不要将制表符扩充到空格。
  /U         将文件作为 UNICODE 文本文件比较。
  /W         为了比较而压缩空白(制表符和空格)。
  /nnnn      指定不匹配处后必须连续
             匹配的行数。
  [drive1:][path1]filename1
             指定要比较的第一个文件或第一个文件集。
  [drive2:][path2]filename2
             指定要比较的第二个文件或第二个文件集。

示例

比较:mode_ex1.ans 和 mode_ex1-3.ans
  • 内容一样
代码语言:javascript
复制
D:\day1\mode>fc mode_ex1.ans mode_ex1-3.ans
正在比较文件 mode_ex1.ans 和 MODE_EX1-3.ANS
FC: 找不到差异

比较:mode_ex1.ans 和 mode_ex1-2.ans
  • 修改了倒数第2行数据:4 -> 6
代码语言:javascript
复制
D:\day1\mode>fc mode_ex1.ans mode_ex1-2.ans
正在比较文件 mode_ex1.ans 和 MODE_EX1-2.ANS
***** mode_ex1.ans
3
4
5
***** MODE_EX1-2.ANS
3
6
5
*****
比较:mode_ex1.ans 和 mode_ex1-4.ans
  • 两行不一样,正数第二行和倒数第二行
代码语言:javascript
复制
D:\day1\mode>fc mode_ex1.ans mode_ex1-4.ans
正在比较文件 mode_ex1.ans 和 MODE_EX1-4.ANS
***** mode_ex1.ans
4
1
5
***** MODE_EX1-4.ANS
4
2
5
*****

***** mode_ex1.ans
3
4
5
***** MODE_EX1-4.ANS
3
6
5
*****

Linux

文件内容比较命令:diff

  • 查看diff帮助
代码语言:javascript
复制
oder@ubuntu:~$ diff --help
Usage: diff [OPTION]... FILES
Compare FILES line by line.

Mandatory arguments to long options are mandatory for short options too.
      --normal                  output a normal diff (the default)
  -q, --brief                   report only when files differ
  -s, --report-identical-files  report when two files are the same
  -c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
  -u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context
  -e, --ed                      output an ed script
  -n, --rcs                     output an RCS format diff
  -y, --side-by-side            output in two columns
  -W, --width=NUM               output at most NUM (default 130) print columns
      --left-column             output only the left column of common lines
      --suppress-common-lines   do not output common lines

  -p, --show-c-function         show which C function each change is in
  -F, --show-function-line=RE   show the most recent line matching RE
      --label LABEL             use LABEL instead of file name and timestamp
                                  (can be repeated)

  -t, --expand-tabs             expand tabs to spaces in output
  -T, --initial-tab             make tabs line up by prepending a tab
      --tabsize=NUM             tab stops every NUM (default 8) print columns
      --suppress-blank-empty    suppress space or tab before empty output lines
  -l, --paginate                pass output through 'pr' to paginate it

  -r, --recursive                 recursively compare any subdirectories found
      --no-dereference            don't follow symbolic links
  -N, --new-file                  treat absent files as empty
      --unidirectional-new-file   treat absent first files as empty
      --ignore-file-name-case     ignore case when comparing file names
      --no-ignore-file-name-case  consider case when comparing file names
  -x, --exclude=PAT               exclude files that match PAT
  -X, --exclude-from=FILE         exclude files that match any pattern in FILE
  -S, --starting-file=FILE        start with FILE when comparing directories
      --from-file=FILE1           compare FILE1 to all operands;
                                    FILE1 can be a directory
      --to-file=FILE2             compare all operands to FILE2;
                                    FILE2 can be a directory

  -i, --ignore-case               ignore case differences in file contents
  -E, --ignore-tab-expansion      ignore changes due to tab expansion
  -Z, --ignore-trailing-space     ignore white space at line end
  -b, --ignore-space-change       ignore changes in the amount of white space
  -w, --ignore-all-space          ignore all white space
  -B, --ignore-blank-lines        ignore changes where lines are all blank
  -I, --ignore-matching-lines=RE  ignore changes where all lines match RE

  -a, --text                      treat all files as text
      --strip-trailing-cr         strip trailing carriage return on input

  -D, --ifdef=NAME                output merged file with '#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE is 'old', 'new', or 'unchanged'.  GTYPE is LTYPE or 'changed'.
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

  -d, --minimal            try hard to find a smaller set of changes
      --horizon-lines=NUM  keep NUM lines of the common prefix and suffix
      --speed-large-files  assume large files and many scattered small changes
      --color[=WHEN]       colorize the output; WHEN can be 'never', 'always',
                             or 'auto' (the default)
      --palette=PALETTE    the colors to use when --color is active; PALETTE is
                             a colon-separated list of terminfo capabilities

      --help               display this help and exit
  -v, --version            output version information and exit

FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'.
If --from-file or --to-file is given, there are no restrictions on FILE(s).
If a FILE is '-', read standard input.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

Report bugs to: bug-diffutils@gnu.org
GNU diffutils home page: <https://www.gnu.org/software/diffutils/>
General help using GNU software: <https://www.gnu.org/gethelp/>

示例

  • 显示目录下文件列表
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ ls -la
total 4872
drwxrwxrwx 2 coder coder    4096 Jun 13 00:52 .
drwxrwxr-x 3 coder coder    4096 Jun 13 01:06 ..
-rwxrw-rw- 1 coder coder      42 Jun 13 00:48 mode_ex1-2.ans
-rwxrw-rw- 1 coder coder      40 Jun 13 00:50 mode_ex1-3.ans
-rwxrw-rw- 1 coder coder      40 Jun 13 00:52 mode_ex1-4.ans
-rwxrw-rw- 1 coder coder      28 May  2 07:54 mode_ex1.ans
-rwxrw-rw- 1 coder coder      52 May  2 07:54 mode_ex1.in
-rwxrw-rw- 1 coder coder     178 May  2 07:48 mode_ex2.ans
-rwxrw-rw- 1 coder coder 4954289 May  2 07:48 mode_ex2.in
  • 常规命令
    • "|"表示前后2个文件内容有不同
    • "<"表示后面文件比前面文件少了1行内容
    • ">"表示后面文件比前面文件多了1行内容
  • 比较:mode_ex1.ans 和 mode_ex1-3.ans
    • 内容一样
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ diff mode_ex1.ans mode_ex1-3.ans
1,14c1,14
< 4
< 1
< 5
< 1
< 4
< 2
< 4
< 8
< 2
< 1
< 2
< 3
< 4
< 5
---
> 4
> 1
> 5
> 1
> 4
> 2
> 4
> 8
> 2
> 1
> 2
> 3
> 4
> 5
\ No newline at end of file

  • 奇怪吧,文件内容命名一样,显示结果确是都变了,接下来用file查看文件编码格式
    • Windows中:回车符为 ‘\r’,回到行首;换行符为 ‘\n’,换到当前位置的下一行;
    • Linux系统中:觉得每行结尾加两个字符没有必要,故结尾只有换行符,即‘\n’;
    • Mac系统中:每行结尾只有回车符,即‘\r’
    • 文件编码格式搞得鬼,mode_ex1-3.ans文件是再windows下拷贝mode_ex1.ans内容,新建生成的,每个行都带有回车换行符号,所以比较的时候就都显示不同了
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1.ans
mode_ex1.ans: ASCII text
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1-3.ans
mode_ex1-3.ans: ASCII text, with CRLF line terminators

可以用Linux下的命令:sed进行替换

  • 参考命令
代码语言:javascript
复制
sed -i "s/\r//" mode_ex1-3.ans

  • 替换
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ sed -i "s/\r//" mode_ex1-3.ans
coder@ubuntu:~/Desktop/coder/mode$ sed -i "s/\r//" mode_ex1-2.ans
coder@ubuntu:~/Desktop/coder/mode$ sed -i "s/\r//" mode_ex1-4.ans
coder@ubuntu:~/Desktop/coder/mode$ file ^C
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1-2.ans 
mode_ex1-2.ans: ASCII text
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1-3.ans 
mode_ex1-3.ans: ASCII text
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1-4.ans 
mode_ex1-4.ans: ASCII text
  • 替换后继续比较
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ file mode_ex1.ans mode_ex1-3.ans 
mode_ex1.ans:   ASCII text
mode_ex1-3.ans: ASCII text
比较:mode_ex1.ans 和 mode_ex1-3.ans
  • 两个文件内容相同
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ diff mode_ex1.ans mode_ex1-3.ans 
coder@ubuntu:~/Desktop/coder/mode$

因为内容完全相同,所以没显示不同点

比较:mode_ex1.ans 和 mode_ex1-2.ans
  • 只有一行不一样,倒数第二行
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ diff mode_ex1.ans mode_ex1-2.ans 
13c13
< 4
---
> 6

上面的"13c13"表示两个文件在第13行内容不同

比较:mode_ex1.ans 和 mode_ex1-4.ans
  • 两行不一样,正数第二行和倒数第二行
代码语言:javascript
复制
coder@ubuntu:~/Desktop/coder/mode$ diff mode_ex1.ans mode_ex1-4.ans 
2c2
< 1
---
> 2
13,14c13,14
< 4
< 5
---
> 6
> 5
\ No newline at end of file

上面的"2c2"表示两个文件在第2行内容不同 "13,14c13,14"表示两个文件在第13,14行内容不同, ==但其实只有13行内容不同啊,没搞明白==

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-06-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小码匠和老码农 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 碎碎念
  • 准备测试文件
  • Windows
    • 文件内容比较命令:fc
      • 示例
        • 比较:mode_ex1.ans 和 mode_ex1-3.ans
        • 比较:mode_ex1.ans 和 mode_ex1-2.ans
        • 比较:mode_ex1.ans 和 mode_ex1-4.ans
    • Linux
      • 文件内容比较命令:diff
        • 示例
          • 比较:mode_ex1.ans 和 mode_ex1-3.ans
          • 比较:mode_ex1.ans 和 mode_ex1-2.ans
          • 比较:mode_ex1.ans 和 mode_ex1-4.ans
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档