LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-03-2010, 12:37 PM   #1
chudster
LQ Newbie
 
Registered: May 2008
Posts: 22

Rep: Reputation: 0
Question How to use grep, cut, or awk to get an IP from a file


Hello, I am new to shell scripting. I have a file which contains a line like this:

SERVER=10.205.110.19

How can I cut out the IP and assign it to a variable?

Thanks.
 
Old 02-03-2010, 12:41 PM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Two simple possibilities:
Code:
cat yourfile | awk -F= '{ print $2 }'
or
Code:
awk -F= '{ print $2 }' < yourfile
Kind regards,

Eric
 
Old 02-03-2010, 12:44 PM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Oops, sorry, rereading your post I imagine the file contains more lines than just the SERVER one; so small adjustment:
Code:
cat yourfile | grep "SERVER" | awk -F= '{ print $2 }'
Darn, must be getting tired, forgot the assignment to the variable:
Code:
VARIABLE=`cat yourfile | grep "SERVER" | awk -F= '{ print $2 }`
Kind regards,

Eric

Last edited by EricTRA; 02-03-2010 at 12:47 PM.
 
1 members found this post helpful.
Old 02-03-2010, 01:35 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
If it's literally just a bunch of VAR=VALUE, and you want all the VALUEs to get shoved into their respective VARs, then you can just "source" the file. Be aware that's actually just the same as copying and pasting the contents of the file to where the "source" directive is, so weird things you might not have expected otherwise might have happened.

Another approach like EricTRA's is to do:
Code:
SERVER="$(grep "SERVER=" "$FILE")"
SERVER="${SERVER#*=}"
 
Old 02-03-2010, 07:06 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Code:
echo SERVER=10.205.110.19 |cut -d'=' -f2
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
grep and cut command tanveer Linux - General 3 09-05-2008 02:17 AM
tcprobe+grep+cut Axion Programming 1 08-24-2004 05:27 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:45 AM.

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