LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-23-2005, 10:22 AM   #1
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
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?
 
Old 01-23-2005, 10:37 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
It looks like nl works pretty good alone...
nl file.txt
 
Old 01-24-2005, 05:37 AM   #3
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
Old 01-24-2005, 09:02 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
#
 
Old 01-24-2005, 12:46 PM   #5
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
... and 'voila' what I don't want!
Don't you see that my listy is left trimmed taking into account the highesrt number?
 
Old 01-25-2005, 08:48 AM   #6
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
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)
 
Old 10-04-2005, 06:59 PM   #7
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
ADSL Router line 1 Mandrake 10. Line 2 is win xp pro virtthemaest Linux - Networking 0 08-05-2004 12:35 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration