Script to change first line of files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to change first line of files in directory
# 1  
Old 06-09-2012
Script to change first line of files in directory

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..
# 2  
Old 06-09-2012
Code:
for i in * ; do
  first=$(head -1 $i)
  head=$(echo $head | sed 's/ /_/')
  if [ "X$head" -eq "X" ] ; then
    head="blank"
  fi
  while [ -e $head ];do
    head="$head.1"
  done
  echo "mv $i $head" # if this looks good on the first pass, then uncomment the next line and try again
  #mv $i $head
done


Last edited by Skrynesaver; 06-11-2012 at 08:53 AM..
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 06-09-2012
I put this in a file in the directory with the files, added #!/usr/bin/bash to the first line, and ran it. I get an endless output of,

head: cannot open `=$ head.1' for reading: No such file or directory

It is stuck in a loop and I have to kill it.

Am I running this correctly?

LMHmedchem
# 4  
Old 06-09-2012
Quote:
Originally Posted by LMHmedchem
Am I running this correctly?
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:
Code:
cd "$1"
for f in *; do
    [ ! -f "$f" ] && continue
    if [ -s "$f" ]; then
        printf %s\\n 1c "$f" . w q | ed -s "$f"
    else
        printf %s\\n "$f" > "$f"
    fi
done

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.

Regards,
Alister

Last edited by alister; 06-09-2012 at 02:57 PM..
# 5  
Old 06-09-2012
Quote:
Originally Posted by alister
Perhaps this will meet your needs:
Code:
cd "$1"
for f in *; do
    [ ! -f "$f" ] && continue
    if [ -s "$f" ]; then
        printf %s\\n 1c "$f" . w q | ed -s "$f"
    else
        printf %s\\n "$f" > "$f"
    fi
done

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.

Code:
if ! cd "${1:-no-such-directory}"     # also quote on the off chance that something in the path has spaces
then
   echo "abort: could not switch to '$1' or parameter was missing"
   exit 1
fi

# 6  
Old 06-09-2012
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.

Code:
#!/usr/bin/bash
   FILENAME=$1
   sed "1 c\\$FILENAME" $FILENAME > TEMP
   rm -f $FILENAME
   cp -f TEMP $FILENAME
   rm -f TEMP

LMHmedchem
# 7  
Old 06-09-2012
Quote:
Originally Posted by agama
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.

Code:
if ! cd "${1:-no-such-directory}"     # also quote on the off chance that something in the path has spaces
then
   echo "abort: could not switch to '$1' or parameter was missing"
   exit 1
fi


Bah! Where's the fun in that? Smilie

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.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change ? char from files and directory

Hello, I must change files and dirs name which contains che "?" char, I try this: rename 's/?/-/' *.* nothing, what's the problem? thanks (14 Replies)
Discussion started by: ionral
14 Replies

2. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

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)
Discussion started by: ScKaSx
4 Replies

3. Shell Programming and Scripting

How to change a directory in shell script?

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)
Discussion started by: thelakbe
3 Replies

4. Shell Programming and Scripting

directory change in shell script

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)
Discussion started by: vls1210
2 Replies

5. Shell Programming and Scripting

Script to change file contents line by line

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)
Discussion started by: vini99
4 Replies

6. Linux

Change directory in a shell script

How do u change ur directory using a shell script?? (12 Replies)
Discussion started by: laxmi
12 Replies

7. Shell Programming and Scripting

change directory using shell script

How do u change ur directory using a shell script?? (0 Replies)
Discussion started by: laxmi
0 Replies

8. Shell Programming and Scripting

Change of directory thru script

: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)
Discussion started by: shiningram
4 Replies

9. Shell Programming and Scripting

Change Directory via a script?

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)
Discussion started by: George Borrmann
3 Replies

10. UNIX for Dummies Questions & Answers

How do I change ownership of a directory and all of it's files.

How do I change ownership of a directory and all of it's files without changing permissions? (1 Reply)
Discussion started by: mborin
1 Replies
Login or Register to Ask a Question