0
false###File.txt 
many rows
###online = false --> ONE SHOT import (delete all table contents before importing)
###online = true  --> DIFFERENTIAL import (import without clean the table contents)
###online = true

many rows

I want to select lines having online alone and replace true with false but it need not be row number 3 it can come anywhere in the file

My attempt : test.sh

online=true
perl -i -p -e "s/^(online[ ]*=[ ]*).*$/\1 $online/" File.txt 

It does the work but adds a space before false. Can someone correct above command so that it doesnt add a space before the word true

2 Answers 2

2

You can try sed:

sed -i '3s/true/false/' file.txt

If you want to keep the original file as file.txt.bak:

sed -i.bak '3s/true/false/' file.txt

Also if you want to replace more than one occurrence of true with false in the same line:

sed -i.bak '3s/true/false/g' file.txt

EDIT: As per your clarified query, Try this:

sed -i.bak '/DIFFERENTIAL\|[^t][^r][^u][^e]$/!s/true/false/' file.txt

This will replace "true" with "false" in the lines where there is no "DIFFERENTIAL" and also where "true" comes at the end of the line.

9
  • The problem with this it It need not be row number 3 it can come anywhere in the file
    – Peck
    Mar 17, 2015 at 11:48
  • @Peck: Please update your question and add this info..also how to know the string of which row to replace and which row to keep..
    – heemayl
    Mar 17, 2015 at 11:49
  • @Peck: You can replace value from any row, just use the row number instead of 3..if its the 100th row: sed -i '100s/true/false/' file.txt
    – heemayl
    Mar 17, 2015 at 11:56
  • the thing is I dont know where it will come in the file think this might work testing this ### sed '/DIFFERENTIAL/!s/true/false/g'
    – Peck
    Mar 17, 2015 at 11:57
  • @Peck: so how to know the row number then?
    – heemayl
    Mar 17, 2015 at 11:58
0

I found following command that will help me:

sed '/DIFFERENTIAL/!s/true/false/g'
1
  • Check the edit in my answer..there is a problem in your current pattern, it does not match if "true" is at the end of the line..
    – heemayl
    Mar 17, 2015 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .