The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace columns from File1 with columns from File2 seijihiko UNIX for Dummies Questions & Answers 1 04-22-2009 03:34 AM
3 columns to 10 columns convertion aloctavodia Shell Programming and Scripting 4 02-14-2009 12:51 AM
Row to Columns vskr72 UNIX for Dummies Questions & Answers 4 03-21-2007 09:53 AM
rearranging the data in file (from columnwise to rowwise) reldb Shell Programming and Scripting 3 01-26-2007 05:36 AM
Rearranging fields from a pipe bthomas Shell Programming and Scripting 4 06-16-2005 11:42 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-22-2009
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
Rearranging columns

Hi,
I have an input file as follows :

input.txt

abcdTXXqwe
axdfSYYrew
dasgTXXqwt
gtfsTYYwer
gadfSXXerw
gwerSYYTXX

Now I have to get four output files.

output1.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as XX
output2.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as YY
output3.txt should have the first four cloumns, Where the rows containing 5th column as S and 6th-7th columns as XX
output4.txt should have the first four cloumns, Where the rows containing 5th column as S and 6th-7th columns as YY

i.e.

output1.txt

abcd
dasg


output2.txt

gtfs

output3.txt

gadf


output4.txt

axdf
gwer

I tried using cut and awk, but its too lengthy.

Is there any better solution?
  #2 (permalink)  
Old 05-22-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Quote:
Originally Posted by sudhamacs View Post
I tried using cut and awk, but its too lengthy.
Is there any better solution?
Yes, with awk , but maybe shorter then yours:
Code:
awk 'BEGIN{
  a[TXX]="output1.txt"
  a[TYY]="output2.txt"
  a[SXX]="output3.txt"
  a[SYY]="output4.txt"}
{print substr($0,1,4) > a[substr($0,5,3)]}' file
Bits Awarded / Charged to Franklin52 for this Post
Date User Comment Amount
05-22-2009 Neo Great. 3,000
  #3 (permalink)  
Old 05-22-2009
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
Its erroring out saying
Quote:
awk: cmd. line:6: (FILENAME=input.txt FNR=1) fatal: expression for `>' redirection has null string value
  #4 (permalink)  
Old 05-22-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Check the format of your file at the positions 5 to 7, do you have more then 4 combinations?
Try this, it gives filenames as TXX SYY TYY etc, the 5 to the 7 positions of the lines:

Code:
awk '{print substr($0,1,4) > a[substr($0,5,3)]}' file
  #5 (permalink)  
Old 05-23-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Code:
awk 'a=substr($1,5,3);b=substr($1,1,4);
a=="TXX"{print b > "output1.txt"}
a=="TYY"{print b> "output2.txt"}
a=="SXX"{print b> "output3.txt"}
a=="SYY"{print b> "output4.txt"}' input


-Devaraj Takhellambam
  #6 (permalink)  
Old 05-26-2009
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
Thanks.
Reply

Bookmarks

Tags
rearranging columns, splitting file

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0