Adding delimiter to a fixed pattern of date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding delimiter to a fixed pattern of date
# 1  
Old 10-07-2014
Adding delimiter to a fixed pattern of date

I have a text file as below:
Code:
jan16201413:17PM 
jan1620143:17PM 
jan1620143:17PM
jan1620143:17PM 
jan1620143:17PM

I want to add a delimeter which will be space to the date part as below:
Code:
jan 16 2014 13:17 PM
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM

So the space delimiter should be placed in a line in following way:
After first 3 characters(which determines monthe e.g. jan) then after two characters(which determines date e.g. 16) then after 4 characters(which determines year e.g. 2014) then before the last two characters(which determines AM or PM)

Is this possible?
# 2  
Old 10-07-2014
You could try:
Code:
sed 's/\(...\)\(..\)\(....\)\(.*\)\(.[^[:space:]]\)[[:space:]]*/\1 \2 \3 \4 \5/' file

# 3  
Old 10-07-2014
Quote:
Originally Posted by Don Cragun
You could try:
Code:
sed 's/\(...\)\(..\)\(....\)\(.*\)\(.[^[:space:]]\)[[:space:]]*/\1 \2 \3 \4 \5/' file

Thanks sir it works like charm! Smilie but the only problem with this is i am not getting space before PM it is coming like below:
Code:
jan 16 2014 13:17PM

I want it to be
Code:
jan 16 2014 13:17 PM

# 4  
Old 10-07-2014
If you have gawk try this

Code:
[akshay@nio tmp]$ cat file
jan16201413:17PM 
jan1620143:17PM 
jan1620143:17PM
jan1620143:17PM 
jan1620143:17PM


Code:
[akshay@nio tmp]$ gawk --re-interval '{ $0 = gensub(/([A-Za-z]{3})([0-9]{1,2})([0-9]{4})([0-9]{1,2}:[0-9]{1,2})([A-Za-z]{2})/, "\\1 \\2 \\3 \\4 \\5 \\6","",$0)}1' file

OR
Code:
[akshay@nio tmp]$ gawk --re-interval 'match($0, /([A-Za-z]{3})([0-9]{1,2})([0-9]{4})([0-9]{1,2}:[0-9]{1,2})([A-Za-z]{2})/, m){print m[1],m[2],m[3],m[4],m[5],m[6]}' file

Would result

Code:
jan 16 2014 13:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM


Last edited by Akshay Hegde; 10-07-2014 at 04:57 AM..
# 5  
Old 10-07-2014
Quote:
Originally Posted by Akshay Hegde
If you have gawk try this

Code:
[akshay@nio tmp]$ cat file
jan16201413:17PM 
jan1620143:17PM 
jan1620143:17PM
jan1620143:17PM 
jan1620143:17PM


Code:
[akshay@nio tmp]$ gawk --re-interval '{ $0 = gensub(/([A-Za-z]{3})([0-9]{1,2})([0-9]{4})([0-9]{1,2}:[0-9]{1,2})([A-Za-z]{2})/, "\\1 \\2 \\3 \\4 \\5 \\6","",$0)}1' file

OR
Code:
[akshay@nio tmp]$ gawk --re-interval 'match($0, /([A-Za-z]{3})([0-9]{1,2})([0-9]{4})([0-9]{1,2}:[0-9]{1,2})([A-Za-z]{2})/, m){print m[1],m[2],m[3],m[4],m[5],m[6]}' file

Would result

Code:
jan 16 2014 13:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM 
jan 16 2014 3:17 PM


I dont have gawk!

---------- Post updated at 03:01 AM ---------- Previous update was at 03:00 AM ----------

Hi Don,

Can you please tell me which part of your suggested code adds space after the last two characters of the line?

Thanks
# 6  
Old 10-07-2014
Try perl if you have

Code:
perl -pe '$_ = join (" ", $_ =~ m/(.{3})(\d{1,2})(\d{4})(\d{1,2}:\d{1,2})(.{2})/)."\n"' file


Last edited by Akshay Hegde; 10-07-2014 at 05:34 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 10-07-2014
Thanks but i dont have perl also... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Converting fixed width file to pipe delimiter in Linux(red-hat)

Hi, I am facing a typical scenario for AWK command . In HP- UNIX is behave as expected but in red hat linux same awk code is not give the same result. The below code is for convert the fixed width file to pipe delimiter file in HP-unix server. awk code: #!/bin/awk -f NR!=1... (11 Replies)
Discussion started by: brij_abhi
11 Replies

2. Shell Programming and Scripting

Adding a delimiter to a variable length file

Hi, I'm new to unix, i have a variable length file like below, 01|Test|Test1|Sample| 02|AA|BB|CC|DD| 03|AAA|BBB|CCC|DDD|EEE|RRR|TTT|SSS|YYY| I need to make this as a fixed length file. Assume that i have 10 columns in the DAT file. for ex: the first 01 record is having 4cols -... (8 Replies)
Discussion started by: Mohankumar Venu
8 Replies

3. Shell Programming and Scripting

Adding String at specific postition of a delimiter

I have a file containing data lines: 1|14|CONSTANT||11111111||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|STA11ACT|ddts555S||||00001|rrttbbggcc| 1|15|CONSTANT||22222222||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|rrtha772|llkis000||||00001|AAEtbbggcc| I want to add a... (3 Replies)
Discussion started by: pparthiv
3 Replies

4. Shell Programming and Scripting

how to implement in one-line awk in a fixed file having no delimiter

Hi, I have a file a.txt having no delimiter. I want to exclude the line which contains 435th character as 1 or 2 and redirect the rest of the lines to another file b. Can you pls suggest how to do this in one liner awk. Following is just one line of the input file a:- 120110116 ... (10 Replies)
Discussion started by: millan
10 Replies

5. Shell Programming and Scripting

search for a string without fixed delimiter in the line

Hi Need your help to assign the string to a variable from a line which has no fixed delimter in unix. for example , my file contains Name="report"" File Name one="test1" File Name two="test2" now how do I read report , test1 and test2 ? var1=report var2=test1 var3=test2 ... (1 Reply)
Discussion started by: rashmisb
1 Replies

6. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

7. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

8. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

9. Shell Programming and Scripting

Adding delimiter to logged in users

Hi guys! Just was wanting to run a command that would allow me to seperate the currently logged in users. Basically from this format: user1 user2 user3 To: user1|user2|user3 (Note the lack of a pipe at the end, not sure if thats possible) Basically it needs to be in this... (11 Replies)
Discussion started by: crawf
11 Replies

10. Shell Programming and Scripting

adding delimiter to a fixed width file

Hi , I have a file : CSCH74000.00 CSCH74000.00 CSCH74100.00 CSCH74000.00 CSCH74100.00 CSCH74000.00 CSCH74000.00 CSCH74100.00 CSCH74100.00 CSCH74100.00 I have to put a delimiter( say comma) in between after 6th character: CSCH74,000.00 CSCH74,000.00 CSCH74,100.00 (2 Replies)
Discussion started by: sumeet
2 Replies
Login or Register to Ask a Question