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 12-16-2009, 04:56 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
awk help!!


Look at the following code:
Code:
date|awk '{print $4}'|awk -F ":" '{print $1}'>temp
a=`cat temp`
insted of moving some data into temp and then taking into a variable a,why can't we assign the value directly to variable,a like:
Code:
a=date|awk '{print $4}'|awk -F ":" '{print $1}'
 
Old 12-16-2009, 05:12 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
Hi,

You can:

a="`date|awk '{print $4}'|awk -F ":" '{print $1}'`"
or
a=$(date|awk '{print $4}'|awk -F ":" '{print $1}')

Both methods execute the date | awk ..... part first and put the output in the variable.

Hope this helps.

Last edited by druuna; 12-16-2009 at 05:22 AM. Reason: Fixed a typo.
 
Old 12-16-2009, 05:19 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thanks for your reply.

Quote:
Originally Posted by druuna View Post
a=$(date|awk '{print $4}'|awk -F ":" '{print $1}')"
I think you wrongly typed " there.

Last edited by ashok.g; 12-16-2009 at 05:21 AM.
 
Old 12-16-2009, 05:21 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
Yep I did, I'll correct the typo in my original reply.

Last edited by druuna; 12-16-2009 at 05:23 AM.
 
Old 12-16-2009, 06:10 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
no need to use awk 2 times!!
Code:
date|awk '{split($4,f,":");print f[1]}'
or
Code:
date|awk -F"[: ]" '{print $4}'
 
Old 12-16-2009, 06:23 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by ghostdog74 View Post
Code:
date|awk -F"[: ]" '{print $4}'
Can you explain me the above command?
 
Old 12-16-2009, 07:52 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ashok.g View Post
Can you explain me the above command?
The output of "date" is piped into awk.

Based on the defined field separator, the 4th field is printed.

Which part do you not understand? HINT: You can use regexes when defining field separators.
 
Old 12-17-2009, 12:25 AM   #8
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by pixellany View Post
Which part do you not understand?
What matches that field separator you used?I know that "[ ]" is used to match the character containing in that but you used ": " there. Actually the output of date doesn't contain any substring matching with ": ". Did you catch my point?

Last edited by ashok.g; 12-17-2009 at 12:31 AM.
 
Old 12-17-2009, 12:36 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ashok.g View Post
What matches that field separator you used?I know that "[ ]" is used to match the character containing in that but you used ": " there. Actually the output of date doesn't contain any substring matching with ": ". Did you catch my point?
[ ] is used to list a range of characters so, in this case, input lines will be split into words separated by ":" or " ".
 
Old 12-17-2009, 12:43 AM   #10
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thank you...
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
using awk on a awk result jpgauvin Programming 1 12-15-2008 03:57 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM

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

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