0

Is it possible at all to delete special symbols like \ from a file while reading and searching in file word with sed and commenting line that consists word then save but \ will be in place?

I mean I have a file with company names(2 words) and between them can be some characters and in the original file used character escaping(regex) for those symbols. For example

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 substitute \ with nothing and comment, But I wanna save with \ too as previously was. Output I would like to have:

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$/"

My script is:

sed -E -e 's/\\//g' -i -e '/ITALY.*Delta M/s/^/#/' file.txt

But it saves without \. Yes, I know I can, not delete and just search as Red\\\\ Park. However, if I have 20 names, I wouldn't like to type \\\\ for every company name that consists 2-3 words:) Or maybe you have another vision to do it with another command, not sed? I am open for every advice or help Is it possible to get what I want to? Thank you so much!

0

2 Answers 2

2

If I understand you correctly, let's see if this works for you:

sed -E -e 's/^(.*\w+)\\\\ /#\1\\\\ /' -e 's/\\\\.([^ ]*)$/\\\\ \1/ file

Output:

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$/"
1
  • no no;( your script comments all names with \\, I wanna comment names that I type sed -E -e 's/\\//g' -i -e '/ITALY.*Delta M/s/^/#/' file.txt here only Delta M is commented, not other words with \.
    – Alex Cam
    Apr 7, 2020 at 5:16
0

They say persistence is a virtue......

I am assuming that all 2 name companies are separated by \\ and one more character and that countries are always upper case and all companies contain at least one lower case, i.e. a company cannot be a sub-string of a country.

An awk solution if yours supports the -i inplace option; I believe most do

awk -i inplace -v country="ITALY" -v company="Red.Park" -F'\\' 'BEGIN{
    OFS="\\"}
    $1~country && NR>1?$(NR-2)$NR:$1~company{
        printf "#"}
    NF>1{$NF=" "substr($NF,2)
    };1' file1
Output

#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$/"
lunar_static KKK_FRANCE_NUM "/^08.00.Green\\ Banana$/"

Walkthrough

Tell awk to make the changes -i inplace and pass your country and company in as variables -v. Since you have an unknown separator after \\ in your file then use . between the two parts of the company name

awk -i inplace -v country="ITALY" -v company="Red.Park" -F'\' 'BEGIN{

set the field separator to break on \ which will give you 3 fields where \\ occurs and then set

        OFS="\\"} 

to put them back when you print if there was a break.

If the first field $1==country that's the first part of the match.

For the company, check if you split on \ and ended up with multiple fields NR>1 or not and then test against either the last 2 fields or the unsplit field as appropriate.

If we have a match then printf # (that's print without a newline)

        $1~country && NR>1?$(NR-2)$NR:$1~company{
        printf "#"}

If you got more than 1 field NF>1 then you split on a \\? between company names and want to make sure the ? is whitespace when you print it, so remove the first character of the last field and replace it with a space

    NF>1{$NF=" "substr($NF,2)

And print

;1' file1
3
  • I evaluated ur joke about my persistence)) first Thank u very much, then I d like to know, according to your script I ll have to change every time country name and company names? no, its same time consuming as \\\\\\\\ . I just wanted to remover \\ while reading but when i save file after commenting \\ will be in place as before removing :(
    – Alex Cam
    Apr 7, 2020 at 5:25
  • ;) You wiill have to pass a search term in for every search anyway. But you can pass these into awk from a script if you have a list. Whichever tool you use you will have to do this. Though in the first pass this will replace all of the odd characters after \\` with a space and so after the first use you can just search for Red Park` rather than Red.Park.
    – bu5hman
    Apr 7, 2020 at 6:53
  • The -i inplace option writes to the original file, yes. TBH you could probably do this with sed and sponge more easily but I thought an awk solution would be fun to do.
    – bu5hman
    Apr 7, 2020 at 6:57

You must log in to answer this question.

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