LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-08-2012, 02:10 PM   #1
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Rep: Reputation: Disabled
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
 
Old 08-08-2012, 02:45 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

How about something like this?
Code:
(echo Blah && cat backup) > backup.tmp && mv backup.tmp backup

Cheers,
Tink
 
Old 08-08-2012, 03:23 PM   #3
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-08-2012, 05:40 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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.
 
Old 08-08-2012, 06:02 PM   #5
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
thank very very much
work very well
 
Old 08-08-2012, 06:09 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Welcome ... sorry about the duplicated echo :) .. I'm sure you got
that sorted yourself, though ;)
 
Old 08-09-2012, 04:25 AM   #7
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
there is a little problrem if the file "pippo.log" is empty don't add the line in sed command
 
Old 08-09-2012, 04:10 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Can you please rephrase that? I don't understand what a) the problem and b) the
desired result is ... :)
 
Old 08-09-2012, 04:22 PM   #9
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-09-2012, 04:39 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 :)
 
Old 08-09-2012, 05:14 PM   #11
micronemo
LQ Newbie
 
Registered: Aug 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
delete top lines of a text file until a word is met njfhdsiue Linux - Software 3 07-07-2011 03:48 AM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
script to insert no. of lines in a file to the start of the file LarsB Linux - Newbie 3 09-17-2009 08:22 PM
insert at the top of a file saavik Programming 7 08-29-2008 06:29 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration