LinuxQuestions.org
Help answer threads with 0 replies.
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 07-29-2009, 08:41 AM   #1
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Rep: Reputation: 44
copy certain text


Is it possibly to copy certain text from a file

i have a script that will configure my wireless card $interface
and i would like it to grap the mac address of the $interface so i thought

ifconfig $interface > add.txt

but since it will say alot of things in there when i type ifconfig including the mac address is it possible just to get a certian part i.e. search a file for **:**:**:**:**:** then put it in a var called $mac

Thanks
 
Old 07-29-2009, 08:50 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pr0xibus View Post
is it possible just to get a certian part i.e. search a file for **:**:**:**:**:** then put it in a var called $mac
Yes, you can build a regular expression to match the MAC address and use egrep -o to get the result, e.g something like
Code:
mac=$(/sbin/ifconfig eth0 | egrep -o \([0-9A-Z][0-9A-Z]:\)\{5\}[0-9A-Z][0-9A-Z])
but more simply you can do something like
Code:
mac=$(/sbin/ifconfig eth0 | awk '/HWaddr/{print $NF}')
since the MAC address is the last field of the line containing "HWaddr".
 
Old 07-29-2009, 08:58 AM   #3
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by pr0xibus View Post
and i would like it to grap the mac address of the $interface so i thought

ifconfig $interface > add.txt

put it in a var called $mac
do you mean the ip address?
here's what my 'ifconfig wlan0' displays:
ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr 00:21:5d:b2:e5:96
inet addr:10.62.11.219 Bcast:10.62.11.223 Mask:255.255.255.224
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1630626 errors:0 dropped:0 overruns:0 frame:0
TX packets:1040182 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2140727532 (1.9 GiB) TX bytes:243190342 (231.9 MiB)

The IP address can be seen in the line with 'inet addr:...'

If I want to store the IP address into a variable, I'd go this
way:
1. make sure the interface is up (ifconfig wlan0 up)
2. make sure the IP address has been attributed
if [ $(ifconfig wlan0 | grep -c 'inet addr:') -eq 0 ]; then
#no ip address found
... exit the script ...
fi
3. the ip address exists, store it in the variable mac:
mac=$(ifconfig wlan0 | grep 'inet addr:' | sed 's?.*inet addr:\([0-9][.0-9]*\).*$?\1?')

4. check:
echo $mac
should display:
10.62.11.219

replace wlan0 by the name of your interface if it's different
 
Old 07-29-2009, 08:58 AM   #4
pr0xibus
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Slackware
Posts: 215

Original Poster
Rep: Reputation: 44
colucix you my friend are a LEGEND

Last edited by pr0xibus; 07-29-2009 at 09:00 AM.
 
  


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
copy some text from one file to another send2rawat Linux - General 7 11-10-2010 01:15 AM
Copy text from terminal to a file vitalstrike82 Slackware 2 05-22-2009 10:12 AM
can't copy text from firefox to vi file toedoe Linux - Software 2 08-26-2007 07:23 AM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
Copy text between terminals? Ryan_Sutton Linux - Newbie 3 07-06-2002 10:41 PM

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

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