[sed] Print all the lines in between two patterns


, , , , , ,

The following CLI one-liner prints all the lines between the lines containing PATTERN1 and PATTERN2 (including the lines with these two patterns) in a file, FILENAME:


[unix] Send mail from the command line using mail and sendmail commands



1. If the message isn’t too long then type the following in the commandline and hit enter

$ mail -s "Subject of the email" -c cc_recipient@domain1.com -b bcc_recipient@domain2.com main_recipient@domain3.com -- -F"Sender Name" -fSender@domain4.com

Then keep typing your message. To send it either enter a blank line with just a single period (“ . ”’) in it and hit enter again, or just hit control+d. That’s it! A email to main_recipient@domain3.com has been sent with a carbon copy (cc) to cc_recipient@domain1.com and a blind carbon copy (bcc) to bcc_recipient@domain2.com. The recipients will see the message from “Sender Name” who has email address Sender@domain4.com.

The sendmail flags (-f and -F) come after the -- sign. Notice that there is no space between the sendmail flags and the corresponding arguments.

Needless to say, you may skip the cc and bcc fields.

2. If the message is fairly long you may want to compose it in a text file, say message.txt, using a convenient editor. Send the email with the body of the email as the text in message.txt as follows

$ mail -s "Subject of the email" -c cc_recipient@domain1.com -b bcc_recipient@domain2.com main_recipient@domain3.com -- -F"Sender Name" -f sender@domain4.com < message.txt

3. In order to send a file as attachement (say, attachment.pdf) use the following

$ uuencode attachment.pdf  attachment_name_that_the_reciepents_will_see.pdf | mail -s "Subject of the email" -c cc_recipient@domain1.com -b bcc_recipient@domain2.com main_recipient@domain3.com -- -F"Sender Name" -f sender@domain4.com < message.txt

Update: If -F and -f flags are used together only -f and its argument are used. Weird!

[vim] Show and delete all lines that contain a certain pattern


, , , , , ,

1. Show all the lines that match a pattern, say PATTERN

Use “g!” or “v” for negative matching. For example, to show all the lines that DO NOT match the pattern, PATTERN, use

2. Delete all the lines that contain the pattern, PATTERN

Again, g! or v may be used for negative matching.

Application: You may use any of the following two commands to delete all the blank lines (including lines containing only whitespaces) from a file

Reference: Vim Wiki.

[c++] Parse numbers of different types given as key-value pairs from a file


, , , , , , , , , ,

The Problem: I have the following file (say, “inp.txt”) that contains some data and I want to read the values of different types in a c++ code.

num_1 = 123
num_2 = 10.9e-2

I put the variable names in order to remind myself to assign the right values to the right variables.

Solution: One way to do this is to use an istringstream instance. Here’s a sample code:

// Parse input given as key = value (values may be of different type)
#include <sstream>
using namespace std;

int main(){

    // Input stream
    ifstream InpStream("inp.txt");

    // We want to get parsed num_1 and num_2
    int num_1;
    double num_2;

    // Define strings to hold the key and corresponding value
    string key, value;

    // Read the input file using getline ('=' is the delimiter)
    while ( getline(InpStream, key, '=') ) {

        // Print what key is read
        cout << "key read: " << key << endl;

        // Create an istringstream instance to parse the key
        // (string to sring conversion)
        istringstream ss_key(key);
        ss_key >> key;

        // Now read the value and print what is read (value is a string)
        getline(InpStream, value);
        cout << "value read: " << value << endl;

        // Create another istringstream instance to parse the value
        istringstream ss_value(value);

        if ( key == "num_1") {
            // string to integer conversion
            ss_value >> num_1;
            cout << "Parsed num_1: " << num_1 << endl;
        else if (key == "num_2") {
            // string to double conversion
            ss_value >> num_2;
            cout << "Parsed num_2: " << num_2 << endl;
        cout << endl;
    return 0;

Here’s the output

key read: num_1
value read: 123
Parsed num_1: 123

key read: num_2
value read: 10.9e-2
Parsed num_2: 0.109

Reference: DaniWeb, Cplusplus.

[vim] Fold lines in vim


, , , ,

These are the vim folding commands that “fold” certain lines in the file in order to help you focus at certain part of it:

zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero — all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

Ex like range commands also work: e.g, “:10,20 fold” will fold lines 10 through 20.

You may use the following in your ~/.vimrc file to automatically fold lines when you indent them (for example in a program file):
set foldmethod=indent

Reference: Linux.com. You may also find the vim documentation for folds using “:help foldmethod” while you are looking at a file using vim.