Register
It is currently Thu Jul 31, 2014 1:34 pm

Script for pulling computer name from txt file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Feb 02, 2009 8:25 am   

Joined: Mon Feb 02, 2009 8:21 am
Posts: 4
I have a txt file we use for naming workstations. It looks like thist

bas1234 = Room1computer 2
bas1233 = Room1computer 4
bas1235 = Room1computer 3

I was to be able to use the bas1234, which I'm pulling from dmidecode and have in a variable already, search the file for the variable and assign the computername to another variable. I tried using cat and awk, but my scripting experience is limited. Thanks for any help you may be able to provide.

Thanks, Phillip


Top
 Profile  
 PostPosted: Mon Feb 02, 2009 9:01 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I think you need to explain more what you want to do.
As I understand it you want to match a computername in a file and then assign a new name?

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Feb 02, 2009 9:04 am   

Joined: Mon Feb 02, 2009 8:21 am
Posts: 4
I have a value "dellst" to I'm pulling from dmidecode (Dell Service tag). In my file, asset.txt, I have the following info.

bas1234 = Room1computer 2
bas1233 = Room1computer 4
bas1235 = Room1computer 3

I want to search asset.txt for bas1234, and take Room1computer2 and assign it to compname.


Top
 Profile  
 PostPosted: Mon Feb 02, 2009 9:18 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
okey, you just want to search asset.txt for (ex.) bas1234 and then get "Room1computer 2" as an answer??

Code:
grep "bas1234" asset.txt | cut -d'=' -f2

that should do it :)

if you promptly want to use awk
Code:
grep "bas1234" asset.txt | awk '{print $3}'


or sed
Code:
grep "bas1234" asset.txt | sed "s/^.* =//"


All of these are usuable with variable=$(command), or `` if you prefer those :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Feb 02, 2009 9:38 am   

Joined: Mon Feb 02, 2009 8:21 am
Posts: 4
compname=$(grep $dellst asset.txt | cut -d'=' -f2)
echo " This computer's name will be = "$compname

I'm getting the first record showing up along with the right one, except

Computer1is computer's name will be = Computer2

Computer1 is the right one, Computer2 if the first record.


Top
 Profile  
 PostPosted: Wed Feb 04, 2009 2:01 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I'm sorry mate, but I really don't understand your explaination.

Do like this, copy/paste the result you're getting and then manually create a correct result.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Feb 05, 2009 9:05 am   

Joined: Mon Feb 02, 2009 8:21 am
Posts: 4
I figured it out, my txt file had the same serial number in twice. I guess it would post the first one, at the end of the line, and the second back at the beginning.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP