0

A header file is produced by running the main script and its content changes anytime I run the program . I want to assign the content of header file to other three files as their header. The content of other three files should not change except their header. For example for file1

header content
file1 content

for file2

header content
file2 content

How it is done?

Note: Please consider that the program runs many times so the header must be overwritten not many headers.

newzad
  • 706
  • 1
  • 14
  • 29

4 Answers4

1

run this code once to preserve the original files without the header information:

for file in myfile1 myfile2 myfile3
do
  cp ${file} ${file}.orig
done

run this code every time your header file changes

for file in myfile1 myfile2 myfile3
do
  cat header ${file}.orig > ${file}
done

where myfile1, myfile2 and myfile3 are the 3 original files you were referring and header is the file containing the ever changing header information.

MelBurslan
  • 2,383
  • 4
  • 18
  • 26
1

This code will backup each $file if it hasn't been saved before and then join the header on to the saved body. Just make sure the files don't have the header on the first time you run the script.

for file in myfile{1,2,3}
do
    [ -f $file.body ] || mv $file $file.body
    cat header $file.body >$file
done
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • I did for just one file. It works well, header does not accumulate in the files with every run. However there are always 3 headers in a file. I just used: [ -f file1.body ] || mv file1 file1.body cat header file1.body >file1 – newzad May 15 '13 at 06:58
  • @newzad As I said, make sure the file starts with no headers before you run the script. – Douglas Leeder May 15 '13 at 09:19
0

cat should do it

cat header file1 > file1-t
mv file1-t file1
Zombo
  • 1
  • 62
  • 391
  • 407
0
 cat headerfile file1 > file1_withHeader
Sidharth C. Nadhan
  • 2,191
  • 2
  • 17
  • 16