Printing all the values in the middle of two columns | Unix Linux Forums | UNIX for Dummies Questions & Answers

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

Printing all the values in the middle of two columns

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2013
evelibertine evelibertine is offline
Registered User
 
Join Date: May 2011
Last Activity: 27 December 2013, 6:16 PM EST
Posts: 193
Thanks: 94
Thanked 0 Times in 0 Posts
Printing all the values in the middle of two columns

Hi,

I have a tab delimited text file with three columns:

Input:


Code:
1 25734      25737
1 32719      32724
1 59339      59342
1 59512      59513
1 621740     621745

For each row of the text file I want to print out all the values between the second and third columns, including them. The output should look like this:


Code:
1 25734 25735 25736 25737
1 32719 32720 32721 32722 32723 32724
1 59339 59340 59341 59342
1 59512 59513
1 621740 621741 621742 621743 621744 621745

Thank you
Sponsored Links
    #2  
Old 01-21-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 Posts

Code:
awk '{ printf "%d ",$1; j=$2; while(j<=$NF) { if(j==$NF) { printf "%d\n",j; j++; } else { printf "%d ",j; j++; } } }' filename

The Following User Says Thank You to Yoda For This Useful Post:
evelibertine (01-21-2013)
Sponsored Links
    #3  
Old 01-21-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 April 2014, 12:00 AM EDT
Location: Amsterdam
Posts: 8,878
Thanks: 230
Thanked 2,141 Times in 1,922 Posts

Code:
awk '{s=$1; for(i=$2; i<=$NF; i++) s=s OFS i; print s}' file

The Following User Says Thank You to Scrutinizer For This Useful Post:
evelibertine (01-21-2013)
    #4  
Old 01-22-2013
guruprasadpr's Avatar
guruprasadpr guruprasadpr is offline Forum Advisor  
Shrink...ing
 
Join Date: Jun 2009
Last Activity: 15 April 2014, 5:07 AM EDT
Location: India
Posts: 675
Thanks: 30
Thanked 215 Times in 214 Posts
Using Perl's range operator :


Code:
$ perl -ape '$_=join " ",$F[0],($F[1]..$F[2],"\n");' file

Guru.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing columns from a text file that do not have any values in second and third columns evelibertine UNIX for Dummies Questions & Answers 3 11-22-2011 03:53 PM
Get values from different columns from file2 when match values of file1 cgkmal Shell Programming and Scripting 17 09-29-2011 05:03 AM
Linux to Windows Printing: PDF starts printing from middle of page. rohan69 Windows & DOS: Issues & Discussions 5 08-04-2011 05:24 AM
Unix File - Adding columns in the middle ravi.videla Shell Programming and Scripting 11 12-07-2010 06:31 PM
printing only the middle word between two patterns tigta09 UNIX for Dummies Questions & Answers 7 09-09-2009 06:16 PM



All times are GMT -4. The time now is 12:45 AM.