LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Line numberimg (https://www.linuxquestions.org/questions/linux-software-2/line-numberimg-281213/)

fiomba 01-23-2005 10:22 AM

Line numberimg
 
Hi,
is there any more clever way of line numbering, than using nl or cat -n + sed, if I want left trimmed numbers?
Quote:

dcop | sort | nl | sed 's/ //' | sed 's/\t/ /'
Explanation:
dcop (for who does not know) = Desktop Communication Protocol, that is, the KDE tool for
...............................................data interchange among KDE applications
sort | nl ............................... = ordered and line numbered output
sed 's/ //' ............................ = left trim of 4 blanks
sed 's/\t/ /' ........................... = substitution of the tab with a blank.

Is there any shorter command?

homey 01-23-2005 10:37 PM

It looks like nl works pretty good alone...
nl file.txt

fiomba 01-24-2005 05:37 AM

What a pity that "nl" does not trim line numbers...
I don't want an output like this:
Quote:

command: dcop | sort | nl
-----1--kaccess
-----2--kded
-----3--kdesktop
-----4--kdf-4667
-----5--khotkeys
-----6--kicker
-----7--klauncher
-----8--kmix
-----9--knotes
----10--knotify
----11--konsole-4843
----12--kopete
----13--korgac
----14--ksmserver
----15--kwin
----16--kwrited
----17--randr
but...
Quote:

dcop | sort| nl | sed 's/----//' | sed 's/\t/-/'
-1-kaccess
-2-kded
-3-kdesktop
-4-kdf-4667
-5-khotkeys
-6-kicker
-7-klauncher
-8-kmix
-9-knotes
10-knotify
11-konsole-4843
12-kopete
13-korgac
14-ksmserver
15-kwin
16-kwrite-4953
17-kwrited
18-randr

homey 01-24-2005 09:02 AM

Well I guess I'm missing something here...
This is what I get using FC3
Code:

# dcop | sort | nl
    1  kded
    2  kdesktop
    3  khotkeys
    4  kicker
    5  kio_uiserver
    6  klauncher
    7  klipper
    8  kmix
    9  knotify
    10  konqueror-5254
    11  konsole-5355
    12  kscd
    13  ksmserver
    14  kwin
    15  noatun
#


fiomba 01-24-2005 12:46 PM

... and 'voila' what I don't want!
Don't you see that my listy is left trimmed taking into account the highesrt number?

fiomba 01-25-2005 08:48 AM

I have the solution! (from an another forum's guru)
Quote:

dcop | sort | nl -s \ | cut -c 5-
from the man nl:
Quote:

-s \x = add STRING after line number (in our case x is a blank)
To left trim you must use
Quote:

cut -c 5-
Cut let pass from the fifth character onward, eliminating in this way the first 4 blanks (left trim)

fiomba 10-04-2005 06:59 PM

Hi all,
I am critically ri-considering my old posts, giving an answer (if I know!) in the hope to be of help to
some newbie (as I was some times ago).
To be sincere in many arguments I am still a newbie, but anyway...

About the item of line numbering, I have found that nl has all I need.

The default nl is right padding line numbering up to 6 characters and, besides that, it inserts
a tab between the number and the displayed line:
Quote:

000001 NL(1) User Commands NL(1)
000002
000003 NAME
000004 nl - number lines of files
000005
000006 SYNOPSIS
000007 nl [OPTION]... [FILE]...
000008
000009 DESCRIPTION
000010 Write each FILE to standard output, with line numbers added.
000011 With no FILE, or when FILE is -, read standard input.
000012
000013 Mandatory arguments to long options are mandatory for short options too.
000014
000015 -b, --body-numbering=STYLE
000016 use STYLE for numbering body lines
000017
000018 -d, --section-delimiter=CC
000019 use CC for separating logical pages
000020
000021 -f, --footer-numbering=STYLE
000022 use STYLE for numbering footer lines
000023
000024 -h, --header-numbering=STYLE
000025 use STYLE for numbering header lines
000026
000027 -i, --page-increment=NUMBER
000028 line number increment at each line
000029
000030 -l, --join-blank-lines=NUMBER
000031 group of NUMBER empty lines counted as one
000032
000033 -n, --number-format=FORMAT
000034 insert line numbers according to FORMAT
As you can see I have not used nl alone, but
Quote:

nl <file> -n rz -b a
where:
"-n rz" inserts line numbers according to format rz (right justified, leading zeros)
"-b a" numbers "all" lines (also the empty ones)
I quoted the "leading zeros" version, only to better show the effect of trimming.

Such trimming can be easily obtained by adding:
Quote:

-w <n>
For example, with "-w 3" 000034 --> 034
The last problem is the separator between line numbering and the line itself:
Quote:

use the option -s 'x'
As a summary, the complete command for the line numbering
I wanted in my original post is:
Quote:

nl <file> -n rz -b a -w 3 -s " "
001 NL(1) User Commands NL(1)
002
003 NAME
004 nl - number lines of files
005
006 SYNOPSIS
007 nl [OPTION]... [FILE]...
008
009 DESCRIPTION
010 Write each FILE to standard output, with line numbers added.
011 With no FILE, or when FILE is -, read standard input.
012
013 Mandatory arguments to long options are mandatory for short options too.
014
015 -b, --body-numbering=STYLE
016 use STYLE for numbering body lines
017
018 -d, --section-delimiter=CC
019 use CC for separating logical pages
020
021 -f, --footer-numbering=STYLE
022 use STYLE for numbering footer lines
023
024 -h, --header-numbering=STYLE
025 use STYLE for numbering header lines
026
027 -i, --page-increment=NUMBER
028 line number increment at each line
029
030 -l, --join-blank-lines=NUMBER
031 group of NUMBER empty lines counted as one
032
033 -n, --number-format=FORMAT
034 insert line numbers according to FORMAT
Of course the padding depends on the number of lines of the file.
Bye.


All times are GMT -5. The time now is 09:24 PM.