LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-22-2006, 04:32 AM   #1
onradius
LQ Newbie
 
Registered: Feb 2006
Posts: 12

Rep: Reputation: 0
awk on a variable


Hi

I need to do some awk on a variable. The variable is made like this:

/field1/field1 /field2 blabla etc

I put it into $test

And now I need the first field out of it.
I tried awk '{print $1}' $test
but that didn't work.

If I store the variable on the disk, and then do awk with the file, it works but I need it directly from the variable.

Thanks allready!
 
Old 02-22-2006, 04:36 AM   #2
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
cat $test |awk '{print $1}'


if your field seperator is "/" then you might need to do something like:

cat $test |awk -F "/" '{print $1}'
 
Old 02-22-2006, 05:09 AM   #3
onradius
LQ Newbie
 
Registered: Feb 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by satinet
cat $test |awk '{print $1}'


if your field seperator is "/" then you might need to do something like:

cat $test |awk -F "/" '{print $1}'
No it doesn't work. It always says NO SUCH FILE OR DIRECTORY with each field it contains
 
Old 02-22-2006, 05:31 AM   #4
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
sorry, i meant echo rather than cat.


echo $test|awk '{print $1}'

or

echo $test |awk -F "/" '{print $1}'

the "-F" means field seperator.
 
Old 02-22-2006, 06:26 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
to extract the first field
Code:
sed -n 's/^.*\///p' filename
 
Old 02-22-2006, 06:42 AM   #6
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
he doesnt want to use the file. he wants to expand the variable...

why use that sed when awk will do it far more simply??
 
Old 02-22-2006, 07:07 AM   #7
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
just gave one more solution to go with,

if it is not with a file and with variable
Code:
echo $var | sed -n 's/^.*\///p'
correct me if I am deviating!
 
Old 02-22-2006, 08:11 AM   #8
onradius
LQ Newbie
 
Registered: Feb 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks! I'm allright now
 
Old 02-22-2006, 08:34 AM   #9
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
what did you do?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM


All times are GMT -5. The time now is 11:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration