|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Adding a column to a text file with row numbers
Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: Code:
A X B Y C D Output: Code:
A X 1 B Y 2 C D 3 |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
What have you tried?
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Try like... Code:
sed "=" test1.txt | sed "N;s/\n/ /" |
|
#4
|
||||
|
||||
|
On your path this would put the line numbers at the end: Code:
sed = test1.txt | sed 'N;s/\(.*\)\n\(.*\)/\2 \1/' An awk alternative would be: Code:
awk '{print $0,NR}' test1.txt |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Use this if the position of the new column never matters, try nl command Code:
nl filename Code:
# cat file asddg asgfdfgh sdastg djsf fj Code:
# nl file
1 asddg
2 asgfdfgh
3 sdastg
4 djsf
5 fj |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
perl
Hi, Try this one, Code:
perl -nle 'print "$_ $.";' input_file Cheers, Ranga
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding row of numbers | SkySmart | Shell Programming and Scripting | 12 | 06-13-2012 03:14 AM |
| How do you delete cells from a space delimited text file given row and column number? | evelibertine | UNIX for Dummies Questions & Answers | 5 | 06-01-2011 01:41 PM |
| Adding a column to a text based on file name | rlapate | Shell Programming and Scripting | 12 | 05-23-2009 09:22 PM |
| Adding a new column in a text file | snahata | Shell Programming and Scripting | 10 | 03-13-2009 07:00 AM |
| Changing the column for a row in a text file and adding another row | aYankeeFan | Shell Programming and Scripting | 9 | 05-02-2005 09:42 PM |
|
|