script to include filename with variations at the beginning of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to include filename with variations at the beginning of the file
# 1  
Old 03-25-2011
script to include filename with variations at the beginning of the file

Hi there,

I have a bunch of files that I want to modify.
As a beginner, but nevertheless enthusiast, in the use of the shell I want to create a script enabling me to modify those files quickly.
I had only some partial success with various peaces of scripts but I would like to create one script that do all the various actions.

So I have many files looking like these:
Code:
how-to.html
contact-me.html
what-is-new.html
etc...

There already have content in it but I want to add some content to them. I want to include their names with some variations in the beginning of each files, for example for how-to.html, I want those lines to be added at the beginning of the file:
Code:
how-to_mysite
how-to
How To
how to

I did a lot of searching and I basically found this script that seemed to have the bases of what I was looking for:
Code:
for file in *
do
    echo "$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done

Trying to get it step by step, I am already stuck at what I think could be logically the best choice for the first one:
  • first remove the .html extension (to put it back again at the end of the whole process)
  • second put the name of the file added with _mysite, a line break and again the name of the file

For the first part,I thought about using the following command, which I habe been using for a long time in the terminal without any problem
Code:
rename s"/*.html//g" *.html

For the second part, I just made a small modification of the model that I found:
Code:
for file in *
do    
    echo "$file"_mywebsite"\n$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done;

But I cannot figure out why this is not worlking:
Code:
`rename s"/*.html//g" *.html`;
for file in *
do    
    echo "$file"_mywebsite"\n$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done;
`rename s"/*/*.html/g" *`;

There is something wrong in the rename commands are they are not executed but I donīt have a clue.
I tried ' instead of " but it doesnīt change the output, I always get:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE / at (eval 1) line 1.

I also put the rename commands inside "for file in * do" and "done;" but no change

Last edited by Franklin52; 03-25-2011 at 11:39 AM.. Reason: Please use code tags
# 2  
Old 03-25-2011
Forget the rename command, use mv

Not that using mv will make you loose the inode of the target file you can alternately do :

Code:
for i in *.html
do
     echo "${i%.*}" >tmpfile
     cat "$i" >>tmpfile
     cat tmpfile >>"$i"
     rm tmpfile
done

---------- Post updated at 04:01 PM ---------- Previous update was at 03:54 PM ----------

For education purpose :

Code:
# a=toto.AnyThing.html
# echo $a
toto.AnyThing.html
# echo "${a%.*}"
toto.AnyThing
# echo "${a%%.*}"
toto

also read this

Last edited by ctsgnb; 03-25-2011 at 12:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add part of pathname/subfolder name to beginning of filename

Hello everyone, I need some help renaming files. I have a directory with a bunch of subfolders in it like so with DWI being the working directory and the 3 levels after it being subdirectories home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz... (8 Replies)
Discussion started by: azurite
8 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

3. What is on Your Mind?

Variations on a theme...

Well you lot seriously amaze me. There was a thread a few hours ago, (on the date stamp shown in this upload), that generated loads of solutions to a fairly simple problem:- https://www.unix.com/shell-programming-scripting/235829-how-print-particular-character-n-number-times-line.html As a... (0 Replies)
Discussion started by: wisecracker
0 Replies

4. Shell Programming and Scripting

mv command to include files beginning with "." (like .htaccess)

Hi, how can I get the mv command to include files beginning with . (such as .htaccess)? Right now when I mv a directory the .htaccess file is missing and I need to do this on a lot of directories, so there's a lot of wordpress permalinks that don't work anymore because the .htaccess file wasn't... (5 Replies)
Discussion started by: vanessafan99
5 Replies

5. Shell Programming and Scripting

script to insert variable file into another, bit like C's #include

Hi, I want to insert one file into another like this: file 1: 1 2 3 <!-- #include file="file2" --> 4 5 <!-- #include file="file3" --> 6 with the complete output going to another file. Have seen some solutions to similar problems using sed, but this problem is different because it is... (5 Replies)
Discussion started by: cvq
5 Replies

6. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

7. UNIX for Dummies Questions & Answers

Option in sql script to include column headers when spooling file to .csv format

Can anyone help me how to include COLUMN HEADER when spooling file to .CSV format through SQL statement. Thanks, Akbar (4 Replies)
Discussion started by: s1a2m3
4 Replies

8. Shell Programming and Scripting

how to include field in the output filename of awk

Im using awk and I want the output filename to contain the first field of the input file. Ex. 1 dddd wwwww 1 eeeee wwww 1 wwww eerrrr 2 eeee eeeeee I want the output files to be xxx1 and xxx2 Thank you (4 Replies)
Discussion started by: yahyaaa
4 Replies

9. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies

10. Shell Programming and Scripting

how to include header file in b-sh script

I'm new in this. In C/C++, you define variables in one header file & then simply include it in any files where those variables are referenced. I tried to include a header named DefineVars- which initializes all the globals- but when I add the line "DefineGlobals" at the begining of another... (2 Replies)
Discussion started by: bluemoon1
2 Replies
Login or Register to Ask a Question