File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
12
12
13
- _ 1302 TILs and counting..._
13
+ _ 1303 TILs and counting..._
14
14
15
15
---
16
16
@@ -1253,6 +1253,7 @@ _1302 TILs and counting..._
1253
1253
- [ File Type Info With File] ( unix/file-type-info-with-file.md )
1254
1254
- [ Find All Files Matching A Name With fd] ( unix/find-all-files-matching-a-name-with-fd.md )
1255
1255
- [ Find A File Installed By Brew] ( unix/find-a-file-installed-by-brew.md )
1256
+ - [ Find Duplicate Lines In A File] ( unix/find-duplicate-lines-in-a-file.md )
1256
1257
- [ Find Files With fd] ( unix/find-files-with-fd.md )
1257
1258
- [ Find Newer Files] ( unix/find-newer-files.md )
1258
1259
- [ Fix Unlinked Node Binaries With asdf] ( unix/fix-unlinked-node-binaries-with-asdf.md )
Original file line number Diff line number Diff line change
1
+ # Find Duplicate Lines In A File
2
+
3
+ Let's say I have a large file in a Ruby project. I want to find instances of a
4
+ ` field ` declaration being duplicated throughout the file. Just searching for
5
+ duplicate lines within the file is going to result in all kinds of false
6
+ positives (think, lots of duplicate ` end ` lines).
7
+
8
+ What I can do is ` grep ` for a pattern that will just match on the lines that
9
+ are ` field ` declarations. The results of the ` grep ` can then be piped to ` sort `
10
+ which will order them. This ordering will mean that any duplicates are placed
11
+ next to each other. Lastly, I'll pipe the sorted lines to ` uniq ` with the ` -d `
12
+ flag which will filter the results down to just those lines that are repeated.
13
+
14
+ Here is what the whole thing looks like:
15
+
16
+ ```
17
+ $ grep -o "field :[a-zA-Z_][a-zA-Z_0-9]*" file.rb | sort | uniq -d
18
+ ```
19
+
20
+ See ` man uniq ` for more details on the available flags.
You can’t perform that action at this time.
0 commit comments