LinuxQuestions.org
Help answer threads with 0 replies.
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 02-27-2012, 01:51 AM   #1
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

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


i have this file
test

[z_jigneshb.WFS_aftab.ST:s_m_EDW_F_SOP_DMND_FCST_DTL_DS]
$$INPUT_FILE_DATE=000000
$$PFFLAG=0

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
DS_P_201202

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

Code:
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
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Code:
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:
Code:
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:
Code:
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.
 
  


Reply


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


All times are GMT -5. The time now is 11:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration