Posts Tagged ‘heaven and the earth’

Add line numbering to text file with prefix text on Linux with awk

Monday, March 25th, 2013

 

Recently I blogged a tiny article on how to add line numbering to ASCII text files with nl cmd. Today I needed to do the same line numbering, except I wanted to add a prefix text "R0" to each line number to match requirements of program which will later take use of .txt database file. I looked through nl (number lines) command manual but didn't find option with which I can insert a prefix string to each numbered line. I'm not a regular expression GURU, thus I asked for some help in irc.freenode.net on how to do it. Thanks to the kind guys in #bash I got it.

Here is how to add line numbering starting from 01onwards by adding a text prefix "R0":

$ awk '{ printf "R0%02d %s\n", NR, $0 }' text_file_to_number.txt > numbered_text_file_with_prefix.txt

The file to number in my case included a huge text file containing verses from Holy Bible, here is few lines from it before and after parsing and numbering with string prefix:

 

In the beginning God created the heaven and the earth.
                — Genesis 1:1
%
And the earth was without form, and void; and darkness was upon
the face of the deep. And the Spirit of God moved upon the face of
the waters.
                — Genesis 1:2
%
And God said, Let there be light: and there was light.
                — Genesis 1:3
 

 

R001 In the beginning God created the heaven and the earth.
R002            — Genesis 1:1
R003 %
R004 And the earth was without form, and void; and darkness was upon
R005 the face of the deep. And the Spirit of God moved upon the face of
R006 the waters.
R007            — Genesis 1:2
R008 %
R009 And God said, Let there be light: and there was light.
R010            — Genesis 1:3

 

 

To add any other prefix except R0 for example SAMPLE_PREFIX_STRING, substitute in above awk expression R0 with whatever expression;

 

awk '{ printf "SAMPLE_PREFIX_STRING%02d %s\n", NR, $0 }' text_file_to_number.txt > numbered_text_file_with_prefix.txt

Linux bash: Add line numbering to text file with NL – number lines command

Friday, March 22nd, 2013

Lets say you have a plainjt text TXT file with sentences and you would like to put quickly line numbering in a Linux bash shell. First thing that might pop up to your mind is to use a loop cat text file and print it line by one together with output of incremental values counted from 0 to the number of lines the file contains. This would work but is useless work. As coreutils package already contains a tool called nl ( /usr/bin/nl ). Here is paste from header of nl manual page;

 

NAME
       nl – number lines of files

SYNOPSIS
       nl [OPTION]… [FILE]…
 

Thus to put line numbering to lets say this fortune's bible quotes plain text file;

$ nl bible > bible_quotes_numbered.txt

Here is a small chunk of bible_quotes_numbered.txt;

 

$ cat bible_quotes_numbered.txt

 1  In the beginning God created the heaven and the earth.
     2                  — Genesis 1:1
     3  %
     4  And the earth was without form, and void; and darkness was upon
     5  the face of the deep. And the Spirit of God moved upon the face of
     6  the waters.
     7                  — Genesis 1:2
     8  %
     9  And God said, Let there be light: and there was light.
    10                  — Genesis 1:3
    11  %
    12  And God saw the light, that it was good: and God divided the light
    13  from the darkness.
    14                  — Genesis 1:4
    15  %
    16  And God called the light Day, and the darkness he called Night.
    17  And the evening and the morning were the first day.
    18                  — Genesis 1:5
    19  %
    20  And God said, Let there be a firmament in the midst of the waters,
    21  and let it divide the waters from the waters.
    22                  — Genesis 1:6
    23  %
    24  And God made the firmament, and divided the waters which were under
    25  the firmament from the waters which were above the firmament: and
    26  it was so.
    27                  — Genesis 1:7
    28  %
    29  And God called the firmament Heaven. And the evening and the morning
    30  were the second day.
    31                  — Genesis 1:8
    32  %
    33  And God said, Let the waters under the heaven be gathered together
    34  unto one place, and let the dry land appear: and it was so.
    35                  — Genesis 1:9
    36  %
    37  And God called the dry land Earth; and the gathering together of
    38  the waters called he Seas: and God saw that it was good.
    39                  — Genesis 1:10
    40  %
    41  And God said, Let the earth bring forth grass, the herb yielding
    42  seed, and the fruit tree yielding fruit after his kind, whose seed
    43  is in itself, upon the earth: and it was so.
    44                  — Genesis 1:11
 

 

It is worthy mentioning the same opeartion can be achieved with good old /bin/cat with cat -n, e.g.;

$ cat -n bible >bible_quotes_numbered.txt

 

On *BSDs nl command is not available, thus cat -n is very good to know of since it works for numbering ASCII plain text files on Free/Net/OpenBSDs