The file has lines like:

AAA | AaaaA = "aaa -x 1"

BBB | BbbbB = "bbb -y 1"

which need to be replaced by:

AAA | AaaaA = "aaa -y 1"

BBB | BbbbB = "bbb -x 1"

I can't get sed to read the entire line.

It is planned to execute through a script, variables are used as strings. There are many such files, so it looks like this:

for i in $ files

do

sed $ i

done

  • Those. Need to change something from the sign - to пробел 1 ? - Mike
  • it is absolutely incomprehensible what your problem is and what answer you want to receive. - aleksandr barakin
  • "Ie you need to change something from the sign - to the gap 1" exactly - Sebastian Pereiro
  • The problem is that you cannot change only -x to -y, because you get two -y, which then change to -x (during the second pass of the script). Therefore, it is necessary to bind to additional data, in my case entirely to the line, it is unique. But it is impossible to pass this line to sed, it does not understand all spaces, quotes and hyphens, to shield the entire line hemorrhoid, I want the Sedo to feed the variable. That's the question, how? - Sebastian Pereiro
  • 2
    @SebastianPereiro can and not bind. can be done in one pass of the script, sed 's/-x/-y/;s/-y/-x/' - Mike

2 answers 2

If you need to replace x with y and vice versa, you can make it easier:

enter image description here

    Maybe use awk? Well, for example, if you replace x with y and vice versa, you get something like

     awk '{if ($4 == "-x") print $1 " " $2 " " $3 " -y " $5;if ($4 == "-y") print $1 " " $2 " " $3 " -x " $5}' <файл> 

    Regarding the double pass sed. In the first pass, you can replace x or y with some unique signature, for example, y with YyYy, x with XxXx, in the second pass, sed will not confuse replacing YyYy with x, XxXx with y.

     sed -e 's/\(^[AZ]\{3\}|[A-Za-z]\{5\} = "[az]\{3\} -\)y\( 1"$\)/\1YyYy\2/g' 

    In total there will be 4 passes: 2 preparatory on x and y, and two final on signatures.

    • Thank you, good option, but I did it in the forehead - I entered the entire line in sed - Sebastian Pereiro
    • In sed, you can give multiple replacement commands at once through a semicolon. And if you want, you can even organize IF - Mike
    • It is possible, but ... For IF-THEN, you probably need to get dirty with labels and t. IMHO, along with command tupling, this makes the syntax even more unreadable. When used in bash, I prefer to divide them into atomic-semantic operations with comments ... Otherwise, I’ll break my eyes in a couple of months. - FatalException