sed appending needed only after first instance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed appending needed only after first instance
# 8  
Old 02-15-2011
Quote:
Originally Posted by yinyuemi
Code:
sed '0,/<falsemodule-option>/ {
/<falsemodule-option>/a\<LdapLogin>
}' file

awk is also a good way to try:

Code:
awk '/<falsemodule-option>/{p++} /<falsemodule-option>/ && p==1 {$0= $0"\n<LdapLogin>"}1'

Looks like only awk can help me in this scenario.
This helped me. now i would like to have some text written before the pattern.
Well, please can you tell me the syntax to write the text before the pattern that i am searching for ?

Thanks.

---------- Post updated at 01:38 PM ---------- Previous update was at 01:34 PM ----------

Quote:
Originally Posted by yinyuemi
Code:
sed '0,/<falsemodule-option>/ {
/<falsemodule-option>/a\<LdapLogin>
}' file

awk is also a good way to try:

Code:
awk '/<falsemodule-option>/{p++} /<falsemodule-option>/ && p==1 {$0= $0"\n<LdapLogin>"}1'

Looks like only awk can help me in this scenario.
This helped me. now i would like to have some text written before the pattern.
Well, please can you tell me the syntax to write the text before the pattern that i am searching for ?

Thanks.
# 9  
Old 02-15-2011
Code:
awk '/<falsemodule-option>/{p++} /<falsemodule-option>/ && p==1 {$0="<LdapLogin>\n"$0}1'

This User Gave Thanks to yinyuemi For This Post:
# 10  
Old 02-15-2011
You can do it using sed:
Code:
sed '1,/<falsemodule-option>/s/<falsemodule-option>/<LdapLogin>/' input_file

# 11  
Old 02-16-2011
Quote:
Originally Posted by Shell_Life
You can do it using sed:
Code:
sed '1,/<falsemodule-option>/s/<falsemodule-option>/<LdapLogin>/' input_file

I tried but it is doing the job of replacing rather than inserting a line before the pattern.

---------- Post updated at 03:35 AM ---------- Previous update was at 03:34 AM ----------

Quote:
Originally Posted by yinyuemi
Code:
awk '/<falsemodule-option>/{p++} /<falsemodule-option>/ && p==1 {$0="<LdapLogin>\n"$0}1'



Thanks. do we have a '-i' flag like we have in sed which allows the changes to take right on the file upon execution.
I want the changes to be done on the file directly and not on the STDOUT
something like for example
Code:
sed -i '/pattern/a\text' FILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

2. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

3. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

4. Shell Programming and Scripting

help needed to put instance numbers

Hi All I need help am having a source file as below emp dept class subclass region country division first i need to get line count and i need to divide by 3 it is an parameter passing value number of lines 7 (8 Replies)
Discussion started by: ragu.selvaraj
8 Replies

5. UNIX for Dummies Questions & Answers

Help needed in Appending multiple lines

Hello, There is a log file A.log where new lines are getting added every minute. When ever any new lines are getting added in to A , the same lines needs to be appended to B.log I need to append these newly added lines to end of another file B through a shell script. I tried CAT but its... (2 Replies)
Discussion started by: twisterboy
2 Replies

6. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

7. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

8. Shell Programming and Scripting

sed command - substitue first instance

hi i have one file where i want to substitute only first instance of swap with swap1 i want to replcae only first instance of swap in my script i know we can do this with awk. but i need to do this with sed only i tried follwoing code sed 's/swap/swap1' filename but here all... (15 Replies)
Discussion started by: d_swapneel14
15 Replies

9. Shell Programming and Scripting

Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like: stuff morestuff 0 otherthing 0 etc I want to substitute for the 0 but what I want to... (9 Replies)
Discussion started by: eltinator
9 Replies

10. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies
Login or Register to Ask a Question