LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2010, 10:30 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
awk gsub() command - string (column) manipulation - substitution


i use this script to get the time and date of back and fourth transactions for a particular execution id. I use a substr command on the 5th column to to cut the milli seconds off the time value. - otherwise the times would look like 08:30:04.235

grep <executionID> <auditfile> | awk '{ print $1, $2, $3, $4, substr($5,1,8}

FIX -> Mon 3/1/2010 08:30:04
FIX <- Mon 3/1/2010 08:32:36
FIX <- Mon 3/1/2010 08:35:08
FIX -> Mon 3/1/2010 08:35:27
FIX <- Mon 3/1/2010 08:37:02
FIX <- Mon 3/1/2010 08:39:37
FIX -> Mon 3/1/2010 08:39:42

anyhow - i append two sed commands to further clarify the direction of the message.

awk '{ print $1, $2, $3, $4, substr($5,1,8} | sed -e 's/->/ ->IN/g' | sed -e 's/<-/<-OUT/g'

FIX -> IN Mon 3/1/2010 08:30:04
FIX <- OUT Mon 3/1/2010 08:32:36
FIX <- OUT Mon 3/1/2010 08:35:08
FIX -> IN Mon 3/1/2010 08:35:27
FIX <- OUT Mon 3/1/2010 08:37:02
FIX <- OUT Mon 3/1/2010 08:39:37
FIX -> IN Mon 3/1/2010 08:39:42

I tried using an awk gsub () command within the string instead of the two seds, but it did not work:
awk '{ print gsub(/<regex>/, <replace with>,$1), $2, $3, $4, substr($5,1,8}

the sed works ok, but it would be cooler to make the replacement within the awk command:

gsub(/->/,-> IN, $1)

Is there a way where i could replace the value of the $1 column in the awk print string?

Last edited by casperdaghost; 03-07-2010 at 11:07 PM.
 
Old 03-08-2010, 02:12 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Your problem is that you're trying to use gsub in the print
statement. You can do two things:
a) use gensub instead of gsub
Code:
$ awk '{print gensub( /<-/, "<- OUT", "g", gensub( /->/ , "-> IN","g"))}' ghost 
FIX -> IN Mon 3/1/2010 08:30:04
FIX <- OUT Mon 3/1/2010 08:32:36
FIX <- OUT Mon 3/1/2010 08:35:08
FIX -> IN Mon 3/1/2010 08:35:27
FIX <- OUT Mon 3/1/2010 08:37:02
FIX <- OUT Mon 3/1/2010 08:39:37
FIX -> IN Mon 3/1/2010 08:39:42
b) separate gsub from print.
Code:
awk '{gsub( /->/ , "-> IN");gsub( /<-/ , "-> OUT");print}' ghost 
FIX -> IN Mon 3/1/2010 08:30:04
FIX -> OUT Mon 3/1/2010 08:32:36
FIX -> OUT Mon 3/1/2010 08:35:08
FIX -> IN Mon 3/1/2010 08:35:27
FIX -> OUT Mon 3/1/2010 08:37:02
FIX -> OUT Mon 3/1/2010 08:39:37
FIX -> IN Mon 3/1/2010 08:39:42
In this case I'd prefer b) - it's cleaner & shorter.


Cheers,
Tink
 
  


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
string substitution lipun4u Linux - Newbie 3 02-22-2010 02:19 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
String Substitution Swapna173 Linux - Newbie 1 08-04-2008 06:22 AM
Need command for string manipulation hanke Programming 6 07-03-2007 12:27 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:19 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