Showing files with line numbers
Posted: 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:
Just replace the path and file with anything you like.
It will look like this
It's nice
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:
Here I will only get a listing of line 10 and 20 of the file /etc/rc.conf. Making the work a lot easier.
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:
Will show everything from line 10 to EOF
Will show lines 10 to 20.
And so on.
And now to the Einstein competition based on this: How can I print out line 10,20 and 30 from my file?
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
It's nice

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.confWill show everything from line 10 to EOF
Code: Select all
sed -ne '10,20 p' /etc/rc.confWill show lines 10 to 20.
And so on.
And now to the Einstein competition based on this: How can I print out line 10,20 and 30 from my file?