Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-08-2012, 02:10 PM
|
#1
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Rep: 
|
Insert lines on top of a text file
hello, i must insert output of echo "$(date +%Y%m%d%H%M%S) Inizio backup" on top of text file pippo.log . can you help me with awk or sed command. thanks very very much
|
|
|
08-08-2012, 02:45 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Hi, welcome to LQ!
How about something like this?
Code:
(echo Blah && cat backup) > backup.tmp && mv backup.tmp backup
Cheers,
Tink
|
|
|
08-08-2012, 03:23 PM
|
#3
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Original Poster
Rep: 
|
thanks, but can't create temporary file , i must do it with i single command.
i try with this but i don't know how complete this:
echo "$(date +%Y%m%d%H%M%S) Inizio backup" | sed -e ' ????1i' /root/pippo.log
|
|
|
08-08-2012, 05:40 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Code:
sed -i "1i""$(echo \"$(date +%Y%m%d%H%M%S) Inizio backup\")" /root/pippo.log
Last edited by Tinkster; 08-08-2012 at 06:09 PM.
|
|
|
08-08-2012, 06:02 PM
|
#5
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Original Poster
Rep: 
|
thank very very much
work very well
|
|
|
08-08-2012, 06:09 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Welcome ... sorry about the duplicated echo :) .. I'm sure you got
that sorted yourself, though ;)
|
|
|
08-09-2012, 04:25 AM
|
#7
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Original Poster
Rep: 
|
there is a little problrem if the file "pippo.log" is empty don't add the line in sed command
|
|
|
08-09-2012, 04:10 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Can you please rephrase that? I don't understand what a) the problem and b) the
desired result is ... :)
|
|
|
08-09-2012, 04:22 PM
|
#9
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Original Poster
Rep: 
|
if pippo.log contains one or more lines your command work very well but if pippo.log is empty your command don't work.
i must have a command that work with empty file and not empty file
thanks
|
|
|
08-09-2012, 04:39 PM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
You won't be able to do this with a sed one-liner I'm afraid, sed operates on lines.
Write a wrapper script.
Code:
if [ `cat /root/pippo.log 2>/dev/null |wc -l` -eq 0 ]; then
echo "" > /root/pippo.log
fi
sed -i "1i""$(echo \"$(date +%Y%m%d%H%M%S) Inizio backup\")" /root/pippo.log
Last edited by Tinkster; 08-09-2012 at 04:42 PM.
Reason: minor amendment :)
|
|
|
08-09-2012, 05:14 PM
|
#11
|
LQ Newbie
Registered: Aug 2012
Posts: 14
Original Poster
Rep: 
|
thanks
|
|
|
All times are GMT -5. The time now is 04:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|