Rename a group of files based on a regex

Vedi la faq in italiano Suppose you want to rename the name of a group of files that have the following template: file1_tmp.sh file2_tmp.sh … file99_tmp.sh and suppose you want to remove the ‘_tmp’ middle part in all the files. We can use the command “rename” or “prename”: rename ‘s/_tmp//’ *.sh  

Just print line 25 of a file

Vedi la faq in italiano Suppose you only want to print a single line of a file, which for convenience is indicated by line 25 perl -ne ‘$. == 25 && print && exit’ file.txt Of course if we also want to print lines 31 and 57 perl -ne ‘($. == 25 || $. == Continue Reading

Encode/decode a file or string in Mime Base64

Vedi la faq in italiano Suppose you have a “test encode” string and a “prova.txt” file you want to encode in Mime base64. To solve the problem we can simply launch the command line: perl -MMIME::Base64 -e ‘print encode_base64(“test encode”)’ perl -MMIME::Base64 -0777 -ne ‘print encode_base64($_)’ prova.txt For the reverse operation just do: perl -MMIME::Base64 Continue Reading

Number a text file

Vedi la faq in italiano Suppose we have a text file (prova.txt) that we want to number. We can use: perl -pe ‘$_ = “$. $_”‘ prova.txt If you want to simulate the output of cat -n prova.txt just use: perl -ne ‘printf “% 5d %s”, $., $_’ prova.tx To remove the numbering we can Continue Reading

Extract all public GPG keys from a file

Vedi la faq in italiano Suppose you have a file named “file-with-keys-gpg” and want to extract it by deleting any other line in the file. perl -ne ‘print if /—–BEGIN PGP PUBLIC KEY BLOCK—–/../—–END PGP PUBLIC KEY BLOCK—–/’ file-with-keys-gpg Of course by extrapolating the logic we might want to print all the lines between two Continue Reading