Using sed to find and append or insert on SAME line

Hi,

$ cat f1
My name is Bruce and my surname is

I want to use SED to find �Bruce� and then append � Lee� to the end of the line in which �Bruce� is found
Then a more tricky one�. I want to INSERT �.a string� in to a line in which I find sometihng. So example

$ cat f2
My name is Yacov and I am from Israel. I used to be a programmer but then I found love for a woman who told me to become a teacher.

I want to use SED to insert the string � excellent � where I find the string �programmer�. I want to insert this string exactly in beginning of the substring �programmer�

I have tried the A and the I option but A is appending to the next line, and I is wiping out the rest of the string. I dont understand what i am not following on the man page or the pages i have seen on google.

What have you tried?

Assuming that you used the s (substitute) command in sed to change the empty string at the end of a line to a specific string, why not also use the s command to change occurrences of programmer to excellent programmer (although it seems strange that you want a leading <space> character in the replacement text on those substitutions)?

1 Like

I hope this can elaborate my questions more:

1)
I want to use SED to find "StringToFind" and then append ON THE SAME LINE where the StringToFind is found, "MyString"

cat file1.txt

I would like "StringToFind" to be "Difficult"
I would like "MyString" to be "Easy"

line1 stuff is amazing especially when it is so difficult to understand
line2 more stuff is more amazing because it just is.
line3 i have nothing more to describe

2)
I want to use SED to insert "MyString" to the left or right examples?
So Before AND / OR After i have found "StringToFind" , i want to insert "MyString"

so say i have a file

cat insertexample.txt
line1 jobname RunReportForRisk
line2 Incond ConditionA-OK ODAT AND
line3 Outcon ConditionB-OK ODAT ADD
line4 i have many of these files and the inconditions and outcondition lines
line5 are all random BUT i know that they have a -OK at the end portion of it
line6 and then it might have the ODAT AND or ODAT ADD...

I would in this case like to add the string "_DLY" just before the -OK part.
so the file can contain Incond randomcondition-OKODAT AND

Thanks,

I do know the basic sed command like :

sed -e 's/Imre/Hungarian/g' myfile.txt > myfile_edit.txt

Although when i use -e or not it seems to do the same thing, so i dont really understand -e
I have read what is -e but i dont understand it , IF i am not seeing the difference.

I have tried things like /string/a \imre and it appends imre BUT on a new line...
and also i\ (not sure what is the purpose of forward slash.)
I dont seem to understand even how to read the man pages, it really frustrates me, this is why i have come to a forum.
The examples in MAN pages and even some SED documentation is not easy to understand for a new linux person, im sorry, this is my opinion.
This is why i come to a forum and speak to a human.
When i can pick up more BASIC FOUNDATION then i am sure i can Start or Begin to better understand the man pages.

Unfortunately it's not much clearer now, at least to me. Let me try to help by just guessing what you need, ignoring the samples you gave above.
Please be aware that sed matches case sensitive (unless told otherwise in some versions), so without extra effort it won't find the word Difficult if there's difficult in the file:

sed '/[Dd]ifficult/ s/$/Easy/' file

Here, /[Dd].../ is an address to find the relevant line(s) and append thereto.

For the second part of your question, you got me confused - there's no _DLY part nor randomcondition-OKODAT AND

If you want to prepend a string with another, use & : man sed :

Try

sed 's/-OK/_DLY&/' file

will result in _DLY-OK printed in the output.

1 Like

Thanks for trying to understand me, i am actually laughing now, so thank you.
I dont know what it is with me and why i confuse people so much at times,
Sorry i am just confused now i really hate feeling like this.
I dont understand the sed pages at all, well at least i think i dont,

so maybe if i can just explain it in a form of a sentence.
1a
i want to basically append at the end of a line where the search pattern i am searching for is found, thats it.

