I need a script to take the filename of every file in a directory and substitute that file name for whatever is on the first line of the file. There may or may not be anything on the line, but I want the line to be the same as the file name. Most of the script tools I have used are non-destructuve, so I'm not sure how to go about this. I guess I could write to a temp file, delete the original, and then rename the temp, but that seems rather crude.
My understanding is this is what the sed c command is for (something like sed '1 c filename' file.txt), but I'm not sure of the usage and how I would get the filename.
Any suggestions as to where I could start.
LMHmedchem
Last edited by LMHmedchem; 06-09-2012 at 02:55 AM..
With all due respect to Skrynesaver, that script is severely braindamaged.
The line that's giving you that error looks to be an assignment but the assignment operator cannot have whitespace around it. It appears to be an attempt to generate a unique filename, but the result, if the syntax were correct, would be a filename with a string of .1.1.1.1.1.1 appended.
The variable $first is set on the second line but it's never used. I think it was intended to be used in place of the second occurence of $head on the third line.
The sed invocation will only modify the first space it encounters. If there is another, the unquoted use of $head in the -e test and the mv command will implode.
Bugs aside, I believe Skrynesaver misunderstood your request. It appears that the script is an attempt to take the contents of the first line in a file and use that to rename the file, instead of using the filename to modify the first line in the file.
Regards,
Alister
---------- Post updated at 01:42 PM ---------- Previous update was at 01:19 PM ----------
Perhaps this will meet your needs:
It takes one argument, the path to the directory to work on. It would be prudent to test it on a dummy directory with a few sample files.
I would suggest one small change: add some error checking to the cd command. If the user mistypes the path, it will work on all files in the current directory which is probably not what is intended.
This script will accept a filename and and do the renaming, but it is rather awkward and I would need to generate a list of files in the directory, which I guess is no big deal.
I would suggest one small change: add some error checking to the cd command. If the user mistypes the path, it will work on all files in the current directory which is probably not what is intended.
Bah! Where's the fun in that?
You are correct, of course. Better safe than sorry, especially when the damage can be so severe.
However, I think the parameter expansion, cd "${1:-no-such-directory}" is misguided. As unlikely as it may be to exist, no-such-directory is a valid directory name. In my opinion, it's a bad idea to replace an absent or empty parameter with anything, in this instance.
Regards,
Alister
---------- Post updated at 02:36 PM ---------- Previous update was at 02:25 PM ----------
It's much safer and appropriate for that replacement to occur when $1 is referenced in the echo statement.
Hi All,
I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs.
How would I do this in a script?
I have the following script called "csvtabs":
awk 'BEGIN {
FS... (4 Replies)
HI,
I need to change the working directory by using the shell script
/Export/home/user_name
I have to go one step back like
/Export/home
Please help on this.:confused: (3 Replies)
Hi,
I am trying to change the directory in my script, & want to check if the directory is present or not .
Ex:
cd /home/xyz/temp/logs
if the logs directory is not present i want to show the error in script instead of shell script error.
Can anybody please help me on the same
Thx in... (2 Replies)
Hi,
I'm struggling to write a script to do the following,
-will go through each line in the file
-in a specific character positions, changes
the value to a new value
-These character positions are fixed througout the file
-----------------------
e.g.: file1.sh will have the following 3... (4 Replies)
:confused:
Hi All,
This script is not working. I want to change the directory as per users selection in current shell. Looks like it is spawning sub-shell internally.
I have used
. changedir.sh
source changedir.sh
./changedire.sh
But did not work. Currently shell directory remain the... (4 Replies)
I would like to have a script that would change my current working directory. However, any time I execute a 'cd' command in a script, it holds only for the life of that script -- the working directory on exit is the same as when the script was initiated. Is it possible to have the script return... (3 Replies)