Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comm command with awk (or sed)

I'd like to print out all lines that only exist in one of the two input streams using comm (the use of this command is not mandatory though). However using awk or sed seems to mess up it a bit. Eg. comm -23 (git branch -r | awk -F\"[/]\" '/matchinstring/{print $2}' | sort) (blah blah other stream) fails ("The system cannot find the file specified."). awk tries to use the | sort... part. Is there a way to overcome this? Sure, I could output to 2 files, and than use comm on those two files, but can I make it a one-liner? I'm using ConEmu.

Example input 1:

matchingstring-234
matchingstring-456

Example input 2:

matchingstring-123
matchingstring-345
matchingstring-456

Expected output:

matchingstring-234
like image 579
Miklos Jakab Avatar asked Feb 15 '26 12:02

Miklos Jakab


1 Answers

Input files:

$ cat f1
matchingstring-234
matchingstring-456

$ cat f2
matchingstring-123
matchingstring-345
matchingstring-456

Using awk

$ awk 'FNR==NR{arr[$1];next}!( $1 in arr)' f2 f1
matchingstring-234

Using grep

$ grep -v -F -x -f f2 f1
matchingstring-234

Using join

$ join -v 2 <(sort f2) <(sort f1)
matchingstring-234

From Man grep

-v, --invert-match
          Invert the sense of matching, to select non-matching lines.

-F, --fixed-strings
          Interpret PATTERN as a list of fixed strings (instead of regular
          expressions), separated by newlines,  any  of  which  is  to  be
          matched.

-x, --line-regexp
          Select  only  those  matches  that exactly match the whole line.
          For a regular expression pattern, this  is  like  parenthesizing
          the pattern and then surrounding it with ^ and $.

-f FILE, --file=FILE
          Obtain patterns  from  FILE,  one  per  line.   The  empty  file
          contains zero patterns, and therefore matches nothing.  Multiple
          -f can be used to specify different files.
like image 121
Akshay Hegde Avatar answered Feb 17 '26 07:02

Akshay Hegde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!