Help with data manipulation script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with data manipulation script
# 1  
Old 09-25-2012
Help with data manipulation script

Y,T,,H05,6,6,0,0 -> TH05_6
D,5,BT,B -> BT_KIOSK
P,KQC222 -> KQC222
G,B,2 -> BRANI_GATE_2

fileA


Code:
TPM658 Y,T,,H05,6,6,0,0
TPM110 D,5,BT,B
TPM136 P,KQC222
TPM180 P,BQC913
TPM575 Y,B,,T05,14,14,0,0
IPM760 G,B,2
TPM011

I need to use second column
$1,$2,$3,$4.....
if first char =='Y' then obtain $2,$4,'_',$6
else if first char =='D' then obtain $3_'KIOSK'
else if first char =='P' then obtain $2
else if first char =='G' then BRANI_GATE_2
else null
getting the result:

Code:
TPM658 TH05_6
TPM136 KQC222
TPM180 BQC913
TPM575 BT05_14
IPM760 BRANI_GATE_2
TPM011

Hi guys, need an approach guide to the problem. I dont know how to start Smilie. Thanks.

Last edited by Scrutinizer; 09-25-2012 at 03:44 AM.. Reason: code tags and formatting
# 2  
Old 09-25-2012
Is this homeworks ?
# 3  
Old 09-25-2012
Quote:
Originally Posted by delugeag
Is this homeworks ?
nope I am doing intern and my boss requested me to do it ...
# 4  
Old 09-25-2012
Code:
 sort -k2 file2 -o file2
awk -F"," 'NR==FNR{a[$1]++;next} {char=substr($1,length($1),1);if(a[char] ){ if (char == "Y") {
print $2,$4"_"$6} else if (char == "D") { print $3"_KIOSK" } else if  (char == "P" ) { print $2} else if (char == "G"){ print "BRANI_GATE_2" } else {print }}
}' file1 file2

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 09-25-2012
Quote:
Originally Posted by pravin27
Code:
 sort -k2 file2 -o file2
awk -F"," 'NR==FNR{a[$1]++;next} {char=substr($1,length($1),1);if(a[char] ){ if (char == "Y") {
print $2,$4"_"$6} else if (char == "D") { print $3"_KIOSK" } else if  (char == "P" ) { print $2} else if (char == "G"){ print "BRANI_GATE_2" } else {print }}
}' file1 file2

will do work on your script to find the problem but thanks for a good start
# 6  
Old 09-25-2012
Shell version:

Code:
while IFS=' ,' read label f1 f2 f3 f4 f5 f6 f7
do
  case $f1 in
    Y) res=${f2}${f4}_${f6} ;;
    D) res=${f3}_KIOSK      ;;
    P) res=$f2              ;;
    G) res=BRANI_GATE_2     ;;
    *) res=                 ;;
  esac
  printf "%s\n" "$label $res"
done < infile


Last edited by Scrutinizer; 09-25-2012 at 04:35 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-25-2012
Quote:
Originally Posted by Scrutinizer
Shell version:

Code:
while IFS=' ,' read label f1 f2 f3 f4 f5 f6 f7
do
  case $f1 in
    Y) res=${f2}${f4}_${f6} ;;
    D) res=${f3}_KIOSK      ;;
    P) res=$f2              ;;
    G) res=BRANI_GATE_2     ;;
    *) res=""               ;;
  esac
  echo "$label $res"
done < infile

OMG!!! I LOVE YOU!!!!

I dont know what is the miraculous script u have written but this is superBBBBB.....

Last edited by ment0smintz; 09-25-2012 at 04:41 AM..
This User Gave Thanks to ment0smintz For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data manipulation, Please help..

Hello, I have a huge set of data that needs to be reformatted. Here is a simple example to explain the process. I have number n=5 and a input with many numbers separated with comma: ... (11 Replies)
Discussion started by: liuzhencc
11 Replies

2. UNIX for Dummies Questions & Answers

Data Manipulation

Dear Sir, I have file input RGR001|108.28|-2.86489|100-120|RANGGAR RGR002|108.071|-2.69028|80-100|RANNGAR RGR003|108.168|-2.97053|50-80|RANNGAR RGR007|108.192722222|-2.766138889|0-50|RANGGARI want to create files by joining each rows with each rows below Output as below ... (4 Replies)
Discussion started by: radius
4 Replies

3. UNIX for Dummies Questions & Answers

Data manipulation

Hallo Team, I need to manipulate existing data file. Have a look at current data and expected data: Current Data: 27873517141 27873540000 27873515109 27873517140 27873540001 27873540000 27873501343 27873540000 27873517140 27873511292 27873645989 27873540000 27873540000... (7 Replies)
Discussion started by: kekanap
7 Replies

4. Shell Programming and Scripting

Data manipulation using shell

Dear all I have a dataset (in text format,delimited by tab) which have 100 variables (say, var0-var99) and more than 100,000 observations. I want to do the following: 1. for variable var0-var49, I want to add "00" in front of each data (for example, "1" would become "001") 2. for variable... (8 Replies)
Discussion started by: littlewenwen
8 Replies

5. UNIX for Dummies Questions & Answers

Script for data manipulation

Hi all! my first post here, so mods -- if this should ideally be in the scripts section, please move there. Thanks! I have data in the following format: key1:value1 key2:value2 key3:value3 A B C D key1:value4 key2:value5 key3:value6 A1 B1 key1: ... and so on I want an output... (2 Replies)
Discussion started by: gnat01
2 Replies

6. Shell Programming and Scripting

Data manipulation from one file

HI all i have a file consisting of following numbers 0000 0000 0000 0000 0000 1010 0000 0100 0000 0000 0000 1111 0000 1010 0000 0100 (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

7. Shell Programming and Scripting

Tricky data manipulation...

Hi everyone.. I am new here, hello.. I hope this doesn't come across to you folks as a stupid question, I'm somewhat new to scripting :) I'm seeking some help in finding a way to manipulate data output for every two characters - example: numbers.lst contains the following output:... (3 Replies)
Discussion started by: explicit
3 Replies

8. UNIX for Dummies Questions & Answers

Data Manipulation

Hello I am currently having problems in mapulating a certain file which contains vaious data. Belos is a sample content Event=<3190> Client IP=<151.111.11.143> DNS=<abc.sbc.com> TransCount=<139> Client IP=<150.222.133.163> DNS=<xyz.yuu.com> TransCount=<3734> Event=<3120> Client... (11 Replies)
Discussion started by: khestoi
11 Replies

9. Shell Programming and Scripting

Data manipulation with Awk

Hello guys, I'm a new member here and I need some help with the Awk application. I'm using it through the Terminal app of OSX (I'm a Mac user). I have a huge file with a large amount of data (rows of 3D cartesian coordinates). The data is typically like the following example (actually, the... (13 Replies)
Discussion started by: Cham
13 Replies

10. UNIX for Dummies Questions & Answers

data manipulation script

I have a folder called {homedata} Within this folder there are 12 subfolders 200601.......200612 Within each subfolder there are 8 sets of files Each filename commences with A B C D E F G or H, so {filename}* can be used. I am trying to write a script which will from the top level go... (1 Reply)
Discussion started by: grinder182533
1 Replies
Login or Register to Ask a Question