Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2

    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!

    file=/tmp/test.txt
    backup=$file.bak
    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 Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    636
    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:
    Code:
    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
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2
    Yeah! I got it to work now! thanks alf55!!

Posting Permissions

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