Score:0

How to append to the end of a line before the first pattern match?

in flag

I have a file containing some random data like:

number1 number2 number3
&END

I want to add a \ to the end of the line containing number3, but I cannot match a pattern on that line as that number keeps changing. So I try matching "&END" in sed and then append a line before it, but that does not work for me because it creates a new line, like

number1 number2 number3

\

&END

My desired output is

number1 number2 number3 \
&END

Also, I only need to make this change at the first occurence of &END in the file. Is there a way to do this using sed, or maybe awk?

EDIT: My input file is test.restart, that looks like:

&COORD

1.4089452021105357E+01  1.3165670625576730E+01  1.2727066323166799E+01 \
 ...

 ...

1.3549295360587577E+01  1.2120902691780184E+01  1.2741652733169291E+01

&END
...
...
&COORD

H 3475843 73457834 7346587435
...
...
&END

I only need to make the changes in the block of text before the first occurence of &END.

pLumo avatar
in flag
You need to add some real-world examples to make your question solvable.
Pauling0304 avatar
in flag
@pLumo Real world examples of what? My tried solutions that don't work?
pLumo avatar
in flag
I mean of the input data. ("*end of the line containing number3, but I cannot match a pattern on that line as that number keeps changing.*"). What is your real-world "*number3*" then? Or better: how do you know that this is the line you want to match?
pLumo avatar
in flag
Another hint: Use `code` styling instead of `quote` styling would make your question better readable. You can edit your question.
pLumo avatar
in flag
okkkk... your real-world example is very different to your simpified example. The \ should not come at the end of the line before `&END`... Will it be in the next line after `&COORD` instead?
Pauling0304 avatar
in flag
@pLumo No, that \ at the end of the line after &COORD is already present. I now want to put it at the end of the line before &END.
pLumo avatar
in flag
ah good, then my answer is valid.
Score:1
in flag

You can use sed -z and include \n in your pattern:

-z, --null-data
separate lines by NUL characters

sed -Ez 's/(\n+&END)/ \\\1/' file

sed by default replaces only the first occurency per line. As you have only one line if you use NUL-delimiter, you're fine.

-E tells sed to use Extended Regex (ERE) instead of Basic Regex (BRE). You can omit the flag, but then you need to escape the braces:

sed -z 's/\(\n+&END\)/ \\\1/' file

Output:

&COORD

1.4089452021105357E+01  1.3165670625576730E+01  1.2727066323166799E+01 \
 ...

 ...

1.3549295360587577E+01  1.2120902691780184E+01  1.2741652733169291E+01 \

&END
...
...
&COORD

H 3475843 73457834 7346587435
...
...
&END

Note, -z is fine in Ubuntu, but not available in all sed implementations and thus not a portable solution.

Pauling0304 avatar
in flag
Can you tell me what the -E flag is for? And can I use the -i with it, because I want to edit this into the same file?
pLumo avatar
in flag
Yes, you can use `-i` if you're fine with the output. Added explanation for `-E`.
Pauling0304 avatar
in flag
I get the error: sed: -e expression #1, char 19: invalid reference \1 on `s' command's RHS
pLumo avatar
in flag
sure, you use the same command ?
Pauling0304 avatar
in flag
Yes, I even copy pasted
pLumo avatar
in flag
Both work fine for me. which Ubuntu version? I get this error when using the first version without `-E` which is expectable
Pauling0304 avatar
in flag
Ubuntu 20.04.3 LTS
pLumo avatar
in flag
Same version here. Should work just fine, cannot tell anymore, I'm sorry.
Pauling0304 avatar
in flag
I tried the other option without the -E flag, and that does not give any error but it doesn't do the job either.
pLumo avatar
in flag
Good idea @bac0n.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.