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
# 8  
Old 10-07-2014
okay use sed as Don suggusted


Code:
sed 's/\(.\{3\}\)\([0-9]\{1,2\}\)\([0-9]\{4\}\)\([0-9]\{1,2\}:[0-9]\{1,2\}\)\(.\{2\}\)/\1 \2 \3 \4 \5/g' file

---------- Post updated at 04:00 PM ---------- Previous update was at 03:57 PM ----------

keep in mind its always good if you show attempts at a solution and OS and versions are being used.

Last edited by Akshay Hegde; 10-07-2014 at 06:38 AM.. Reason: typo
This User Gave Thanks to Akshay Hegde For This Post:
# 9  
Old 10-07-2014
Quote:
Originally Posted by Akshay Hegde
okay use sed as Don suggusted


Code:
sed 's/\(.\{3\}\)\([0-9]\{1,2\}\)\([0-9]\{4\}\)\([0-9]\{1,2\}:[0-9]\{1,2\}\)\(.\{2\}\)/\1 \2 \3 \4 \5/g' file

---------- Post updated at 04:00 PM ---------- Previous update was at 03:57 PM ----------

keep in mind its always good if you show attempts at a solution and OS and versions are being used.
Thanks alot!
Thankyou for giving the guidance.. I tried your Perl script and it works perfectly! Smilie

But i have one problem. Actually my text file looks like below:
Code:
jan16201413:17PM abc def ghi
jan1620143:17PM abc def ghi
jan1620143:17PM abc def ghi
jan1620143:17PM abc def ghi
jan1620143:17PM abc def ghi

when i am applying the Perl script given by you it works but it gives the output 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

It is wipping off the next three columns.

Is there a way to solve this?
# 10  
Old 10-07-2014
Code:
perl -lape '$F[0] = join(" ", $F[0] =~ m/(.{3})(\d{1,2})(\d{4})(\d{1,2}:\d{1,2})(.{2})/);  $_ = join(" ",@F) '  file

# 11  
Old 10-07-2014
Hi Akshay,

This is giving me all the columns but they all are coming in the same cell.
I am redirecting the output to a .xls file as below:
Code:
perl -lape '$F[0] = join(" ", $F[0] =~ m/(.{3})(\d{1,2})(\d{4})(\d{1,2}:\d{1,2})(.{2})/);  $_ = join(" ",@F) '  file.txt > file.xls

Earlier when i was using the sed command given by Don it was coming perfectly in the .xls file
Code:
sed 's/\(...\)\(..\)\(....\)\(.*\)\(.[^[:space:]]\)[[:space:]]*/\1 \2 \3 \4 \5/' file.txt > file.xls

only one problem i was getting there was the output was coming like below:
Code:
jan 16 2014 13:17PM abc def g hi
jan 16 2014 3:17PM abc def g hi
jan 16 2014 3:17PM abc def g hi
jan 16 2014 3:17PM abc def g hi
jan 16 2014 3:17PM abc def g hi

It was giving space before the last two letters instead of giving space before PM.
Can we edit the code given by Don? The SED command mentioned above.
# 12  
Old 10-07-2014
Code:
sed 's/\(.\{3\}\)\([0-9]\{1,2\}\)\([0-9]\{4\}\)\([0-9]\{1,2\}:[0-9]\{1,2\}\)\(.\{2\}\)\(.*\)/\1 \2 \3 \4 \5 \6/g' file

I get this
Code:
jan 16 2014 13:17 PM  abc def ghi
jan 16 2014 3:17 PM  abc def ghi
jan 16 2014 3:17 PM  abc def ghi
jan 16 2014 3:17 PM  abc def ghi
jan 16 2014 3:17 PM  abc def ghi

This User Gave Thanks to Akshay Hegde For This Post:
# 13  
Old 10-07-2014
Thanks a ton Akshay that works perfectly! Smilie
# 14  
Old 10-07-2014
Quote:
Originally Posted by Sharma331
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
The code I gave you does not add any space after the last two characters on the line as long as the input you give it is in the format you specified (your sample data included dates presented as a string of 15 or 16 non-space characters sometimes followed a single space character). Since your input file has additional characters at the end of the line that you didn't tell us about before, I'm not surprised that the output doesn't work for you.

In the future, please don't waste your time and ours by giving us sample data that is not representative of your actual data. If you would have given us representative data to begin with, you would probably have gotten working code in 2 or 3 posts instead of the 13 posts this thread took to solve your problem.
This User Gave Thanks to Don Cragun For This Post:
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