Visit The New, Modern Unix Linux Community


add a number to the beginning of every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add a number to the beginning of every line
# 1  
add a number to the beginning of every line

hey,

i would like to add a line number to the beginning like so:

Code:
red 
blue
green
yellow

will be:
Code:
1=>red 
2=>blue
3=>green
4=>yellow

please advise
thank u.
# 2  
What Operating System and version you you have?
What Shell or Programming Language do you use?

Do you actually want the characters ">=" in the output?
How many records? Unix Shell has trouble counting above 2^23 .
# 3  
i

thank u for your reply
i use red hat i think it's the latest version
using perl ,sed, awk , bash or nl will be fine
and yes i do want ">=" included
# 4  
Unix provided a nice utilty called nl (number line) see if that could help
you. If not see if this works for you

Code:
 
cat xx.data
 
red
hello world
green
blue ribbon winner

 
cat xx.ksh
 
#!/bin/ksh
let lineno=0
while read dataline
do
   let lineno=lineno+1
   echo "$lineno=> $dataline"
done < ./xx.data

 
./xx.ksh
 
1=> red
2=> hello world
3=> green
4=> blue ribbon winner

This User Gave Thanks to BeefStu For This Post:
# 5  
See if your version of nl supports the -s option:
Code:
nl -s '=>' myfile > mynewfile

IT should have that option, because -s is part of the POSIX standard.
This User Gave Thanks to jim mcnamara For This Post:
# 6  
The Perl way.
Code:
perl -pe 'BEGIN{$i=1}s/^/$i=>/;$i++' inputfile.txt

This User Gave Thanks to balajesuri For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #475
Difficulty: Medium
A Yottabyte (YB) equals 1,208,925,819,614,629,174,706,176 bytes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies

2. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

3. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

4. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

5. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

6. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

7. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

8. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

9. Linux

Add file's date at beginning of every line in file

How would I do that? /Rutger (6 Replies)
Discussion started by: rutgerblom
6 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies

Featured Tech Videos