Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-14-2012
Registered User
 
Join Date: Jul 2005
Posts: 94
Thanks: 11
Thanked 0 Times in 0 Posts
Put data in tabular form..

Dear Friends,
I have a file as under :


Code:
     +++ ME 12-06-13 18:16:20 
A  RED FEW AND ROW1 1MN FEL  AS
   HI FI BV      LR     TS    HR    ES    MR    
       *  0      13296   0    120   1     15
   KS  RR   
   10   0

      +++ ME 12-06-13 18:26:20 
A  RED FEW AND ROW2 1MN FEL  AS
   HI FI BV      LR     TS    HR    ES    MR
       *  1      13296   0    120   0     15
   KS  RR
   10   0

I am required to get the output as under:

Code:
 NAME	   DATE	   TIME    HI FI BV      LR     TS    HR    ES    MR   KS  RR
   ROW1 12-06-13 18:16:20     *  0      13296    0    120   1     15   10   0
   ROW2 12-06-13 18:26:20     *  1      13296    0    120   0     15   10   0

I am totally out of thought how it can be done.

Please help. Thanks in Advance.
Sponsored Links
    #2  
Old 06-15-2012
Registered User
 
Join Date: Aug 2009
Location: France
Posts: 295
Thanks: 13
Thanked 66 Times in 65 Posts
Is your file already tab-delimited?
Here a straight bit of code:

Code:
#!/usr/bin/awk -f

BEGIN{
	OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"
}
/\+\+\+/{
	s=$3 OFS $4
	getline
	s=$5 OFS s
	getline;getline; gsub("\t",OFS); s=s $0
	getline;getline; gsub("\t",OFS); s=s $0
	print s
}

Usage : save this code with a filename, set +x flag; then use: ~/unix.com$ filename your_data_file | column -ts'|'

Last edited by tukuyomi; 06-15-2012 at 05:27 PM..
The Following User Says Thank You to tukuyomi For This Useful Post:
vanand420 (06-16-2012)
Sponsored Links
    #3  
Old 06-16-2012
Registered User
 
Join Date: Jul 2005
Posts: 94
Thanks: 11
Thanked 0 Times in 0 Posts
MySQL

Quote:
Originally Posted by tukuyomi View Post
Is your file already tab-delimited?
Here a straight bit of code:

Code:
#!/usr/bin/awk -f

BEGIN{
	OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"
}
/\+\+\+/{
	s=$3 OFS $4
	getline
	s=$5 OFS s
	getline;getline; gsub("\t",OFS); s=s $0
	getline;getline; gsub("\t",OFS); s=s $0
	print s
}

Usage : save this code with a filename, set +x flag; then use: ~/unix.com$ filename your_data_file | column -ts'|'
Perfect!!!!!!

$ cat e1f | awk 'BEGIN {OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"} /\+\+\+/ {s=$3 OFS $4;getline;s=$5 OFS s;getline;getline;gsub("\t",OFS);s=s $0;print s}'
NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR
ROW1| 12-06-13| 18:16:20 * 0 13296 0 120 1 15
ROW2| 12-06-13| 18:26:20 * 1 13296 0 120 0 15

$ cat e1f | awk 'BEGIN {OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"} /\+\+\+/ {s=$3 OFS $4;getline;s=$5 OFS s;getline;getline;gsub ("\n",OFS);s=s $0;print s}'
NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR
ROW1| 12-06-13| 18:16:20 * 0 13296 0 120 1 15
ROW2| 12-06-13| 18:26:20 * 1 13296 0 120 0 15

$ cat e1f | awk 'BEGIN {OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"} /\+\+\+/ {s=$3 OFS $4;getline;s=$5 OFS s;getline;getline;gsub ("",OFS);s=s $0;print s}'
NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR
ROW1| 12-06-13| 18:16:20| | | | | | | *| | | 0| | | | | | | 1| 3| 2| 9| 6| | | | |
0| | | | | 1| 2| 0| | | | 1| | | | | | 1| 5|
ROW2| 12-06-13| 18:26:20| | | | | | | *| | | 1| | | | | | | 1| 3| 2| 9| 6| | | | |
0| | | | | 1| 2| 0| | | | 0| | | | | | 1| 5|

$ cat e1f | awk 'BEGIN {OFS="| ";print "NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR"} /\+\+\+/ {s=$3 OFS $4;getline;s=$5 OFS s;getline;getline;gsub (" ",OFS);s=s $0;print s}'
NAME| DATE| TIME| HI| FI| BV| LR| TS| HR| ES| MR| KS| RR
ROW1| 12-06-13| 18:16:20| | | | | | *| | 0| | | | | | 13296| | | | 0| | | | 120| | | 1| | | | | 15
ROW2| 12-06-13| 18:26:20| | | | | | *| | 1| | | | | | 13296| | | | 0| | | | 120| | | 0| | | | | 15

gsub changes the output format completely. Is this normal. How to put single delimiter | after each field.

Last edited by vanand420; 06-17-2012 at 10:20 AM.. Reason: New Findings
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Put data into tabular form vanand420 UNIX for Dummies Questions & Answers 4 06-11-2012 01:45 AM
Tabular form in shell script sreelu Shell Programming and Scripting 2 05-19-2011 02:51 AM
How to find max value in a tabular data? classic Shell Programming and Scripting 4 08-10-2010 10:34 AM
How to read tabular data? akash028 UNIX for Dummies Questions & Answers 1 09-29-2009 09:55 AM
converting a tabular format data to comma seperated data in KSH Hemamalini UNIX for Dummies Questions & Answers 2 06-16-2008 04:37 AM



All times are GMT -4. The time now is 11:57 PM.