Manipulate data in detail problem facing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulate data in detail problem facing
# 1  
Old 12-14-2009
Manipulate data in detail problem facing

Input
Code:
Participant number: HAC
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          808       1083      276
   2    1  +  Front         1373       1636      264
   3    1  -  Back       1837       2047      211

Participant number: BCD
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          781       1005      225
   2    1  +  Back        1113       2349     1237
   2    2  +  Last       2409       2974      566
   3    1  -  Last       2977       3169      193
   3    2  -  Back       3250       3390      141

desired output:
Code:
HAC_001-type01-positive-Front       808      1083  +1
HAC_002-type01-positive-Front      1373      1636  +1
HAC_003-type01-negative-Back      2047      1837  -1
BCD_001-type01-positive-Front       781      1005  +1
BCD_002-type01-positive-Back      1113      2349  +1
BCD_002-type02-positive-Last      2409      2974  +1
BCD_003-type01-negative-Last      3169      2977  -1
BCD_003-type02-negative-Back      3390      3250  -1

I would like to extract each detail and manipulate it based on my desired output.
For those with "-' at third column, it Distance_start and Distance_end should put in reversed direction.
Thanks a lot.

Last edited by patrick87; 12-14-2009 at 04:42 AM.. Reason: data edited mistakes
# 2  
Old 12-14-2009
Wrench

Try:

Code:
  awk 'NF==3 { _h=$NF } /+/ { _v="positive";v="+1"; } /-/{ _v="negative";v="-1"; }
 NF>3&& !($1 ~ /^Position/){
if (!v) { t=$6;$6=$5;$5=t; }
printf "%s_%03d-type%02d-%s-%s\t%s\t%s\t%s\n",_h,$1,$2,_v,$4,$6,$5,v }' file

Output:
Code:
HAC_001-type01-positive-Front   1083    808     +1
HAC_002-type01-positive-Front   1636    1373    +1
HAC_003-type01-negative-Back    2047    1837    -1
BCD_001-type01-positive-Front   1005    781     +1
BCD_002-type01-positive-Back    2349    1113    +1
BCD_002-type02-positive-Last    2974    2409    +1
BCD_003-type01-negative-Last    3169    2977    -1
BCD_003-type02-negative-Back    3390    3250    -1


Last edited by dennis.jacob; 12-14-2009 at 04:15 AM..
# 3  
Old 12-14-2009
Thanks dennis,
Your code work perfectly Smilie
In between, I just found out that I did a careless mistakes about my question for those data that with "-".
I just edited my problem again.
Input
Code:
Participant number: HAC
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          808       1083      276
   2    1  +  Front         1373       1636      264
   3    1  -  Back       1837       2047      211

Participant number: BCD
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          781       1005      225
   2    1  +  Back        1113       2349     1237
   2    2  +  Last       2409       2974      566
   3    1  -  Last       2977       3169      193
   3    2  -  Back       3250       3390      141

desired output:
Code:
HAC_001-type01-positive-Front       808      1083  +1
HAC_002-type01-positive-Front      1373      1636  +1
HAC_003-type01-negative-Back      2047      1837  -1
BCD_001-type01-positive-Front       781      1005  +1
BCD_002-type01-positive-Back      1113      2349  +1
BCD_002-type02-positive-Last      2409      2974  +1
BCD_003-type01-negative-Last      3169      2977  -1
BCD_003-type02-negative-Back      3390      3250  -1

I would like to extract each detail and manipulate it based on my desired output.
From those with "-' at third column, it Distance_start and Distance_end should put in reversed direction.
Thanks a lot.

---------- Post updated at 05:40 AM ---------- Previous update was at 03:41 AM ----------

Thanks dennis,
Thanks for the idea you given, I already found out the way to solve my problem.
Have a nice day.
Thanks for sharing Smilie
# 4  
Old 12-14-2009
patrick87, if you got your problem fixed, it will be better to paste the updated code here to share.

Below is the update from dennis.jacob's script.

Code:
awk 'NF==3 { _h=$NF } /+/ { _v="positive";v="+1"; } /-/{ _v="negative";v="-1"; }
 NF>3&& !($1 ~ /^Position/){
if (v~/-/) { t=$6;$6=$5;$5=t; }
printf "%s_%03d-type%02d-%s-%s\t%s\t%s\t%s\n",_h,$1,$2,_v,$4,$5,$6,v }' file

# 5  
Old 12-14-2009
Thanks for your reminding, rdcways.
Below is the solution of my problem:
Code:
awk 'NF==3 { _h=$NF } /+/ { _v="positive";v="+1"; } /-/{ _v="negative";v="-1"; }
 NF>3&& !($1 ~ /^Position/){
if (v~/-/) { t=$6;$6=$5;$5=t; }
printf "%s_%03d-type%02d-%s-%s\t%s\t%s\t%s\n",_h,$1,$2,_v,$4,$5,$6,v }' file

