Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    21

    Unanswered: 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.

  2. #2
    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

  3. #3
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    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

  4. #4
    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 16:20.

  5. #5
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    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

  6. #6
    Join Date
    Apr 2007
    Posts
    21
    That worked great.

    Thanks!

  7. #7
    Join Date
    May 2007
    Posts
    3
    it looks like 'read' was your problem there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •