🛠️ Mastering Linux: Extracting the 10th Line

December 12, 2024

The current image has no alternative text. The file name is: tux.svg_.png

Today, I had a technical interview with a company, and a Senior Linux System Administrator asked me a question about a Linux command that really made me think.

The question was: “How do you show only the 10th line of a file?”

At first, the question seemed easy. I began with the command:

$ head -n 10 file_name

The command outputted the first 10 lines of the file. However, then came the challenge: how to extract only the 10th line from that file? I thought about using the tail command, but at that moment, I couldn’t piece together the solution. I kept pondering over it, and after the interview, I did a quick search and found three 🕵️‍♂️ elegant solutions.

🔢 Solution 1: Using awk

You can use the awk command with a condition to print only the 10th line:

$ awk -v line=10 'current_line==line' file_name

Here, current_line refers to the current line number, line=10 is specified and then the command should print only the 10th line or the line number specified on line variable.

🔢 Solution 2: Using sed

The sed command also provides a straightforward way to achieve this:

$ sed -n '10p' file_name

The -n flag suppresses the default output, and 10p tells sed to print only the 10th line of the file_name.

🌐 Solution 3: Combining head and tail

This approach I liked most combines head and tail:

$ head -n 10 file_name | tail -1

Here, head -n 10 extracts the first 10 lines, and tail -1 selects only the last line from those, which is the 10th line of the file.

I really would like to thank the Senior Linux System Administrator for making this question.

Enabling Automatic Identation on Paste

April 16, 2024

Working with legacy code can be a daunting task. Often, a seemingly minor issue like a missing indentation can render your code dysfunctional. But fear not! There’s a simple solution to this problem, and it doesn’t involve installing any additional plugins. By leveraging a built-in feature of Visual Studio Code (VSCode), we can streamline our workflow and avoid such pitfalls. Follow the steps below to enable this functionality:

  • Open Visual Studio Code
  • Click on the cog icon
  • Click on settings
  • On the field “Search settings”, search for “editor.formatOnPaste“
  • Mark the option “Format On Past“

With auto indentation enabled, you can breathe easier knowing that your code will stay neatly formatted, making it easier to read and maintain. Say goodbye to indentation woes and hello to smoother coding sessions in VSCode!

How to make VIM Text Editor More Productive

October 14, 2022

Today reading the linux magazine and I found out a very nice article (https://www.linux-magazine.com/Issues/2021/251/Vim-Primping) about how to improve vim text editor.

After I start using Visual Studio Code I start missing some functions on vim such as auto indentation and the highlights of the corresponding brackets.

To make this happens you should edit a file .vimrc located in you /home/homedir/.vimrc (~/.vimrc) and add the following parameters:

edit the .vimrc in your home dir
add the parameters

After make the changes, you only need to edit the files and enjoy the improvements 🙂 have fun!

Missing NTSYSV command

July 18, 2016

I am looking for alternatives for the ntsysv command, once after the introduction of Systemd on Fedora, the #ntsysv doesn’t do all the job anymore.

Suggestions are welcome.

systemctl yyy.service SUCKS

March 6, 2012

I really didn’t like to don’t find the service commands on my Fedora 16 !

Service blablabla status/start/stop/restart; now I was FORCED to use systemctl yyy something.service. The ntsysv doesn’t work because of this systemctl; I think that the effort made to change this stuff could be better used in another projects more important for the project.

New Release Fedora 15 :-)

May 30, 2011

I can’t wait for the release of the upgrade package to upgrade my Fedora 14 to 15 :-). During the last two releases I used the upgrade method to keep my box updated and it worked very well. I didn’t have any problem, it just work perfectly !!! Congratulations for the guys whom made this tool.

Fedora project: http://fedoraproject.org/

 

Cool “OLD” Way to Restart RSyslog – Fedora 11

July 23, 2009

#kill -HUP `cat /var/run/syslogd.pid`

Its the same as

#service rsyslog restart

But in a different way

There are another signals, each one can be represented by a number, here is a small list of signals available:

  • HUP = 1
  • KILL = 9
  • TERM = 15

I realized that it did not change the PID of the process and I got an interesting comment about that saying that rsyslog v5 does not support HUP anymore.

sangoku

Compiling the Linux Kernel – CentOS 5.3

July 21, 2009

Steps:

#yum -y  install kernel-devel kernel-headers

#yum -y groupinstall “Development-Tools”

#cd /usr/src

#wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.30.2.tar.bz2

#tar -jxvf linux-2.6.30.2.tar.bz2

#cd /usr/src/linux-2.6.30.2/

#make menuconfig

#make clean

*#make dep (is no more necessary)

#make bzImage

#make modules

#make modules_install

#make install

*make install will copy the files: System.map, vmlinuz and the initial ram disk initrd-2.6.30.2.img to /boot. It will also add the lines you will need on grub, now just restart your computer and choose the new kernel on the grub screen.

Have Fun !!!

centos

Installing Microsoft Fonts on Fedora 11 x64

July 10, 2009

Follow the steps:

1) Install some dependencies using yum

#yum -y install rpm-build cabextract ttmkfdir xorg-x11-xfs

Paul Frields comment that he made a better spec, available here which does no require the chkfontpath dependence.

2) Installing the last dependence not available by yum

#rpm -ivh http://dl.atrpms.net/all/chkfontpath-1.10.1-2.fc9.i386.rpm

3) Making the msttcorefonts rpm

  • Download the file http://corefonts.sourceforge.net/msttcorefonts-2.0-1.spec to the folder ~/rpmbuild/SPECS/
  • cd ~/rpmbuild/SPECS/
  • rpmbuild -bb msttcorefonts-2.0-1.spec
  • rpm -ivh ~/rpmbuild/SRPMS/msttcorefonts-2.0-1.src.rpm
  • rpm -ivh ~/rpmbuild/RPMS/noarch/msttcorefonts-2.0-1.noarch.rpm

Now just open your openoffice the have fun with the new fonts(TImes new roman, verdana, etc …)

[1] http://corefonts.sourceforge.net/

logo_fedoralogo

Vista SP1 32 bits = Nightmare

June 25, 2009

Last week I got a new hard drive, bigger faster, until this point everything was ok. I backup all of my data on an external drive. Then I decided to install everything (Vista and Fedora) again in my dual boot system.

I installed both operational systems, restored  my backup, installed all the programs I like to use until Vista asked me to install the update “Vista Service Pack 1 – SP1”, bad decision 😦 Thinking in security I accepted the update, then I got the first error. Looks not usual, then Vista asked me “try again?”  Then mistakenly I clicked in this button 😦 bad for me. After this update my windows stop working and did not allowed me even to start my system.

Then I got the recovery disks that come with my laptop, bad disks these ones. They erased all of my partitions erasing the bugged Windows Vista, but as a bonus erasing my Linux system that until this moment has worked with perfection in my system.

Now time to install everything again and again and disabled the automatic updates from Vista, from now all Vista updates will be verified one by one before I install then again.

vista-logo


Create your own website, free on WordPress.com.Create your own website.
Start free