# 6  
Old 12-15-2009
Code:
use strict;
my %hash=('+'=>'positive','-'=>'negative');
my $name;
while(<DATA>){
	if(/Participant number: (.*)/){
		$name=$1;
	}
	elsif(/^\s+[^ ]+/){
		my @tmp = split;
		if($tmp[2] eq "-"){
			printf("%s_%03d-type%02d-%s-%s %s %s %s %s1\n",$name,$tmp[0],$tmp[1],$hash{$tmp[2]},$tmp[3],$tmp[5],$tmp[4],$tmp[2]);
		}
		else{
			printf("%s_%03d-type%02d-%s-%s %s %s %s %s1\n",$name,$tmp[0],$tmp[1],$hash{$tmp[2]},$tmp[3],$tmp[4],$tmp[5],$tmp[2]);
		}
	}
}
__DATA__
Participant number: HAC
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          808       1083      276
   2    1  +  Front         1373       1636      264
   3    1  -  Back       1837       2047      211

Participant number: BCD
Position type Location Distance_start Distance_end Range Mark
   1    1  +  Front          781       1005      225
   2    1  +  Back        1113       2349     1237
   2    2  +  Last       2409       2974      566
   3    1  -  Last       2977       3169      193
   3    2  -  Back       3250       3390      141

# 7  
Old 12-15-2009
Thanks summer,
Your script is another good solution to work for my problem as well^^
Thanks yaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help - manipulate data by columns and repeated

Hello good afternoon to everyone. I'm new to the forum and would like to request your help in handling data. I hope my English is clear. I have a file (Dato01.txt) to contine the following structure. # Col1 - Col2 - Col3 - Col4 Patricia started Jun 22 05:22:58 Carolina started Jun... (5 Replies)
Discussion started by: kelevra
5 Replies

2. Shell Programming and Scripting

awk facing delimiter inside data

Inpu file is as below: CMEOPT1_dump.1:1002 ZN:VTJ3J3C131 CMEOPT1_dump.1:1002 ZN:VTM4M4P123%5 CMEOPT1_dump.1:1002 ZN:VTM3M3P132%5 CMEOPT1_dump.2:1002 OZNG4 CMEOPT2_dump.3:1002 ZB:VTH4H4C132 CMEOPT2_dump.4:1002 ZN:VTK4K4P123 CMEOPT2_dump.5:1002 ZN:BOZ2Z2Z2P131%5 CMEOPT2_dump.5:1002 OZNG4 ... (10 Replies)
Discussion started by: zaq1xsw2
10 Replies

3. Shell Programming and Scripting

Need help to manipulate data using script

Hi i want to manipulate my data to convert row to column name 600 Slno vlan 1 600 2 609 3 700 name 700 Slno vlan 1 600 2 609 3 700 (8 Replies)
Discussion started by: nith_anandan
8 Replies

4. Shell Programming and Scripting

Help with data re-arrangement problem facing

Input file: <symbol>Q9Y8G1</symbol> <name>Q9Y8G1_EMENI</name> <symbol>Q6V953</symbol> <symbol>Q5B8K1</symbol> <name>Q6V953_EMENI</name> <symbol>G1A416</symbol> <name>G1A416_9FUNG</name> <symbol>D4N894</symbol> <name>D4N894_PLEER</name> <symbol>B0FFU4</symbol>... (13 Replies)
Discussion started by: cpp_beginner
13 Replies

5. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

6. Shell Programming and Scripting

Way to extract detail and its content above specific value problem asking

Input file: >position_10 sample:68711 coords:5453-8666 number:3 type:complete len:344 MSINQYSSDFHYHSLMWQQQQQQQQHQNDVVEEKEALFEKPLTPSDVGKLNRLVIPKQHA ERYFPLAAAAADAVEKGLLLCFEDEEGKPWRFRYSYWNSSQSYVLTKGWSRYVKEKHLDA NRTS* >position_4 sample:68711 coords:553-866 number:4 type:partial len:483... (7 Replies)
Discussion started by: patrick87
7 Replies

7. Shell Programming and Scripting

Remove duplicate line detail based on column one data

My input file: AVI.out <detail>named as the RRM .</detail> AVI.out <detail>Contains 1 RRM .</detail> AR0.out <detail>named as the tellurite-resistance.</detail> AWG.out <detail>Contains 2 HTH .</detail> ADV.out <detail>named as the DENR family.</detail> ADV.out ... (10 Replies)
Discussion started by: patrick87
10 Replies

8. Shell Programming and Scripting

how to manipulate with lines while playing with data

hello everyone, well I have a file which contains data, I want to add the data on hourly basis, like my file contains data for 24 hours, (so a total of 1440 ) lines. Now i want to add the data on hourly basis to get average values. like if I use (head) command it is ok for first go, but... (5 Replies)
Discussion started by: jojo123
5 Replies

9. UNIX for Dummies Questions & Answers

Excel data manipulate

All, I have the following format of data in a spreadsheet A 1 2 3 4 B 1 2 3 4 where 'A' is value of 'A1', '1 2 3 4' is value of cell B1, 'B' is value of cell A2, and '1 2 3 4' is value of cell B2. There... (12 Replies)
Discussion started by: rahulrathod
12 Replies

10. Shell Programming and Scripting

manipulate data with specific format

Hi everybody: I have a problem with how I have to manipulate the data which have specific format like this: 249. 0.30727021E+05 0.30601627E+05 0.37470780E-01 -0.44745335E+02 0.82674536E+03 248. 0.30428182E+05 0.30302787E+05 0.40564921E-01 -0.45210293E+02 ... (5 Replies)
Discussion started by: tonet
5 Replies
Login or Register to Ask a Question