Combine table field for time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combine table field for time
# 1  
Old 02-24-2014
Combine table field for time

Hi,

I have table like

Code:
usrid   Month Date year   Time
w23da Feb    10    2014 12:42:34 
ae3aw Feb    20    2014 12:47:02
zse3q  Feb    09    2014 10:02:28

all the five fields are inserted into different columns
I want to combine all four (Month,Date,year and Time) and make it below O/P

Code:
usrid             usrdetls
w23da   10.02.2014 12:42:34    
ae3aw   20.02.2014 12:47:02   
zse3q    09.02.2014 10:02:28

# 2  
Old 02-24-2014
What have you tried?
# 3  
Old 02-24-2014
Code:
awk 'BEGIN {X["Jan"] = "01"; X["Feb"] = "02"; X["Mar"] = "03"; X["Apr"] = "04";
X["May"] = "05"; X["Jun"] = "06"; X["Jul"] = "07"; X["Aug"] = "08"; X["Sep"] = "09";
X["Oct"] = "10"; X["Nov"] = "11"; X["Dev"] = "12"; print "userid\tusrdetls"};
NR > 1 {print $1 "\t" $3 "." X[$2] "." $4 " " $5}' <file_name>

Below is the output
Code:
$ cat sample.dat
usrid Month Date year Time
w23da Feb 10 2014 12:42:34
ae3aw Jan 20 2014 12:47:02
zse3q Feb 09 2014 10:02:28
$ awk 'BEGIN {X["Jan"] = "01"; X["Feb"] = "02"; X["Mar"] = "03"; X["Apr"] = "04";
> X["May"] = "05"; X["Jun"] = "06"; X["Jul"] = "07"; X["Aug"] = "08"; X["Sep"] = "09";
> X["Oct"] = "10"; X["Nov"] = "11"; X["Dev"] = "12"; print "userid\tusrdetls"};
> NR > 1 {print $1 "\t" $3 "." X[$2] "." $4 " " $5}' sample.dat
userid usrdetls
w23da 10.02.2014 12:42:34
ae3aw 20.01.2014 12:47:02
zse3q 09.02.2014 10:02:28
$

# 4  
Old 02-24-2014
So - what's your problem? The output you present is very close to what you desired. You may want to add some quotes around the date/time field.
# 5  
Old 02-24-2014
In KSH93:
Code:
#!/bin/ksh93

printf "usrid\tusrdetls\n"
while read v1 v2 v3 v4 v5
do
        [ "$v1" = "usrid" ] && continue
        printf "%s\t%s.%(%m)T.%s %s\n" "$v1" "$v3" "$v2" "$v4" "$v5"
done < file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to combine matches and use a field to adjust coordinates in other fields

Trying to output a result that uses the data from file to combine and subtract specific lines. If $4 matches in each line then the last $6 value is added to $2 and that becomes the new$3. Each matching line in combined into one with $1 then the original $2 then the new$3 then $5. For the cases... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Dummies Questions & Answers

Combine Similar Output from the 2nd field w.r.t 1st Field

Hi, For example: I have: HostA,XYZ HostB,XYZ HostC,ABC I would like the output to be: HostA,HostB: XYZ HostC:ABC How can I achieve this? So far what I though of is: (1 Reply)
Discussion started by: alvinoo
1 Replies

4. Shell Programming and Scripting

awk to combine by field and average by another

In the below awk I am trying to combine all matching $4 into a single $5 (up to the -), and count the lines in $6 and average all values in $7. The awk is close but it seems to only be using the last line in the file and skipping all others. The posted input is a sample of the file that is over... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Combine identical lines and average the one variable field

I have the following file 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:1.45 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:0.283 311027 chrX_310892_311162 300 91.6452... (2 Replies)
Discussion started by: jfern
2 Replies

6. Shell Programming and Scripting

Check a field in a table

I have made a table PRD_WORK_LM.test and it contains one field, ctrl_test. This field contains a 0 or a 1. I want to write a unix script that goes like this: IF ctrl_test = 1 THEN ... ELSE exit FI How can I write this in a script? Do I have to do this within bteq? or outside bteq? can... (5 Replies)
Discussion started by: katled
5 Replies

7. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

8. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

create new table/field

Dear Folks, If I have 2 files, say A and B in format: A: $1 $2 01032 12856 01041 13351 01042 11071 01042 12854 01042 12862 01042 12866 . . . and B: (2 Replies)
Discussion started by: Gr4wk
2 Replies
Login or Register to Ask a Question