Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Adding a column to a text file with row numbers

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-19-2012
evelibertine evelibertine is offline
Registered User
 
Join Date: May 2011
Last Activity: 21 September 2015, 10:44 PM EDT
Posts: 193
Thanks: 94
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 12-19-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 July 2016, 4:38 AM EDT
Location: Amsterdam
Posts: 10,937
Thanks: 417
Thanked 3,007 Times in 2,673 Posts
What have you tried?
Sponsored Links
    #3  
Old Unix and Linux 12-20-2012
bmk bmk is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 17 May 2016, 12:58 PM EDT
Posts: 267
Thanks: 2
Thanked 22 Times in 22 Posts
Try like...


Code:
 sed "=" test1.txt | sed "N;s/\n/ /"

    #4  
Old Unix and Linux 12-20-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 July 2016, 4:38 AM EDT
Location: Amsterdam
Posts: 10,937
Thanks: 417
Thanked 3,007 Times in 2,673 Posts
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  
Old Unix and Linux 12-20-2012
sathyaonnuix's Unix or Linux Image
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 26 June 2014, 12:23 PM EDT
Posts: 129
Thanks: 35
Thanked 14 Times in 13 Posts
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  
Old Unix and Linux 12-20-2012
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 July 2016, 7:22 AM EDT
Location: Chennai, India
Posts: 508
Thanks: 16
Thanked 130 Times in 126 Posts
perl

Hi,

Try this one,


Code:
perl -nle 'print "$_ $.";' input_file

Cheers,
RangaLinux
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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
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



All times are GMT -4. The time now is 08:21 AM.