Score:-1

How do I write a bash script that prints every line of one file also in second file

us flag

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?

lnee avatar
td flag
First im a bit confused can you tell me why you wont this for context
nobody avatar
in flag
I think, you have wrong result for the second case `$p1.sh file2 file1`. The answer should be `cc` and not `c`. Right?
Score:2
in flag

It can be done with grep only

grep -f file1 file2
grep -f file2 file1
Score:0
bd flag

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
nl34 avatar
us flag
thank you! could you explain the logic in this code please
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.