[Solved] Help correcting file with differing number of fields | 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 !!

[Solved] Help correcting file with differing number of fields

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-16-2012
torchij torchij is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 29 April 2014, 5:04 PM EDT
Posts: 67
Thanks: 21
Thanked 1 Time in 1 Post
[Solved] Help correcting file with differing number of fields

Hi all, I have a tab separated file, and one of the fields is sub-delimited by colon. The problem is there can be zero to 4 colons within this field. When I try to change colons to tabs the result is a file with a differing number of fields.

I want to go from:

Code:
a:b:c:d:e
a:b:c
a:b:c:d:e
a
a:b:c:d:e

To:

Code:
a    b     c     d     e
a    b     c     .     .
a    b     c     d     e
a    .     .     .     .
a    b     c     d     e

Any help is appreciated!
Thanks
Sponsored Links
    #2  
Old 11-16-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk -F ":" 'NF<5{for(i=(NF+1);i<=5;i++){$i="."}}{$1=$1}1' OFS="\t" file

The Following User Says Thank You to pamu For This Useful Post:
torchij (11-16-2012)
Sponsored Links
    #3  
Old 11-16-2012
torchij torchij is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 29 April 2014, 5:04 PM EDT
Posts: 67
Thanks: 21
Thanked 1 Time in 1 Post
That seems to work perfectly!! Thanks so much.
    #4  
Old 11-17-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 16 July 2014, 11:41 PM EDT
Posts: 3,057
Thanks: 33
Thanked 657 Times in 636 Posts
this works for n number of columns


Code:
$ n=$(awk -F: 'NF>a{a=NF}END{print a}' input.txt);
$ awk -F: -v n="$n" '{for(i=1i<=n;i++)if($i=="")$i="."}NF==n{gsub(":"," ")}1' input.txt
a b c d e f g h i . .
a b c . . . . . . . .
a b c d e . . . . . .
a . . . . . . . . . .
a b c d e . . . . . .
a b c d e f g h i j k

$ cat a.txt
a:b:c:d:e:f:g:h:i
a:b:c
a:b:c:d:e
a
a:b:c:d:e
a:b:c:d:e:f:g:h:i:j:k

The Following User Says Thank You to itkamaraj For This Useful Post:
torchij (11-19-2012)
Sponsored Links
    #5  
Old 11-19-2012
torchij torchij is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 29 April 2014, 5:04 PM EDT
Posts: 67
Thanks: 21
Thanked 1 Time in 1 Post
works great, thanks so much.
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
using sed delete a line from csv file based on specific data in two separate fields Chris Eagleson UNIX for Dummies Questions & Answers 2 10-10-2012 12:14 PM
separate the file according to the number of fields ratheeshjulk Shell Programming and Scripting 3 08-16-2011 12:33 PM
Problem while counting number of fields in TAB delimited file vikanna UNIX for Advanced & Expert Users 12 06-14-2011 02:50 AM
number of fields in a text file as a variable - perl dynamax Shell Programming and Scripting 1 05-25-2011 03:15 AM
awk sed cut? to rearrange random number of fields into 3 fields axo959 Shell Programming and Scripting 4 04-29-2009 04:18 AM



All times are GMT -4. The time now is 01:15 AM.