Modify line with dynamic variable in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify line with dynamic variable in awk
# 1  
Old 11-25-2011
Modify line with dynamic variable in awk

Hi,

I'm guessing this is probably relatively straight forward to do in awk, but I just can't get my head round it! I have a log file of the following format:
Code:
3:03:35 (lmgrd) TIMESTAMP 10/14/2011
3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone
3:21:05 (MLM) IN: "MATLAB" user1@host2.private.dns.zone
5:00:33 (MLM) IN: "MATLAB" User@host3
5:48:48 (MLM) IN: "MATLAB" user2@host3.private.dns.zone
6:06:47 (MLM) TIMESTAMP 10/14/2011
6:13:15 (MLM) IN: "MATLAB" user2@host2.private.dns.zone
1:02:47 (MLM) TIMESTAMP 10/15/2011
1:14:15 (MLM) IN: "MATLAB" user3@host2.private.dns.zone

What I want to do is prepend each line with the current TIMESTAMP, so the above to become:
Code:
10/13/2011 3:03:35 (lmgrd) TIMESTAMP 10/14/2011
10/14/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone
10/14/2011 3:21:05 (MLM) IN: "MATLAB" user1@host2.private.dns.zone
10/14/2011 5:00:33 (MLM) IN: "MATLAB" User@host3
10/14/2011 5:48:48 (MLM) IN: "MATLAB" user2@host3.private.dns.zone
10/14/2011 6:06:47 (MLM) TIMESTAMP 10/14/2011
10/14/2011 6:13:15 (MLM) IN: "MATLAB" user2@host2.private.dns.zone
10/15/2011 1:02:47 (MLM) TIMESTAMP 10/15/2011
10/15/2011 1:14:15 (MLM) IN: "MATLAB" user3@host2.private.dns.zone

I'm won't be keeping the TIMESTAMP lines in the final output, so am not worried if the prepended timestamp is these lines is correct or not.


Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.


Any ideas?

Thanks!!

Last edited by Franklin52; 11-25-2011 at 08:18 AM.. Reason: Code tags
# 2  
Old 11-25-2011
Your first line of the expected output contains "10/13/2011" which is wrong I guess.

Code:
awk '/TIMESTAMP/ {t=$4}{print t,$0}' file

O/P
Code:
10/13/2011 3:03:35 (lmgrd) TIMESTAMP 10/13/2011
10/13/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone
10/13/2011 3:21:05 (MLM) IN: "MATLAB" user1@host2.private.dns.zone
10/13/2011 5:00:33 (MLM) IN: "MATLAB" User@host3
10/13/2011 5:48:48 (MLM) IN: "MATLAB" user2@host3.private.dns.zone
10/14/2011 6:06:47 (MLM) TIMESTAMP 10/14/2011
10/14/2011 6:13:15 (MLM) IN: "MATLAB" user2@host2.private.dns.zone
10/15/2011 1:02:47 (MLM) TIMESTAMP 10/15/2011
10/15/2011 1:14:15 (MLM) IN: "MATLAB" user3@host2.private.dns.zone


Please use code tags next time.
# 3  
Old 11-25-2011
Quote:
Originally Posted by anchal_khare
Your first line of the expected output contains "10/13/2011" which is wrong I guess.
Yep, that was a mistake!

Quote:
Code:
awk '/TIMESTAMP/ {t=$4}{print t,$0}' file

Brilliant, thanks - I think I was way overcomplicating it!

Quote:
Please use code tags next time.
Will do, sorry!

Thanks very much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

Using AWK to get a specific line using a variable

Hi I have a text like this example.input 1 red 2 blue 3 green If I set this c=2 Then try awk 'NR==$c { print $2 }' example.input I do get nothing If I try awk 'NR==2 { print $2 }' example.input i get blue (8 Replies)
Discussion started by: Jotne
8 Replies

5. Shell Programming and Scripting

awk line variable help

I am using Solaris 10 and have no GNU products installed. I have the following awk code and would like to add a variable. I believe awk doesn't accept variables, but I think nawk can. I would like to replace the red higlighted part of my code with a variable. If nawk is the answer how do I change... (3 Replies)
Discussion started by: thibodc
3 Replies

6. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

7. Shell Programming and Scripting

Dynamic command line generation with awk

Hi, I'm not an expert in awk but i need a simple script to do this: I'd like to AutoCrop PDF files. I found 2 simple script that combined together could help me to automatize :) The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf. The pdfinfo output... (8 Replies)
Discussion started by: gbagagli
8 Replies

8. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies

9. Shell Programming and Scripting

how to modify the value of the variable

Hi I have a variable which holds full path to the file, for example z=/bb/data3/f4222pdb.dta.new I need to remove the extension .new so it would look like z=/bb/data3/f4222pdb.dta Is there a command to do this? This variable is used in the "for" loop later. I am in ksh. Thanks a lot -A (4 Replies)
Discussion started by: aoussenko
4 Replies

10. Shell Programming and Scripting

How to modify the variable

I have a directory name stored in a variable. Does anyone have a piece of code which checks if this stored directory name ends up with the "/" and if it is missing adds it to the same variable. for example I might have A=/bb/data or A=/bb/data/ which needs to be A=/bb/data/ for sure thanks... (5 Replies)
Discussion started by: aoussenko
5 Replies
Login or Register to Ask a Question