Friday, May 17, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 816  / 2 Years ago, thu, january 13, 2022, 4:21:51

for example, I have 2 text files :


file1 consists of:


a 
b

file2 consists of:


a
ab
bc
c

output should be:


$p1.sh file1 file2 
a
ab
bc

how exactly would I code this in Linux?


More From » command-line

 Answers
5

I'm assuming the second example (p1.sh file2 file1) should output cc as there is no line with just c in file1. If so, then:


#!/bin/bash

cat "$1" | while read m
do
grep "$m" "$2"
done | sort -u

example:


$ cat f1
a
b
bb
b
cc
$ cat f2
aa
ab
bc
c
$ ./p1.sh f1 f2
aa
ab
bc
$ ./p1.sh f2 f1
cc

[#1191] Friday, January 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nnaisdio

Total Points: 369
Total Questions: 112
Total Answers: 108

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
nnaisdio questions
;