Results 1 to 5 of 5
I have been making my head explode with this one.
I am new to bash scripting but familiar in batch (ms).
I dont know if this is easier in bash ...
- 05-17-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 2
How do I not only rename a line in a text doc but also files?
I have been making my head explode with this one.
I am new to bash scripting but familiar in batch (ms).
I dont know if this is easier in bash or perl but I need a script that goes into a specific directory and replaces text in a file, then goes to another directory and replaces filenames. However as well as replacing a file it would need to keep it extension if it has one.
For the sake of explaining this better I will attempt to diagram this a bit.
1= old name
2= new name
File= /home/monitor/network/routers (text doc)
Loc1=/home/monitor/log/ (just a renaming of a file)
Loc2=/home/monitor/data/ (this dir has 1 and 1.dat that need to be replaced)
look for 1 and rename with 2 in file
look for 1 and rename with 2 in Loc1
look for 1 and rename with 2 in Loc2
------------------------
From what I learned, as far as the document goes it would need a Sed or Awk command. Is it possible to create a .bak of these before they are named? with the sed command i know that it is -i.* (at least i think so)
ANY help is greatly appreciated.
- 05-17-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The concept is the same, though bash is many orders of magnitude more complex (I mean "featureful") than command.com
Probably in bash. But I am probably biased because I am not a master in perl :PI dont know if this is easier in bash or perl
To do the first thing, as you already researched, you need sed or awk. I don't like sed much, but it's probably better suitted for this particular task.but I need a script that goes into a specific directory and replaces text in a file, then goes to another directory and replaces filenames. However as well as replacing a file it would need to keep it extension if it has one.
To rename files just use the command 'mv'. It's like the DOS "move" or "rename" command (if my memory serves correctly, since I haven't used DOS for many years now).Code:cat <filename> | sed -e 's/<original text>/<new text>/g' > <filename>
I don't quite understand the thing you say about the extension. If you can re-word it I might be able to help with that.
Cheers.
- 05-17-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 2
Thank you VERY much for your help so far
What I mean with the extensions portion is my directory has 2 files
1 and 1.conn
I need to rename both of them to be named
2 and 2.conn
if i use the mv command would it keep the extension?
Whats happening and how I plan to use this:
I dont know if you guys have used hobbit
I have quite an extensive list of ip's I monitor and now have a new naming system to identify them.
In order to rename something currently I have to modify the text file and manually rename the files I explained above.
The goal: To put together a script that when run will prompt me with
Old Name:
New Name:
$echo (or verbose, a printout of whats happening)
Success or Error
as well as back up the original files before I run it.
I will figure out the menu and stuff like that but this is so you guys get an idea of the use that I am aiming towards.
Thank you for any info you guys help, really!
- 05-17-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Well, mv is just a simple command. They tipical usage would be:
So, it's up to you what name will you give to a given file when renaming/moving it, I suppose. It all depends on what are you exactly doing on your script.Code:mv file1 file2
It would be something in the lines of:In order to rename something currently I have to modify the text file and manually rename the files I explained above.
The goal: To put together a script that when run will prompt me with
Old Name:
New Name:
$echo (or verbose, a printout of whats happening)
Success or Error
Code:echo "Old name: " read $old_name echo "New name: " read $new_name echo "Moving $old_name to $new_name..." mv "$old_name" "$new_name" && \ echo "Success" || echo "!!! Failed"
- 05-18-2008 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Assuming your file extension is always .conn, of course. Your loop could look like thisCode:mv $oldname $newname mv $oldname.comm $newname.conn
In this way you could either type the two names on a single line for each file to change, or put the names in a file and pipe it into the script.Code:while read oldname newname do mv $oldname $newname mv $oldname.conn $newname.conn done


Reply With Quote
