OpenSource For You

Tips & Tricks

-

Converting a CSV file to a DSV file using the column and cat commands

To understand how to convert a CSV file to a DSV file, let’s look at a few examples of ESS.csv.

ESS.csv is a comma-separated file; its left and right enclosure is ‘“’ and the field separator is ‘,’.

Example 1: To convert the ESS.csv file to a delimited file (i.e., dsv) using the column command, try the following command:

cat ESS.csv | column -t -s ‘”’ -o ‘’ | column -t -s ‘,’ -o ‘;’ > ESS.dsv

The above command changes the left and right enclosures from ‘“’ to none and the field separator is ‘;’.

You can customise the left enclosure as per your requiremen­ts.

Example 2: Change the left and right enclosures from ‘“’ to ‘‘’ and the field separator is ‘;’.

cat ESS.csv | column -t -s ‘”’ -o ‘’’ | column -t -s ‘,’ -o ‘;’ > ESS.dsv

Example 3: Change the left and right enclosures from ‘“’ to ‘:’ and the field separator is ‘;’.

cat ES.csv | column -t -s \’\’\’ -o ‘:’ | column -t -s ‘,’ -o ‘;’ > ES.dsv

Example files are available on GitHub at https://github.com/hemanth22/CSVToCusto­mDSV and at the GitHub Wiki guide https://github.com/ hemanth22/CSVToCusto­mDSV/wiki/Guide-to-useCSVToCu­stomDSV. To download an example, use the following command:

git clone https://github.com/hemanth22/CSVToCusto­mDSV.git

—Hemanth Bitra, hemanthbit­ra@bitroid.in

Resetting a forgotten root user account password in CentOS 7/RHEL

We can easily reset the root user account by booting into the single mode using the following steps.

Step 1: While booting the system, you see a screen with two options as shown below:

CentOS Linux, with Linux 3.10.0-123.el7.x86_64 CentOS Linux, with Linux 0-rescue-27ca624e1d­5b47ad97bb­a2bbc 648d347

Use the arrow keys as soon as you see the screen with the above two options so that a time-out does not take place. Now select the first option and press the ‘e’ key to edit.

Step 2: You will be presented with the kernel parameters’ screen, and you may have to use the arrow key to see the line where the ‘vmlinuz’ mount point is set. Here, search for the text ‘rhgb quiet’ and replace this text with ‘init=/bin/bash’ without quotes, and press Control+x to boot the system with this edited configurat­ion.

Step 3: After booting, you will see what’s shown below:

bash-4.2#

You will directly get the bash shell with root privileges where you can reset the root password using the ‘passwd’ command as shown below:

bash-4.2# passwd

You can successful­ly reset the root account password in this way.

—Sharmin Shaikh, imsharmeen@gmail.com

Never miss the Ubuntu notificati­on ‘system restart required’

We must reboot the server when we install a new kernel or libraries. Ubuntu Linux can tell you if the system needs a reboot when you login as root user.

*** System restart required ***

The above warning will appear on the login screen, but we may miss this warning. So here is the simple script for administra­tors to find out exactly when the system requires a reboot.

You can manually find the file /var/run/rebootrequ­ired, which indicates that a system restart is required.

Instead of a manual search, you can use the following script to identify the Ubuntu notificati­on of ‘system restart required’ at every login and never miss the warning.

Add the following lines in the file /root/status.sh: #!/bin/bash if [ -f /var/run/reboot-required ] then echo -e “33[33;31m[*** Hello $USER, you must reboot your machine ***]33[0m” else echo -e “33[33;32m\n*** System reboot is NOT required now ***\n`cat /var/lib/updatenoti­fier/updatesava­ilable`\ n33[0m” fi

Now we can add the file path /root/status.sh in / root/.bashrc file to check for the ‘Reboot required’ task at every login made by the root user.

# echo “/root/status.sh” >> /root/.bashrc && tail -n3 / root/.bashrc

—Ranjithkum­ar T, ranjith.stc@gmail.com

Multiple tips for daily use

1. Copy from or paste into the terminal in Ubuntu Many people try to paste into the terminal by using Ctrl+v. But this will not work. We need to use Ctrl+Shift+v to paste into the terminal. Similarly, to copy from the terminal, select the text to be copied and use Ctrl+Shift+c.

This is the simplest and easiest way for new users to overcome the problems of copying and pasting into the terminal.

2. Using ‘tac’ to output the contents of a file instead of ‘cat’

Many people use the ‘cat’ command to output the contents on the terminal. If the contents are huge, then users have to scroll up till they reach the first line of the content. Instead of scrolling all the way up till the beginning and scanning, users can replace ‘cat’ with ‘tac’. The latter works just like the ‘cat’ command — the only difference is that the results are printed in reverse order; i.e., the first line comes last and the last line appears first. So users can easily see the first line without scrolling all the way to the beginning.

3. Getting to know the permission­s of files using the ‘lsl’ command on the terminal in Ubuntu

One can get to know the permission of any file in the system by executing the ‘ls-l’ command on the terminal. The permission­s are shown in the first column. r-read, w-write and x-execute are permission­s for different users, i.e., the first three characters are to show permission­s of the owner on that file, the next three are for group permission­s and the last three are for others’ permission­s on that file. There will be an initial ‘-’ before listing permission­s of that file. So users can get to know who all can access the file that they themselves are using or going to.

As an example, -rwxrw-r-- tells us that there is read, write and execute permission­s for the owner; read and write permission­s for the group; and only read permission for others, on the file.

—Anirudh Kalwa, anirudh.3194@gmail.com

 ??  ??
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India