[Linux] belki ihtiyaci olan cikar

---------

New Message Reply About this list Date view Thread view Subject view Author view

From: Murat SULUHAN (murat.suluhan@tesam.com.tr)
Date: Tue 19 Jun 2001 - 15:01:24 EEST


Adding Line Numbers to a File
By S. Lee Henry

You can easily add line numbers to the contents of a file in several
different ways; we're going to look at several of them this week.
Depending on what you are trying to accomplish, one of these methods
might be right for you.

The vi command (set number) is one way to add line numbers to a file.
If you are a vi user and only need line numbers while you are working
on the file, then this may fulfill your needs. You can achieve the same
results in other editors as well but, obviously, using a different
command. In vi, after adding numbers to a file, the lines will look
like this:

      1 No farms
      2 No food
      3
      4 Think about it

To permanently insert line numbers, you can use one of the following
tricks.

awk
The awk command knows about line numbers. In fact, awk maintains a
special variable called NR that stores the number of each line. Since
awk works on files one line at a time, it's only natural that it tracks
the line it is currently processing, so the variable is available and
current. A command like the following will print each line of a file
preceded by its line number:

    % cat myfile | awk `{print NR, $0}'
    1 No farms
    2 No food
    3
    4 Think about it

In this command, awk reads each line and prints the record number (or
line number) and the complete line ($0). Clearly, you can direct the
output to another file or reduce the command to a single system call,
as shown here:

    % cat myfile | awk `{print NR, $0}' > myotherfile
    % awk `{print NR, $)}' < myfile > myotherfile

grep
You can also add line numbers with grep by using the -n option. A grep
command of the following type will add line numbers:

    % grep -n . myfile > myotherfile

Notice that the empty line is missing:

    $ grep -n . abc
    1:No farms
    2:No food
    4:Think about it

This grep command matches any text in the file (the "." character
matches anything), but it will NOT match nothing (i.e., the empty line).

Paste
The paste command combines the contents of two files, line-by-line. For
example, one file's contents are:

    1
    2
    3
    4
    5
    6
    7

A second file contains:

    No farms
    No food

    Think about it

Paste will combine the contents of the first line, then the second
line, and so on. The output will look like this:

    1 No farms
    2 No food
    3
    4 Think about it
    5
    6
    7

As shown in the example above, if one of the files to be pasted has
more lines than the other, then the resulting file will contain the
combined lines followed by the lines that could not be combined. Unlike
join and merge commands, the contents of files to be pasted do not have
to be sorted first and do not have to share any common content to be
combined. The line numbering method that works best for you depends on
what you're trying to accomplish and your Unix style.

 ____________________________________
|
|
| Murat SULUHAN
| GLOBALSTAR - TE.SA.M. T.U.R.K. A.Ş.
|
|____________________________________

-----------------------------------------------------------------------
Liste üyeliğiniz ile ilgili her türlü işlem için
http://liste.linux.org.tr adresindeki web arayüzünü kullanabilirsiniz.

Listeden çıkmak için: 'linux-request@linux.org.tr' adresine,
"Konu" kısmında "unsubscribe" yazan bir e-posta gönderiniz.
-----------------------------------------------------------------------


New Message Reply About this list Date view Thread view Subject view Author view

---------

Bu arsiv hypermail 2b29 tarafindan uretilmistir.