Sponsored Content
Full Discussion: generating line numbers
Top Forums Shell Programming and Scripting generating line numbers Post 302190114 by era on Monday 28th of April 2008 05:19:20 PM
Old 04-28-2008
Code:
cat -n "$file" | sed -e 's/^ *[1-9][0-9]*/&)/'

The regular expression matches start of line, zero or more spaces, and a nonzero positive integer, and sed replaces any match with whatever matched (& is the "magic" character which does this) and a right parenthesis.

Actually this is probably easier in awk.

Code:
awk '{ print NR ") " $0 }' "$file"

NR is the current line number and $0 is the input line.

Last edited by era; 04-28-2008 at 06:20 PM.. Reason: awk too
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing line numbers

Maybe this question is out there, but I searched and didnt see it. To print my files I use more filename | lpr -Pprinter I would like to print my scripts with line numbers. How do I do this? (2 Replies)
Discussion started by: MizzGail
2 Replies

2. Shell Programming and Scripting

generating random numbers with hamming distance 4

Hi I want to genrate 10 random 32 bit binary numbers with hamming distance 4 and 8. 11010110010101010101010101010101 11010110010101010100010101010010 if we look carefully at these two binary numbers they differ at 4 places hence hamming distance 4. Now I want to genrate these numbers... (2 Replies)
Discussion started by: hack_tom
2 Replies

3. UNIX for Advanced & Expert Users

Add line numbers to end of each line

Hi i would like to add line numbers to end of each line in a file. I am able to do it in the front of each line using sed, but not able to add at the end of the file. Can anyone suggest The following code adds line number to start of each line sed = filename | sed 'N;s/\n/\t/' how can i... (5 Replies)
Discussion started by: rudoraj
5 Replies

4. UNIX for Dummies Questions & Answers

Generating line number

Hi, I am generating a file through some Datastage commands: cat $TempDir/stage.txt |while read line do stagename=`echo $line` dsjob -llinks $proj $jobname $stagename 2>/dev/null >> $TempDir/LinkName.txt Now i have to assign the number... (5 Replies)
Discussion started by: Amey Joshi
5 Replies

5. Shell Programming and Scripting

Display Line numbers

Hi all experts, I am getting error in my shell scripts and i want to find out which lines is in errors. How i can display the line numbers . Is it NU command? Please give me some suggestions. (4 Replies)
Discussion started by: ma466
4 Replies

6. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

7. Shell Programming and Scripting

More with line numbers

Hi All, How to get line numbers when we more on a file in LINUX thanks firestar (1 Reply)
Discussion started by: firestar
1 Replies

8. Shell Programming and Scripting

Generating Gaussian Distributed Random Numbers

I want to generate an awk function that generated a Gaussian distributed set of random numbers. I need to implement the thing below in awk. Rnd is just a uniform random number between 0 and 1 function rgaussian(r1, r2) { Do v1 = 2 * Rnd - 1 v2 = 2 * Rnd - 1 ... (0 Replies)
Discussion started by: kristinu
0 Replies

9. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

10. Programming

generating pair of numbers in special order

I need some help in generating pair of numbers in orders using FORTRAN code. The order is like following. loop_1: 1,2 2,3 3,4 4,5 5,6 6,7 7,8 ..... until <= 2000 loop_2: 1,3 3,5, 5,7 7,9 9,11 11,13 ........until <= 2000 loop_3: 1,4, 4,7 7,10 10,13 13,17 ..... until... (3 Replies)
Discussion started by: vjramana
3 Replies
NL(1)								   User Commands							     NL(1)

NAME
nl - number lines of files SYNOPSIS
nl [OPTION]... [FILE]... DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -b, --body-numbering=STYLE use STYLE for numbering body lines -d, --section-delimiter=CC use CC for separating logical pages -f, --footer-numbering=STYLE use STYLE for numbering footer lines -h, --header-numbering=STYLE use STYLE for numbering header lines -i, --line-increment=NUMBER line number increment at each line -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one -n, --number-format=FORMAT insert line numbers according to FORMAT -p, --no-renumber do not reset line numbers at logical pages -s, --number-separator=STRING add STRING after (possible) line number -v, --starting-line-number=NUMBER first line number on each logical page -w, --number-width=NUMBER use NUMBER columns for line numbers --help display this help and exit --version output version information and exit By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are two delimiter characters for separating logical pages, a missing second character implies :. Type \ for . STYLE is one of: a number all lines t number only nonempty lines n number no lines pBRE number only lines that contain a match for the basic regular expression, BRE FORMAT is one of: ln left justified, no leading zeros rn right justified, no leading zeros rz right justified, leading zeros AUTHOR
Written by Scott Bartram and David MacKenzie. REPORTING BUGS
Report nl bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> Report nl translation bugs to <http://translationproject.org/team/> COPYRIGHT
Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
The full documentation for nl is maintained as a Texinfo manual. If the info and nl programs are properly installed at your site, the com- mand info coreutils 'nl invocation' should give you access to the complete manual. GNU coreutils 8.5 February 2011 NL(1)
All times are GMT -4. The time now is 09:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy