How to get data in a specified format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get data in a specified format
# 1  
Old 04-29-2010
Data How to get data in a specified format

Hii , I have a huge set of data stored in file a.dat as shown below
a.dat:
HTML Code:
081276A BURMA

  Date: 1976/ 8/12   Centroid Time: 23:26:51.8 GMT
  Lat=  26.55  Lon=  97.12
  Depth= 15.0   Half duration= 2.2
  Centroid time minus hypocenter time:  5.6
  Moment Tensor: Expo=24  7.840 -2.440 -5.400 -0.310 -0.090 3.950 
  Mw = 5.9    mb = 6.4    Ms = 6.1   Scalar Moment = 7.99e+24
  Fault plane:  strike=148    dip=45   slip=93
  Fault plane:  strike=323    dip=45   slip=87

051277C BURMA

  Date: 1977/ 5/12   Centroid Time: 12:20: 4.2 GMT
  Lat=  21.60  Lon=  92.77
  Depth= 40.0   Half duration= 3.5
  Centroid time minus hypocenter time:  3.5
  Moment Tensor: Expo=24  -0.399 -8.674 9.073 1.999 -2.447 -3.143 
  Mw = 5.9    mb = 5.4    Ms = 5.7   Scalar Moment = 9.91e+24
  Fault plane:  strike=216    dip=72   slip=3
  Fault plane:  strike=125    dip=87   slip=162

101377B BURMA-INDIA BORDER REG.

  Date: 1977/10/13   Centroid Time: 11:32:13.5 GMT
  Lat=  23.27  Lon=  93.16
  Depth= 60.8   Half duration= 1.9
  Centroid time minus hypocenter time:  4.2
  Moment Tensor: Expo=24  -0.322 -0.890 1.213 -0.681 -0.979 0.464 
  Mw = 5.4    mb = 5.2    Ms = 0.0   Scalar Moment = 1.62e+24
  Fault plane:  strike=145    dip=41   slip=-171
  Fault plane:  strike=48    dip=84   slip=-49

022378A BURMA-INDIA BORDER REG.

  Date: 1978/ 2/23   Centroid Time: 23:18:37.0 GMT
  Lat=  23.16  Lon=  94.93
  Depth=122.2   Half duration= 1.3
  Centroid time minus hypocenter time:  3.8
  Moment Tensor: Expo=23  3.449 0.489 -3.938 -2.334 -3.094 0.527 
  Mw = 5.1    mb = 5.1    Ms = 0.0   Scalar Moment = 5.37e+23
  Fault plane:  strike=331    dip=31   slip=44
  Fault plane:  strike=201    dip=69   slip=113

I want the output to be as b.dat
HTML Code:
    1A 1976 812 2326518 26.550N  97.120E  150  0 0616459  NE INDIA
    1F 148 45   93  323 45   87 
    2A 1977 512 1220 42 21.600N   92.770E  400  0 0575459  NE INDIA
    2F 216 72    3  125 87  162
    3A 19771013 1132135 23.270N   93.160E  608  0 0 05254  NE INDIA
    3F 145 41 -171  48 84   -49
    4A 1978 223 2318370 23.160N   94.930E  122  0 0 05151  NE INDIA
    4F 331 31   44  201 69  113  

Please help me out.. It follows the format
Code:
NUMBEA YearMoDy HrMnSec La.titN Lon.gitE Dept MLMDMSMBMW  NE INDIA
NUMBEF St1 D1 Ra1   St2 D2 Ra2


Last edited by zaxxon; 04-29-2010 at 03:14 AM.. Reason: added one more code tag
# 2  
Old 04-29-2010
MySQL

First of all for full solution is more than code and loops..and for this want more times ..I think make an effort for this unneccesary my sed method..
So for my code is only first line..At least for give an idea full solution that how long use sed solution Smilie

Code:
[root@sistem1lnx /]# cat file
081276A BURMA
  Date: 1976/ 8/12   Centroid Time: 23:26:51.8 GMT
  Lat=  26.55  Lon=  97.12
  Depth= 15.0   Half duration= 2.2
  Centroid time minus hypocenter time:  5.6
  Moment Tensor: Expo=24  7.840 -2.440 -5.400 -0.310 -0.090 3.950
  Mw = 5.9    mb = 6.4    Ms = 6.1   Scalar Moment = 7.99e+24
  Fault plane:  strike=148    dip=45   slip=93
  Fault plane:  strike=323    dip=45   slip=87
...............................

Code:
[root@sistem1lnx /]# sed -e '
/^$/d
/^[0-9]/d
s/[[:punct:]][[:punct:]]*//g
s/Centroid*//g
/^  Moment/d
s/GMT//g
/^   time/d' file | sed -n '
/^  Date 1976.*$/,/^  Date.*1977.*$/{;/^  Date.1977.*$/!p;}' | sed -e :a -e '$!N;s/\n//;ta' | sed -e '
s/^  Date\(.*\).*Time\(.*\)   Lat  \(.*\)  Lon  \(.*\)  Depth \(.*\)   Half duration 22  Mw  \(.*\)    mb  \(.*\)    Ms  \(.*\).*Scalar Moment.*$/\1\2 \3N \4E \5     0 \8\7   \6  NE INDIA/
s/   //g' | sed -e '
s/\(.*\)\(..\)\(..\)\(N\) \(..\)\(..\)\(E\)\(.*\)/1A\1\2.\30 \4\5.\60\7\8/'
 
1A 1976 812  2326518 26.550 N97.120E 150  0 616459  NE INDIA

