Page 1 of 1

Showing files with line numbers

Posted: 31 Jan 2012, 13:04
by viking60
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? :?:

Re: Showing files with line numbers

Posted: 31 Jan 2012, 17:31
by rolf

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

Re: Showing files with line numbers

Posted: 31 Jan 2012, 22:47
by viking60
: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