Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-27-2012, 01:51 AM   #1
LQ Newbie
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
how to append a file using sed command

i have this file


i am writing a script to change the value for the date and flag which it gets from a file in the same directory.
the name of the file is

i have the code working but when i redirect it to the test file, it becomes a blank file

FILE_NAME=$(ls -tr DS_*|head -1)

DATEMONTH=$(echo $FILE_NAME | cut -d "_" -f3)
DATEMONTH=$(echo $DATEMONTH | cut -d "." -f1)
FLAG=$(echo $FILE_NAME | cut -d "_" -f2)
sed '/^\$\$INPUT_FILE_DATE=/s/[0-9]\{6\}/'$DATEMONTH'/' test>test
sed '/^\$\$PFFLAG=/s/.$/'$FLAG'/i test>test
Old 02-27-2012, 02:54 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
sed '/^\$\$INPUT_FILE_DATE=/s/[0-9]\{6\}/'$DATEMONTH'/' test>test
sed '/^\$\$PFFLAG=/s/.$/'$FLAG'/i test>test
You cannot read from a file and redirect output to that same file in a single command.

The shell output redirection operator (>) will truncate/erase the file prior to the command being executed. Because the file has been truncated, there is no data for the sed command to read.

You have two options:

1. Use the sed '-i' option to modify the file in-place. As in:
sed -i '/^\$\$INPUT_FILE_DATE=/s/[0-9]\{6\}/'$DATEMONTH'/' test
sed -i '/^\$\$PFFLAG=/s/.$/'$FLAG'/i test
2. Redirect the output to a temporary file, then copy the temporary file back on top of the original file.

For option 2 to work, you still need to modify your sed commands. The easiest would be to specify multiple sed commands with the -e option. For instance:
sed -e '/^\$\$INPUT_FILE_DATE=/s/[0-9]\{6\}/'$DATEMONTH'/' \
    -e '/^\$\$PFFLAG=/s/.$/'$FLAG'/i test > temp_file
mv temp_file test

Last edited by Dark_Helmet; 02-27-2012 at 02:55 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-28-2011 12:13 AM
[SOLVED] Append text with AWK or SED? LostChild1 Linux - Newbie 6 03-20-2010 09:46 AM
How can I append to a file remotely using a linux command (via ssh keys) abefroman Programming 2 11-27-2009 10:51 PM
[SOLVED] append command output to file by giving command in terminal sumeet inani Linux - Newbie 4 07-03-2009 11:36 AM
Recursive line append with sed usrb Programming 3 04-01-2009 06:10 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:16 PM.

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