If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > script error (urgent)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-04-07, 20:57
usc usc is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
script error (urgent)

i have a requirement to remove a string in a file and add "@" to the end of file. i wrote a script for this. the script is shown below.

the string value gets replaced, but the ONLY problem is that the last line in the original file gets removed and i do get the "@" at the end of file.

find . -type f -name '*.txt' -print | while read i
do
sed -e 's/TEST.//g' $i > $i.tmp && mv $i.tmp $i
echo "@" >> $i
done

can anyone let me know whats going wrong here.
Reply With Quote
  #2 (permalink)  
Old 05-05-07, 01:58
sebobill sebobill is offline
Registered User
 
Join Date: May 2007
Posts: 3
can you either include the contents of one of these files or show me what the last line looks like
Reply With Quote
  #3 (permalink)  
Old 05-07-07, 03:27
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Your last line probably doesn't have a newline and is ignored by sed
e.g.
printf "zozo\nxoxo" | sed 's/o/0/g'
z0z0

printf "zozo\nxoxo\n" | sed 's/o/0/g'
z0z0
x0x0
Reply With Quote
  #4 (permalink)  
Old 05-07-07, 14:53
usc usc is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
contents of my file are like this:

p1: begin

abcd temp.part
xyz

end p1

I want to replace "temp." to "", this works. Also, the file should have "@" as the last line in the file;

for example the output should be:


p1: begin

abcd part
xyz

end p1
@

right now what i get is

p1: begin

abcd part
xyz
@

the line "END P1" is totally removed. how do i add the "@" to the end of file without having "END P1" removed from the file.

Last edited by usc; 05-07-07 at 15:20.
Reply With Quote
  #5 (permalink)  
Old 05-07-07, 17:16
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
find . -type f -name '*.txt' -print | while read i
do
(cat $i;printf "\n@\n") | sed -e 's/TEST.//g' > $i.tmp && mv $i.tmp $i
done
Reply With Quote
  #6 (permalink)  
Old 05-07-07, 19:36
usc usc is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
That worked great.

Thanks!
Reply With Quote
  #7 (permalink)  
Old 05-08-07, 02:10
sebobill sebobill is offline
Registered User
 
Join Date: May 2007
Posts: 3
it looks like 'read' was your problem there
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On