- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to append a line at the beginning of a fil...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 07:59 AM
07-26-2007 07:59 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 08:24 AM
07-26-2007 08:24 AM
Re: How to append a line at the beginning of a file
sed '1i\
your text goes here' file_name > new_filename
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 08:27 AM
07-26-2007 08:27 AM
Re: How to append a line at the beginning of a file
ex -s input_filename <<-EOF
0r !echo insert this at the top of input_filename
wq
EOF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 08:34 AM
07-26-2007 08:34 AM
Re: How to append a line at the beginning of a file
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 08:34 AM
07-26-2007 08:34 AM
Re: How to append a line at the beginning of a file
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}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 08:57 AM
07-26-2007 08:57 AM
Re: How to append a line at the beginning of a file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 09:01 AM
07-26-2007 09:01 AM
Solutionas one liner:
print "Prepended to file" | cat - origfile >/tmp/tmpfile$$ && mv /tmp/tmpfile$$ origfile
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2007 09:24 AM
07-26-2007 09:24 AM
Re: How to append a line at the beginning of a file
Regards
Thomas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2007 12:07 AM
07-27-2007 12:07 AM
Re: How to append a line at the beginning of a file
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.