Unix/Linux Go Back    


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

Adding a column to a text file with row numbers

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
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: 22 August 2014, 5:12 AM 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: 23 May 2015, 5:00 AM EDT
Location: Amsterdam
Posts: 10,202
Thanks: 332
Thanked 2,692 Times in 2,408 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: 31 October 2013, 9:16 AM EDT
Posts: 262
Thanks: 1
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: 23 May 2015, 5:00 AM EDT
Location: Amsterdam
Posts: 10,202
Thanks: 332
Thanked 2,692 Times in 2,408 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: 24 February 2015, 3:14 AM EST
Location: Chennai, India
Posts: 496
Thanks: 9
Thanked 124 Times in 120 Posts
perl

Hi,

Try this one,


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

Cheers,
RangaLinux
Sponsored Links
Closed Linux or Unix Question

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



All times are GMT -4. The time now is 06:18 AM.