Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting 4

Status
Not open for further replies.

AIXtexas

Technical User
Feb 5, 2002
80
US
Is there a command to insert some text, for example "Test File" on the first line of a file, for example, named "shane."


Example, if this were my file now:

tech3:/home/root (856)#cat shane
Test File
This is a test file.

I want it to look like this:

tech3:/home/root (856)#cat shane
This is a test file.


Thanks,
Shane

 
I'm sorry I mixed this up...

Example, if this were my file now:

tech3:/home/root (856)#cat shane
This is a test file.

I want it to look like this:

tech3:/home/root (856)#cat shane
Test File
This is a test file.
 
Is this a trick question?

Anyway you can

1.
echo "TEST FILE"; cat shane
2. (assume you have file called header.txt)
cat header.txt shane

and you can output to new file using

cat header.txt shane > newshane.txt

you can have a footer also

cat header.txt shane footer.txt > newshane.txt

hope it helps
 
echo "TEST FILE" > newfile
cat originalfile >> newfile

or using sed

sed '1i\
TEST FILE' file_name > newfile



Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
You could also let perl do the temp file juggling for you:

Code:
perl -i -p -e 's/^/Test File\n/ if $. == 1' filename(s)

As the parenthetical "s" indicates, this command can be used to apply the same process to multiple files, if so desired.

To learn about the options used, try [tt]man perlrun[/tt], and for [tt]$.[/tt] and its fellow predefined variables, [tt]man perlvar[/tt].

- Rod

IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
or edit the file in place with ed instead of sed. (Note that some sed versions manage the edit in place also):

[script]
ed shane >/dev/null 2>&1 <<eof_ed
1i
Test File
.
w
q
eof_ed
[/script]

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top