Producing a file C.txt that contains common lines between A.txt B.txt using grep

Problem

You have two files (A.txt, B.txt) that contain records (one line each), and you want to find out the common lines between the two.

For example you have the following two files.

A.txt

one
two
three

and B.txt

two
six
seven

and you want to find or produce a new file (C.txt) that contains only the common record (two) from the two files.

Solution

You can use grep with the -Fx options like

grep -Fxf A.txt B.txt > C.txt

that will produce file C.txt that contains the common line (two) from the two files.

The options for grep have the following meaning:

  • -F: Interpret the pattern as a list of fixed strings (instead of regular expressions).
  • -x: Only match whole lines.
  • -f fileA.txt: Read the patterns from fileA.txt.

Changing mail sending from command line from mail(x) to s-nail

Problem

It seems that the default email client in Fedora 40 is s-nail instead of mailx in Fedora 39, and trying to send an email from the command line does not work anymore.

Solution

The two changes required in order for the command to work with s-nail are:

  • Use spaces instead of commas in the recipients, and/or in the cc_recipients lists
  • When you use attachments (-a flag) and cc_recipients (-c flag), the -a flag should be before the -c flag, otherwise:
s-nail: -a is an invalid alias name

So the full command can be like the following:

echo $body | mail -s "Reports for $yesterday" -a $report_path_a -a $report_path_b -c $cc_recipients $recipients

failed to create fsnotify watcher: too many open files

This is quite possibly caused by one of the limits set too low. It is common when using promtail (with Loki for example) to tail log files.

One of the ways to get over this is to increase the value (in this example max_user_instances) either for the session or by making the change permanent by adding to a file (/etc/sysctl.conf).

For testing and doing it for the session, login to the affected server and do the following

ubuntu@server:~$ cat /proc/sys/fs/inotify/max_user_instances 
128
ubuntu@server:~$ sudo sysctl fs.inotify.max_user_instances=8192
fs.inotify.max_user_instances = 8192
ubuntu@server:~$ cat /proc/sys/fs/inotify/max_user_instances 
8192

Convert a single line string ‘a b c’ to a multiple line string with double quotes and commas like [“a”, “b”, “c”] using bash and sed

Problem

You have single line output that contains multiple strings (could be output from a kubectl that gives all the instance names) and you want to convert it to text that can be used as the a variable enclosed by double quotes, one string on one line and separated by commas.

So having the string in a file called input.txt:

a b c

you want to convert it to the following and save it in another file called output.txt

"a",
"b",
"c"


Solution

You can use the following:

cat input.txt | tr ' ' '\n' | sed 's/^/"/g' | sed 's/$/",/g' > output.txt

which will first replace the spaces separating the strings to newlines, and then use two passes with sed, in the first adding the first quote, and the second adding the second quote and the comma.