![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
Quote:
, 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 |
|
||||
|
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
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| rearranging columns, splitting file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|