Register
It is currently Sat Nov 01, 2014 2:10 am

Copy/Append from file to New file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Jun 27, 2006 2:51 pm   

Joined: Tue Jun 27, 2006 2:43 pm
Posts: 1
Hi,

I'm New to Bash Scripting, and any help would be greatly appreciated.

Basically I want to copy from a text file that has this format

A) Ip Node - e.g. 10.0.0.1 cisco1.test.com

and paste into another file which has a different format

B) Node, IP - e.g. cisco1.test.com, 10.0.0.1

So I need to convert from A > B.

Any guidance is greatly appreciated.

Thanks


Top
 Profile  
 PostPosted: Tue Jun 27, 2006 8:38 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Sounds like homework...... but i'd start with the cut command and sed.


Top
 Profile WWW  
 PostPosted: Fri Jun 30, 2006 2:18 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
I actually wrote a similar script to take the contents of a DNS zone file and switch things around to the correct format for an /etc/hosts file. What you're looking for is even simpler than that. I would start with something like this:

Code:
cat FileA | awk '{print $2 ", " $1}' >> FileB


Hope this helps!
-Jeo


Top
 Profile YIM  
 PostPosted: Fri Jun 30, 2006 2:25 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Or even simpler ( I should really stop typing before I think... ):

Code:
awk '{print $2 ", " $1}' FileA >> FileB


If there are lines in there that you don't want, there are some fun things that you can do with grep and regular expressions too. Here's my DNS zone > /etc/hosts script. I'm only looking for a particular subnet, so it's pretty simple. I use SSH keys, so the ssh part is non-interactive:

Code:
#!/bin/sh

ssh root@192.168.1.40 "cat /var/named/techsupport.local.zone|grep '192.168.1'" > /tmp/zone.local

echo "# Do not remove the following line, or various programs" > /etc/hosts
echo "# that require network functionality will fail." >> /etc/hosts
echo "127.0.0.1 localhost.localdomain localhost" >> /etc/hosts

awk '{print $3 " " $1}' /tmp/zone.local >> /etc/hosts

exit



Hope it helps!
-Jeo


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 14 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:  
cron


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