Operating System - HP-UX
1827845 Members
1220 Online
109969 Solutions
New Discussion

Re: How to append a line at the beginning of a file

 
SOLVED
Go to solution
M.Thomas
Frequent Advisor

How to append a line at the beginning of a file

Hi Admins,
In shell scripting, I want to append a line at the starting of file not at the ending
Please tell me how to do that

Thanks in advance
Thomas

8 REPLIES 8
Geoff Wild
Honored Contributor

Re: How to append a line at the beginning of a file

Use sed

sed '1i\
your text goes here' file_name > new_filename

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sandman!
Honored Contributor

Re: How to append a line at the beginning of a file

#!/usr/bin/sh

ex -s input_filename <<-EOF
0r !echo insert this at the top of input_filename
wq
EOF
James R. Ferguson
Acclaimed Contributor

Re: How to append a line at the beginning of a file

Hi Thomas:

# perl -pi.old -e 'print "NEWLINE" if $.==1' fiie

This will update "inplace" the file(s) passed while making a backup copy as "*.old".

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: How to append a line at the beginning of a file

#!/usr/bin/sh

typeset FNAME=/xxx/yyy/myfile
typeset TDIR=${TMPDIR:-/var/tmp}
type T1=${TDIR}/F${$}_1.tmp
typeset -i STAT
typeset PROG=${0##*/}

if [[ -r "${FNAME}" ]]
then
trap 'eval rm -f ${T1}' 0 1 2 3 15

echo "Here is my new stuff" > ${T1}
cat ${FNAME} >> ${T1}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
mv ${T1} ${FNAME}
STAT=${?}
else
echo "${PROG}: Cat failed; status ${STAT}." >&2
fi
else
echo "${PROG}: Can't open \"${FNAME}\"; status ${STAT}." >&2
fi
exit ${STAT}
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: How to append a line at the beginning of a file

Hi (again) Thomas:

If you want only "shell" you can achieve your goal on a commandline thusly:

# echo "NEWLINE"|cat -- - file

This will place "NEWLINE" *before* the "file" contents.

Notice that "--" signals the end of switches for 'cat' and the "-" signifies STDIN as input (along with the name of the file you want to follow it with.

Regards!

...JRF...
Peter Nikitka
Honored Contributor
Solution

Re: How to append a line at the beginning of a file

Hi,

as one liner:

print "Prepended to file" | cat - origfile >/tmp/tmpfile$$ && mv /tmp/tmpfile$$ origfile

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
M.Thomas
Frequent Advisor

Re: How to append a line at the beginning of a file

Thanks to all for your quick response

Regards
Thomas
Steve Post
Trusted Contributor

Re: How to append a line at the beginning of a file

I know this is closed. I see the most obvious and simple solution was missing:

To add "my dog has fleas" to the top of file X.

cat "my dog has fleas" > X.tmp
cat X >> X.tmp
cat /dev/null > ./X
cat X.tmp >> ./X

Perhaps it was missed because it is inefficient and wipes the file for a portion of time. It requires more space because you have making a 2nd copy of file X.

Backwards writing it is.
You are appending the entire file X to the bottom of a one line file.