replace multiple strings in a file in python3 [on hold]
up vote
-1
down vote
favorite
I have test file where i have some entries like below and i need to change memberUid
to nisNetgroupTriple
along with the names
$ cat testfile2
memberUid: tony
memberUid: jacob
memberUid: zenny
memberUid: helori
memberUid: honies
memberUid: hunter
However, i'm able to change the memberUid
to nisNetgroupTriple
with unix sed
like %s/memberUid/nisNetgroupTriple/g
and the angain using awk
like awk '{print $1, "(-,"$2",)"}' testfile2
but this two way process and i'm this can be done more elegantly which i'm not getting now.
Desired Output:
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
awk sed python
put on hold as unclear what you're asking by Sparhawk, RalfFriedl, G-Man, msp9011, schily 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I have test file where i have some entries like below and i need to change memberUid
to nisNetgroupTriple
along with the names
$ cat testfile2
memberUid: tony
memberUid: jacob
memberUid: zenny
memberUid: helori
memberUid: honies
memberUid: hunter
However, i'm able to change the memberUid
to nisNetgroupTriple
with unix sed
like %s/memberUid/nisNetgroupTriple/g
and the angain using awk
like awk '{print $1, "(-,"$2",)"}' testfile2
but this two way process and i'm this can be done more elegantly which i'm not getting now.
Desired Output:
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
awk sed python
put on hold as unclear what you're asking by Sparhawk, RalfFriedl, G-Man, msp9011, schily 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
@tink, sorry for making it not clear, i just need to process thetestfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.
– pygo
2 days ago
Do all lines of your input file startmemberUid:
? If not, please show some other example lines, and their desired output.
– JigglyNaga
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have test file where i have some entries like below and i need to change memberUid
to nisNetgroupTriple
along with the names
$ cat testfile2
memberUid: tony
memberUid: jacob
memberUid: zenny
memberUid: helori
memberUid: honies
memberUid: hunter
However, i'm able to change the memberUid
to nisNetgroupTriple
with unix sed
like %s/memberUid/nisNetgroupTriple/g
and the angain using awk
like awk '{print $1, "(-,"$2",)"}' testfile2
but this two way process and i'm this can be done more elegantly which i'm not getting now.
Desired Output:
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
awk sed python
I have test file where i have some entries like below and i need to change memberUid
to nisNetgroupTriple
along with the names
$ cat testfile2
memberUid: tony
memberUid: jacob
memberUid: zenny
memberUid: helori
memberUid: honies
memberUid: hunter
However, i'm able to change the memberUid
to nisNetgroupTriple
with unix sed
like %s/memberUid/nisNetgroupTriple/g
and the angain using awk
like awk '{print $1, "(-,"$2",)"}' testfile2
but this two way process and i'm this can be done more elegantly which i'm not getting now.
Desired Output:
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
awk sed python
awk sed python
edited 2 days ago
Rui F Ribeiro
38.5k1479128
38.5k1479128
asked 2 days ago
pygo
1064
1064
put on hold as unclear what you're asking by Sparhawk, RalfFriedl, G-Man, msp9011, schily 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by Sparhawk, RalfFriedl, G-Man, msp9011, schily 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
@tink, sorry for making it not clear, i just need to process thetestfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.
– pygo
2 days ago
Do all lines of your input file startmemberUid:
? If not, please show some other example lines, and their desired output.
– JigglyNaga
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago
add a comment |
2
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
@tink, sorry for making it not clear, i just need to process thetestfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.
– pygo
2 days ago
Do all lines of your input file startmemberUid:
? If not, please show some other example lines, and their desired output.
– JigglyNaga
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago
2
2
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
@tink, sorry for making it not clear, i just need to process the
testfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.– pygo
2 days ago
@tink, sorry for making it not clear, i just need to process the
testfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.– pygo
2 days ago
Do all lines of your input file start
memberUid:
? If not, please show some other example lines, and their desired output.– JigglyNaga
2 days ago
Do all lines of your input file start
memberUid:
? If not, please show some other example lines, and their desired output.– JigglyNaga
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Try this:
sed -r 's/^memberUid: (.*)/nisNetgroupTriple: (-,1,)/' testfile2
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
nisNetgroupTriple: (-,zenny,)
nisNetgroupTriple: (-,helori,)
nisNetgroupTriple: (-,honies,)
nisNetgroupTriple: (-,hunter,)
Using sed with search and replace and a capturing group (.*)
.
In the replacement string we just construct your desired format, and the 1
fills in what was captured in the parenthesis.
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
add a comment |
up vote
1
down vote
As you asked for an answer using Python3, here is one using Python regex:
import re
f = open(testfile2).read()
re.sub(r'(memberUid)(:s)(w+)',r'nisNetgroupTriple2(-,3,)',f)
Explanation:
re
is the Python's regex module providingre.sub
method which replaces a pattern with another pattern in the provided string in the following manner:
re.sub(r'original-pattern', r'replacement-pattern', inputString)
- The brackets used in the pattern are used for capturing groups in the original pattern. E.g. in our case, we have captured 3 groups:
memberUid
,:s
andw+
. Then, we can reference back to these groups in our replacement pattern as1
,2
and so on.
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
add a comment |
up vote
0
down vote
As the first part of each input line will always be replaced, the sed
is redundant. All you need is
awk '{print "nisNetgroupTriple: (-,"$2",)"}'
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this:
sed -r 's/^memberUid: (.*)/nisNetgroupTriple: (-,1,)/' testfile2
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
nisNetgroupTriple: (-,zenny,)
nisNetgroupTriple: (-,helori,)
nisNetgroupTriple: (-,honies,)
nisNetgroupTriple: (-,hunter,)
Using sed with search and replace and a capturing group (.*)
.
In the replacement string we just construct your desired format, and the 1
fills in what was captured in the parenthesis.
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
add a comment |
up vote
1
down vote
accepted
Try this:
sed -r 's/^memberUid: (.*)/nisNetgroupTriple: (-,1,)/' testfile2
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
nisNetgroupTriple: (-,zenny,)
nisNetgroupTriple: (-,helori,)
nisNetgroupTriple: (-,honies,)
nisNetgroupTriple: (-,hunter,)
Using sed with search and replace and a capturing group (.*)
.
In the replacement string we just construct your desired format, and the 1
fills in what was captured in the parenthesis.
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this:
sed -r 's/^memberUid: (.*)/nisNetgroupTriple: (-,1,)/' testfile2
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
nisNetgroupTriple: (-,zenny,)
nisNetgroupTriple: (-,helori,)
nisNetgroupTriple: (-,honies,)
nisNetgroupTriple: (-,hunter,)
Using sed with search and replace and a capturing group (.*)
.
In the replacement string we just construct your desired format, and the 1
fills in what was captured in the parenthesis.
Try this:
sed -r 's/^memberUid: (.*)/nisNetgroupTriple: (-,1,)/' testfile2
nisNetgroupTriple: (-,tony,)
nisNetgroupTriple: (-,jacob,)
nisNetgroupTriple: (-,zenny,)
nisNetgroupTriple: (-,helori,)
nisNetgroupTriple: (-,honies,)
nisNetgroupTriple: (-,hunter,)
Using sed with search and replace and a capturing group (.*)
.
In the replacement string we just construct your desired format, and the 1
fills in what was captured in the parenthesis.
edited 2 days ago
answered 2 days ago
tink
4,07511218
4,07511218
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
add a comment |
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
I'll try it , thnx tink. would appreciate little explanation though.
– pygo
2 days ago
Done ... makes sense?
– tink
2 days ago
Done ... makes sense?
– tink
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
completely make sense :-) thnx dude.
– pygo
2 days ago
add a comment |
up vote
1
down vote
As you asked for an answer using Python3, here is one using Python regex:
import re
f = open(testfile2).read()
re.sub(r'(memberUid)(:s)(w+)',r'nisNetgroupTriple2(-,3,)',f)
Explanation:
re
is the Python's regex module providingre.sub
method which replaces a pattern with another pattern in the provided string in the following manner:
re.sub(r'original-pattern', r'replacement-pattern', inputString)
- The brackets used in the pattern are used for capturing groups in the original pattern. E.g. in our case, we have captured 3 groups:
memberUid
,:s
andw+
. Then, we can reference back to these groups in our replacement pattern as1
,2
and so on.
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
add a comment |
up vote
1
down vote
As you asked for an answer using Python3, here is one using Python regex:
import re
f = open(testfile2).read()
re.sub(r'(memberUid)(:s)(w+)',r'nisNetgroupTriple2(-,3,)',f)
Explanation:
re
is the Python's regex module providingre.sub
method which replaces a pattern with another pattern in the provided string in the following manner:
re.sub(r'original-pattern', r'replacement-pattern', inputString)
- The brackets used in the pattern are used for capturing groups in the original pattern. E.g. in our case, we have captured 3 groups:
memberUid
,:s
andw+
. Then, we can reference back to these groups in our replacement pattern as1
,2
and so on.
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
As you asked for an answer using Python3, here is one using Python regex:
import re
f = open(testfile2).read()
re.sub(r'(memberUid)(:s)(w+)',r'nisNetgroupTriple2(-,3,)',f)
Explanation:
re
is the Python's regex module providingre.sub
method which replaces a pattern with another pattern in the provided string in the following manner:
re.sub(r'original-pattern', r'replacement-pattern', inputString)
- The brackets used in the pattern are used for capturing groups in the original pattern. E.g. in our case, we have captured 3 groups:
memberUid
,:s
andw+
. Then, we can reference back to these groups in our replacement pattern as1
,2
and so on.
As you asked for an answer using Python3, here is one using Python regex:
import re
f = open(testfile2).read()
re.sub(r'(memberUid)(:s)(w+)',r'nisNetgroupTriple2(-,3,)',f)
Explanation:
re
is the Python's regex module providingre.sub
method which replaces a pattern with another pattern in the provided string in the following manner:
re.sub(r'original-pattern', r'replacement-pattern', inputString)
- The brackets used in the pattern are used for capturing groups in the original pattern. E.g. in our case, we have captured 3 groups:
memberUid
,:s
andw+
. Then, we can reference back to these groups in our replacement pattern as1
,2
and so on.
edited 2 days ago
answered 2 days ago
shivams
2,86611425
2,86611425
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
add a comment |
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
@ Shivams, thnx Dude , it works perfectly +1
– pygo
2 days ago
add a comment |
up vote
0
down vote
As the first part of each input line will always be replaced, the sed
is redundant. All you need is
awk '{print "nisNetgroupTriple: (-,"$2",)"}'
add a comment |
up vote
0
down vote
As the first part of each input line will always be replaced, the sed
is redundant. All you need is
awk '{print "nisNetgroupTriple: (-,"$2",)"}'
add a comment |
up vote
0
down vote
up vote
0
down vote
As the first part of each input line will always be replaced, the sed
is redundant. All you need is
awk '{print "nisNetgroupTriple: (-,"$2",)"}'
As the first part of each input line will always be replaced, the sed
is redundant. All you need is
awk '{print "nisNetgroupTriple: (-,"$2",)"}'
answered 2 days ago
JigglyNaga
3,569829
3,569829
add a comment |
add a comment |
2
I'm not sure I get what you're asking. Can you rephrase that? Do you need to modify the file by any means? Are you suggesting that sed can't complete the whole job? What has python3 got to do with any of this?
– tink
2 days ago
@tink, sorry for making it not clear, i just need to process the
testfile2
file in a Desired format which i mentioned in the question, sed of course will do it only thing i'm not getting it on the top of my head as of now. python is just the sake of alternative and way to good to know.– pygo
2 days ago
Do all lines of your input file start
memberUid:
? If not, please show some other example lines, and their desired output.– JigglyNaga
2 days ago
@JigglyNaga, yes thats true..
– pygo
2 days ago