# 3  
Old 04-29-2010
An AWK solution (not sure that it meets all yours requirements) :
Code:
awk -F '[[:space:]/:.=]+' '

function printDatas() {
   if (Count == 0) return;
   print sprintf("%6dA", Count), YearMoDy, HrMnSec, LatitN, LongitE, Dept, MLMDMSMBMW, "NE INDIA";
   print sprintf("%6dF", Count), St[1], Dip[1], Ra[1], St[2], Dip[2], Ra[2];
   Fp = 0;
   YearMoDy = HrMnSec = LatitN = LongitE = Dept = MLMDMSMBMW = "?";
   St[1] = St[2] = Dip[1] = Dip[2] = Ra[1] = Ra[2] = "?";
}

#1

/Date:/ {
   printDatas();
   Count++;
   
   YearMoDy = sprintf("%4.4s%2.2s%2.2s", $3, $4, $5);
   HrMnSec  = sprintf("%2.2s%2.2s%2.2s%1.1s", $8, $9, $10, $11);
}

/Lat=/ {
   LatitN  = substr(sprintf("%3.3s.%s000",$3,$4), 1, 7) "N";
   LongitE = substr(sprintf("%3.3s.%s000",$6,$7), 1, 7) "E";
}

/Depth=/ {
   Dept = $3;
}

/Mw =/ {
   MLMDMSMBMW = " 0 0" $9 $10 $6 $7 $3 $4;
}

/Fault plane:/ {
   Fp++;
   St[Fp]  = $5;
   Dip[Fp] = $7;
   Ra[Fp]  = $9;
}

END   { printDatas() }

' reva.dat

Output (with you input sample) :
Code:
     1A 1976 812 2326518  26.550N  97.120E 15  0 0616459 NE INDIA
     1F 148 45 93 323 45 87
     2A 1977 512 1220 42  21.600N  92.770E 40  0 0575459 NE INDIA
     2F 216 72 3 125 87 162
     3A 19771013 1132135  23.270N  93.160E 60  0 0005254 NE INDIA
     3F 145 41 -171 48 84 -49
     4A 1978 223 2318370  23.160N  94.930E 122  0 0005151 NE INDIA
     4F 331 31 44 201 69 113

Jean-Pierre.
# 4  
Old 04-30-2010
How to execute this awk command.
# 5  
Old 04-30-2010
  • Put the code in a script file (reva.sh for example), and modify the name of the input file (rava.dat in the posted code).
  • Make the file executable : chmod +x reva.sh
  • Execute the script reva.sh

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format DATA

Input File AU01NAS002,FCNVX133800117,AU01_Melbourne_Australia,ATT,Internal,NAS SILVER,12287.99,3293.98,6946.02 AU01NAS002,FCNVX133800117,AU01_Melbourne_Australia,ATT,Internal,NAS ARCHIVE,12287.99,3327.12,6912.87... (6 Replies)
Discussion started by: greycells
6 Replies

2. UNIX for Dummies Questions & Answers

Data format

Dear Masters, I have problem with my data result I do vim data result AAA111|^/CANADA|80 BAA111|^/PARIS|60 string with blue colour appears..how can I remove it? So when I do vi, blue string should not appear tks (2 Replies)
Discussion started by: radius
2 Replies

3. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

4. Programming

Transforming data to other format

Dear All I would like to transform data from one format to another format. my Input: 0 0 1 0 1 0.308 0 2 0.554 0 3 0.287 output: Z (0,0)= 1 Z (0,1)=0.308 Z (0,2)=0.554 Z (0,3)=0.287 (2 Replies)
Discussion started by: bala06
2 Replies

5. Shell Programming and Scripting

getting the data in some format

HI i am writing a shell script to generate some files having data in some format. for this i am using awk -F":" '{printf("%-20s:%-20s:%-20s:%-20s:%-15s:%-3s:%-19s:%-2s:%-20s:%-15s:%-2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.txt this helps in... (1 Reply)
Discussion started by: priyanka3006
1 Replies

6. UNIX for Dummies Questions & Answers

Help me to format this data please

Good day, I have a script on each machine on our network that will say the computer name and the number of updates needed. Then the script will send a file via scp to a network share with the title hostname.local The contents of the file would be: hostname N (with N being the number of... (11 Replies)
Discussion started by: glev2005
11 Replies

7. UNIX for Dummies Questions & Answers

Please help me format this data

STMC429 (192.168.171.72) 2008-11-24 14:18:09.412 softwareupdate Loading CatalogURL http://creativesus.conair.lan:8088/index.sucatalog No new software available. There are no updates to install STMC444 (3) (192.168.171.116) 2008-11-24 14:14:31.771 softwareupdate Loading CatalogURL... (4 Replies)
Discussion started by: glev2005
4 Replies

8. Shell Programming and Scripting

format the extracted data

I have executed the following code. #! /bin/ksh ############################ # AFI Monitor Script ############################ . /db2/uszlad48/sqllib/db2profile export mondir=/home/bmwdev1/script/krishna/arc export monlog=$mondir/rcbl2_`date +%Y%m%d`.log # connect to DB db2 connect... (2 Replies)
Discussion started by: kmanivan82
2 Replies

9. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies

10. Shell Programming and Scripting

format data

i have this data: GREEN LIST : 12321 34534 GREEN LIST : 45645 --- 23423 WHITE LIST : 23479 34534 75483 76924 12345 --- 12351 56778 --- 23330 GREEN LIST : 23567 the output must be: GREEN LIST : 12321 GREEN LIST : 34534 GREEN LIST : 45645 --- 23423 (2 Replies)
Discussion started by: inquirer
2 Replies
Login or Register to Ask a Question