Showing files with line numbers

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Showing files with line numbers

Postby viking60 » 31 Jan 2012, 13:04

Often when I work with config files I like to have line numbers. This is useful for adding information in the right place and debugging scripts.
In arch; /etc/rc.config is a clear candidate.
So I do a:

Code: Select all

grep -n ^ /etc/rc.conf | less

Just replace the path and file with anything you like.
It will look like this

Code: Select all

1:#
2:# /etc/rc.conf - Main Configuration for Arch Linux
3:#
4:
5:# -----------------------------------------------------------------------
6:# LOCALIZATION
7:# -----------------------------------------------------------------------
8:#
9:# LOCALE: available languages can be listed with the 'locale -a' command
10:# HARDWARECLOCK: set to "UTC" or "localtime", any other value will result
11:#   in the hardware clock being left untouched (useful for virtualization)
12:# TIMEZONE: timezones are found in /usr/share/zoneinfo
13:# KEYMAP: keymaps are found in /usr/share/kbd/keymaps
14:# CONSOLEFONT: found in /usr/share/kbd/consolefonts (only needed for non-US)
15:# CONSOLEMAP: found in /usr/share/kbd/consoletrans
16:# USECOLOR: use ANSI color sequences in startup messages
17:#
18:LOCALE="nb_NO.UTF-8"
19:HARDWARECLOCK="UTC"
20:TIMEZONE="Europe/Oslo"
21:KEYMAP="no-latin1"
22:CONSOLEFONT="GohaClassic-12"
23:CONSOLEMAP=

It's nice +1
But we can go on debugging our file: After having identified the lines that we have made a lot of changes to we can list only those line numbers:

Code: Select all

sed -ne '10 p' -e '20 p' /etc/rc.conf

Here I will only get a listing of line 10 and 20 of the file /etc/rc.conf. Making the work a lot easier.

Code: Select all

# HARDWARECLOCK: set to "UTC" or "localtime", any other value will result
TIMEZONE="Europe/Oslo"


Now this is utterly useless you say? Because you most likely will have to view more than 2 lines - even if it is brilliant that I can extract lines from different places in the file.
Fear not; you can replace 10 p in the example above with
10,20 p - to list all files between line 10 and 20
or
10,$ p - To list everything from line 10 to the end of file.

In this case we do not need the -e switch for line 20 anymore so we can remove it (we don't have to, but you would get line 20 twice) like this:

Code: Select all

sed -ne '10,$ p'  /etc/rc.conf

Will show everything from line 10 to EOF

Code: Select all

sed -ne '10,20 p'  /etc/rc.conf

Will show lines 10 to 20.
And so on. :greetings
And now to the Einstein competition based on this: How can I print out line 10,20 and 30 from my file? :?:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
rolf
Guru-Berserk
Posts: 1107
Joined: 16 Mar 2010, 16:07

Re: Showing files with line numbers

Postby rolf » 31 Jan 2012, 17:31

Code: Select all

$ sed -ne '10 p' -e '20 p' -e '30 p' /etc/rc.conf | lpr -P MFC620CN


seems to be a form that works, here. -P option to lpr specifies the printer to use, which can be got with:

Code: Select all

$ lpstat -a

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Showing files with line numbers

Postby viking60 » 31 Jan 2012, 22:47

:bowlol Image
That is almost cheating - Gurus always know the answer :-D
adding the -e switches is correct of course. And that printer stuff is probably perfect too. I did say print but I was only thinking of showing it in the terminal.
So I would have settled for

Code: Select all

sed -ne '10 p' -e '20 p' -e '30 p' /etc/rc.conf

Mea culpa and new knowledge - perfect result +1
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Tips & Tricks”