0

hope you are doing well during this time. The thing is I d like to comment some lines in my txt file. I have a code to comment and uncomment multiple lines, however I dont know how to get company name. My text file has this scheme: lunar_static KKK_<countryname>_NUM 08.00.name

lunar_static KKK_USA_NUM 08.00.Winter
lunar_static KKK_ITALY_NUM 08.Winter
lunar_static KKK_ITALY_NUM 08.00.Spring
lunar_static KKK_ITALY_NUM 08.00.Autumn
lunar_static KKK_SPAIN_NUM 08.00.Winter
lunar_static KKK_SPAIN_NUM 08.00.Spring
lunar_static KKK_FRANCE_NUM 08.00.Autumn
lunar_static KKK_FRANCE_NUM 08.00.Autumn
lunar_static KKK_ITALY_NUM 08.00.Ferari-taxi
lunar_static KKK_ITALY_NUM "/^08\\.00\\.Delta\\ M$/"
lunar_static KKK_ROMANIA_NUM "/^08\\.00\\.Delta\\ M$/"
lunar_static KKK_ITALY_NUM "/^08\\.00\\.Red\\_Park$/"

and I wanna comment ITALY with name Autumn, Ferari-taxi, Delta M, Red_Park. I know how comment few names with sed like

sed -i '/Winter\|Spring/s/^/#/' file2.txt

and uncomment

sed -i '/Spring/s/^#//' file2.txt

but it chooses all names despite of country name. Dont know how to get country. EXPECTED OUTPUT

    lunar_static KKK_USA_NUM 08.00.Winter
    lunar_static KKK_ITALY_NUM 08.Winter
    lunar_static KKK_ITALY_NUM 08.00.Spring
    #lunar_static KKK_ITALY_NUM 08.00.Autumn
    lunar_static KKK_SPAIN_NUM 08.00.Winter
    lunar_static KKK_SPAIN_NUM 08.00.Spring
    lunar_static KKK_FRANCE_NUM 08.00.Autumn
    lunar_static KKK_FRANCE_NUM 08.00.Autumn
    #lunar_static KKK_ITALY_NUM 08.00.Ferari-taxi
    #lunar_static KKK_ITALY_NUM "/^08\\.00\\.Delta\\ M$/"
    lunar_static KKK_ROMANIA_NUM "/^08\\.00\\.Delta\\ M$/"
    #lunar_static KKK_ITALY_NUM "/^08\\.00\\.Red\\_Park$/"

I would be glad for every advice. Thank you!

4
  • Add the expected output to your question. – Nasir Riley Apr 2 '20 at 16:25
  • What do comments look like (it varies by language (programming language)). – ctrl-alt-delor Apr 2 '20 at 16:31
  • I can't reproduce your result. Both of those commands work when I try them with the text that you have as they only operate on the lines with Winter and Spring. Are you trying to comment the lines based on the country name or the season? Are you using GNU sed. – Nasir Riley Apr 2 '20 at 16:31
  • ctrl-alt-delor the comment sign is #. Nasir Riley yes, I wanna comment lines based on country name and name in the end. For example, if I choose winter by sed written above it will comment all lines with winter without checking the country name. – Alex Cam Apr 2 '20 at 16:33
2

You need to include your target country in the search ITALY term, group the alternatives (Autumn|Delta....) and allow for the characters in between .* the two search terms

sed -E '/ITALY.*(Autumn|Delta.. M|Red.._Park|Ferari-taxi)/s/^/#/' file1

That's with a slight cheat on the \ to avoid the escapes

sed -E '/ITALY.*(Autumn|Delta\\\\ M|Red\\\\_Park|Ferari-taxi)/s/^/#/' file1

Make the changes in the same file by including the in place -i flag for sed

sed -i -E .....

Edit You could have posted this as another question, but...

"...is it possible to find words with \\ without typing \\\\ in sed ?..." in other words without escaping them.

No. And the reason why is here:

The ‘\’ character, when followed by certain ordinary characters, takes a special meaning

So, in your matching string, if the character after the \ is one which forms one of the reserved pairings (e.g \+, \?, \n) how is sed to know if you are looking for the literal \n or a newline?

So each \ that is intended to be read literally must be escaped to let sed unambiguously know that it is not part of any reserved character sequence.

6
  • oh, great! Thank u so much!!!! – Alex Cam Apr 2 '20 at 16:43
  • 1
    @AlexCam If one of the answers solved your issue, please take a moment to accept it by clicking on the checkmark on the left. That will mark the question as answered and is the way that thanks are conveyed on the Stack Exchange sites. – terdon Apr 2 '20 at 17:31
  • @bu5hman, is it possible to find words with \\ without typing \\\\ in sed ? \\ for character escaping. Just to type for example Red_Park or Delta M? – Alex Cam Apr 3 '20 at 7:03
  • There may be an alternative but without knowing the rules which govern the content of your file then it is difficult to guess. My best advice is for you to get to understand how regex works and figure out the rules which you need to apply.that define a match. If I think f anything I will add a edit to the post. – bu5hman Apr 3 '20 at 17:24
  • @bu5hman, thank u for editing the post. I have found another solution, what if first I substitute \ with nothing without saving and then find words? like sed -E 's/\\//g' file.txt | sed -i -E '/ITALY.*Delta M/s/^/#/' file.txt - but it gives no result. Do u know where is my mistake? It is like deleting while searching, but after commenting \ will exist as in original version) – Alex Cam Apr 6 '20 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.