so regardless what the line says , if i find the word rabbit i want to append the string of my choice at the very-very end of the line
1b
you just confused me with the /[Dd].../ part im sorry no offense, but i dont understand. Maybe if u show me an example or 2.... or maybe i dont need to know this.

with regards to this question, what i have seems to make sense to me.
What i mean by randomcondition is that the names of the conditions are always unique they can be anything BUT they normally or always end with -OK
This is the only part i can use "-OK" where i know i would like to insert my own string to the left of the "-OK".
blahconditionname"mystring"-OK

hope it makes a bit more sense

Yes, you are confusing yourself and us.

In sed you do not append text to the end of a line. The sed a command appends lines of text following the current line. And, the sed i command inserts lines of text before the current line.

You use the sed s command to replace or substitute specified text on a line with replacement text that you specify. If you want to replace the text OK at the end of a line with the string _DLY-OK , you use:

sed 's/OK$/_DLY-&/' input_file > output_file

where the $ in OK$ anchors the matched text to the end of the line. OK found anywhere but at the end of the line will not be affected by this substitute command.

If you want to do that AND change every occurrence of the string daisy to the string tulip no matter where it appears on an input line, you use:

sed -e 's/OK$/_DLY-&/' -e 's/daisy/tulip/g' input_file > output_file

The -e was optional in the sed command above, but both -e options are required in this command. The sed command always takes at least one command argument. If there is more than one sed command argument, you need -e options in front of them so sed can determine which arguments are sed commands and which arguments are the names of files to be processed.

The g flag at the end of the substitute command s/daisy/tulip/g tells sed to make that substitution globally on each addressed line. Without the g flag (i.e., s/daisy/tulip/ ), only the first occurrence of daisy on each addressed line would be changed to tulip .

1 Like

I will have a look at the first piece of code you gave me and see what the affect is.
I think i got the -e option of sed now, thanks also.
The 's/findstr/repstring/g' is about the most common syntax of sed i can actually understand.
which is why i hate why i cant understand a simple thing like append.
I understand it appends a line after the line in which a pattern is found.
I also understand the i option which inserts a string before the line in which a pattern is found.
Is what i say right?
Anyhow, what i am trying to say what i want which is "appending" text to a same line in which a pattern is found is obviously not being correctly asked so i apologize,
Let me test the affects of the first code example you gave me and see if i get it, thanks.

Almost. Yes, the sed a command appends one or more lines of text starting on the line after the addressed line. No, the sed i command (not option) does not insert a string at the start of a line; it inserts one or more lines of text before an addressed line.

Nothing that you have described involves appending LINES of text nor inserting LINES of text. Nothing that you have described can be done with a sed a or i command because you DO NOT want to create new lines of text, you only want to add strings of text to existing lines of text, delete strings of text from existing lines of text, or modify strings of text on existing lines of text. All three of these are done with the sed s command, i.e., commands of the form that you said you already understand:

's/findstr/repstring/g'

which would more correctly be written as:

s/BRE/replacement/flags

where BRE is a basic regular expression (which in its simplest form could just be a string of regular characters, but could also be an extremely complex expression matching repeated complex patterns); replacement could be a simple string of zero or more regular characters or could include meta-characters that refer to selected portions of the text matched by the BRE; and flags (including, but not limited to, the frequently over-used g flag). And, even in that simple form, the / characters separating the command name ( s ), the BRE, the replacement, and the flags can be replaced by almost any other character (except the <newline> character) which can be useful when the BRE or the replacement contain <slash> characters.

Note also that the sed s command, the ed s command, the ex s command, the vi :s command, and the awk sub() and gsub() functions all behave very similarly (although the awk sub() and gsub() functions use extended REs (EREs) instead of the BREs used by sed , ed , ex , and vi .

I understand that there are many new things for you to come to grips with. Did you try any of the proposals and comprehend the results? Why don't you post those and your pertaining questions?
Do you understand the concept of "addresses" in sed ? The concept of regexes and details hereof (like [Dd] , meaning one single char, either "D" or "d")?