LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2012, 01:35 AM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
delimiter file need to insert characters, awk problem.


I have space delimitered data file, data file as follows: The time format is not specified, may be 09:46:34 or 094634 or 09/46/34
Code:
data.txt
4367424271020235424  000210584060118   09:46:34  000000050000  110 3002
7424271020235424     00330210584060118 09:46:52                    0000
I need to insert fix time( ) to field 3, to make it look like this:
Code:
4367424271020235424  000210584060118   time(09:46:34)  000000050000  110 3002
7424271020235424     00330210584060118 time(09:46:52)                    0000
I think I may use awk to deal with such delimitered file, but don't know how to insert.
Thanks in advance!

Last edited by fantasy1215; 07-09-2012 at 02:09 AM.
 
Old 07-09-2012, 02:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this sed solution:
Code:
sed 's/\([0-2][0-9]:[0-5][0-9]:[0-5][0-9]\)/time(\1)/' input
I personally think that sed is the easier solution.
 
Old 07-09-2012, 02:07 AM   #3
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Have a look at this sed solution:
Code:
sed 's/\([0-2][0-9]:[0-5][0-9]:[0-5][0-9]\)/time(\1)/' input
I personally think that sed is the easier solution.
But what if the time format is 20120709094634 instead of 09:46:34?
still Thanks for your quick reply.
 
Old 07-09-2012, 02:33 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Try this:
Code:
awk '{ printf("%-19.19d  %-17.17d  %-20s  %-12.12d  %-3.3d  %-4.4d\n", $1, $2, "time("$3")", $4, $5, $6 ) }' input
Example run:
Code:
$ cat input
4367424271020235424  000210584060118   09:46:34  000000050000  110 3002
7424271020235424     00330210584060118 09:46:52                    0000
7424271020235424     00330210584060118 20120709094634              0000

$ awk '{ printf("%-19.19d  %-17.17d  %-20s  %-12.12d  %-3.3d  %-4.4d\n", $1, $2, "time("$3")", $4, $5, $6 ) }' input 
4367424271020235264  00000210584060118  time(09:46:34)        000000050000  110  3002
0007424271020235424  00330210584060118  time(09:46:52)        000000000000  000  0000
0007424271020235424  00330210584060118  time(20120709094634)  000000000000  000  0000
One side effect: fields that aren't there are printed as zero's (italic parts in the above output). This could be fixed with a more elaborate awk script.

EDIT: I just noticed you edited your original post after an answer has been given, please don't do that. The answer(s) given might look out of place if you do so.

Last edited by druuna; 07-09-2012 at 03:03 AM.
 
Old 07-09-2012, 03:28 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You did not specifically mention formatting, so the obvious solution if not important would be:
Code:
awk '$3 = "time("$3")"' file
If the formatting is important:
Code:
awk 'sub($3,"time("$3")")' file
 
Old 07-09-2012, 04:44 AM   #6
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
You did not specifically mention formatting, so the obvious solution if not important would be:
Code:
awk '$3 = "time("$3")"' file
If the formatting is important:
Code:
awk 'sub($3,"time("$3")")' file
Thanks so much,
awk 'sub($3,"time("$3")")' file
really solves my problem. Thanks. The formatting problem is just the problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script to insert line into file that contains multiple """ characters tara Programming 2 02-15-2012 06:43 PM
[SOLVED] AWK / SED - Parsing a CSV file with comma delimiter, and some extra needs. PenguinJr Programming 8 05-24-2011 06:28 PM
using SED or AWK to cut data from a file, between certain characters hugh86 Linux - Newbie 18 10-28-2010 04:17 AM
Any tool to insert a newline each X characters in a file? neoAKiRAz Programming 5 05-09-2007 07:15 PM
how to insert special characters set2004 Linux - Newbie 2 08-05-2004 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration