Using sed to find and append or insert on SAME line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to find and append or insert on SAME line
# 1  
Old 06-15-2016
Using sed to find and append or insert on SAME line

Hi,

Code:
$ 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

Code:
$ 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.

Last edited by Don Cragun; 06-15-2016 at 05:13 PM.. Reason: Add CODE tags.
# 2  
Old 06-15-2016
What have you tried?
# 3  
Old 06-15-2016
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)?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-15-2016
Sed usage

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"
Code:
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
Code:
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 :

Code:
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.

Last edited by RudiC; 06-15-2016 at 07:36 PM.. Reason: Added code tags.
# 5  
Old 06-15-2016
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:
Code:
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:
Quote:
The replacement may contain the special character & to refer to that portion of the pattern space which matched,
Try
Code:
sed 's/-OK/_DLY&/' file

will result in _DLY-OK printed in the output.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-15-2016
Quote:
Originally Posted by RudiC
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:
Code:
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
Code:
sed 's/-OK/_DLY&/' file

will result in _DLY-OK printed in the output.

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.

2.
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
# 7  
Old 06-15-2016
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:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

2. Shell Programming and Scripting

sed - append line after block

Hi, I posted in another section, but no reply yet. I have an ini file with sections denoted as follows (for example) blah=blah blee=blee bloo=bloo blur=blur blaa=blaa I have ksh script that needs to append a line ${line} to the end of section ${section} I saw this... (7 Replies)
Discussion started by: andyatit
7 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

sed append without using new line

im trying to append to the end of the line using sed but I want to do it without creating a new line the text to which I want to append is all in capital letters. I want to do something like this: LINE]Foo but when I do this: //a\ ] Foo it prints foo on a new line: LINE ]Foo ... (11 Replies)
Discussion started by: mrjavoman
11 Replies

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Shell Programming and Scripting

sed to find first appearance and append string

I have a file like below #GROUP A belongs to Asia GROUP A jojh hans local admin GROUP A gege fans michel jing jong #GROUP U belongs to USA GROUP U jeff goal hello world My requirement is to grep for first apperence of GROUP A which is not commented and append my name to end of file.... (12 Replies)
Discussion started by: vkk
12 Replies

7. Shell Programming and Scripting

How to append line with sed?

Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly How can it be done with sed? (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

Sed find and insert

Hi All I'm trying to insert a pattern if a pattern is found in a file. This is my sample file "PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|25 "PDA"|"Celvin"|"PRJ_AB"|"Completion_Units"|250 I would like to output as "PDA"|"Celvin"|"PRJ_NA"|"Completion_Units"|"Done"|25... (3 Replies)
Discussion started by: Celvin VK
3 Replies

9. UNIX for Dummies Questions & Answers

sed - append text to every line

Hi all I tried this on an old version of sed on NCR Unix MP-RAS: sed -e "s/$/nnn/" file1 >file2 This file (file1): the cat sat on the mat. the cat sat on the mat. the cat sat on the mat. becomes this (file2): the cat sat on the mat.nnn the cat sat on the mat.nnn nnn the... (3 Replies)
Discussion started by: jgrogan
3 Replies

10. Shell Programming and Scripting

How to insert/append line using hamilton cshell

Hi everyone I just need a script that should just search for a particular keyword in a line and after that it should add some text to the next line. I have tried it in HAMILTON CSHELL using sed "hello/a\newtext" file.txt But it give me message sed: "\" must terminate the "a"... (4 Replies)
Discussion started by: sarbjit
4 Replies
Login or Register to Ask a Question