Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Replace lines in a file

    Hi all,

    I am trying to write a shell script to automate removes some lines with some special characters in a file on multiple servers. line1 to 4 are the lines that i am trying to remove within test.txt. I always get an error on the sed line. Can anyone help me on this? Thanks in advance!

    line1="# Below lines are added to disable bash history "
    line2="export HISTFILE=/dev/null"
    line3="export HISTSIZE=0"
    line4="export HISTFILESIZE=0"
    echo $line1
    cp $file $backup
    sed "/$line1/d" $backup > $file
    sed "/$line2/d" $backup > $file
    sed "/$line3/d" $backup > $file
    sed "/$line4/d" $backup > $file

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Saint Paul, MN
    The problem is that you are editing the same file for each line and writing that one line deletion over the same file, so when you are done only the last of your deletes has been done.

    Also you have "/" in the search strings which will need to be "back-quoted".

    If you choose not to use a file you could use pipes:
    sed 'xxxx' sourcefile | sed 'YYYYY' | sed 'ZZZZ' | sed 'GGGGGG' > destinationfile

    So you should look at creating a 'sed script file' and using the "-f sed_script_file" or you can use some features within bash to create the file without needing to create the actual file on a disk.

  3. #3
    Yeah! I got it to work now! thanks alf55!!

  4. $spacer_open

Posting Permissions

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