Remove text from file after only a specific match

Clash Royale CLAN TAG#URR8PPP
I have a file with multiple occurances of a string, I need to remove only one of the occurrences using a specific match. I cannot seem to get a good way of doing this, i keep removing all occurrences
sed -i "s/|item2//g"
file format:
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|item2",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
What i need to do is to match the first set of numbers say 3454343345 then after this remove a specific string, like item2.
Output I am trying to get
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
text-processing sed
|
show 1 more comment
I have a file with multiple occurances of a string, I need to remove only one of the occurrences using a specific match. I cannot seem to get a good way of doing this, i keep removing all occurrences
sed -i "s/|item2//g"
file format:
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|item2",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
What i need to do is to match the first set of numbers say 3454343345 then after this remove a specific string, like item2.
Output I am trying to get
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
text-processing sed
3
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
ok its done - thanks
– user4555
Feb 19 at 10:33
just remove the 'g' from yoursedcommand ....
– RoVo
Feb 19 at 10:33
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36
|
show 1 more comment
I have a file with multiple occurances of a string, I need to remove only one of the occurrences using a specific match. I cannot seem to get a good way of doing this, i keep removing all occurrences
sed -i "s/|item2//g"
file format:
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|item2",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
What i need to do is to match the first set of numbers say 3454343345 then after this remove a specific string, like item2.
Output I am trying to get
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
text-processing sed
I have a file with multiple occurances of a string, I need to remove only one of the occurrences using a specific match. I cannot seem to get a good way of doing this, i keep removing all occurrences
sed -i "s/|item2//g"
file format:
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|item2",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
What i need to do is to match the first set of numbers say 3454343345 then after this remove a specific string, like item2.
Output I am trying to get
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
text-processing sed
text-processing sed
edited Feb 19 at 10:51
user4555
asked Feb 19 at 10:24
user4555user4555
32
32
3
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
ok its done - thanks
– user4555
Feb 19 at 10:33
just remove the 'g' from yoursedcommand ....
– RoVo
Feb 19 at 10:33
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36
|
show 1 more comment
3
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
ok its done - thanks
– user4555
Feb 19 at 10:33
just remove the 'g' from yoursedcommand ....
– RoVo
Feb 19 at 10:33
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36
3
3
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
ok its done - thanks
– user4555
Feb 19 at 10:33
ok its done - thanks
– user4555
Feb 19 at 10:33
just remove the 'g' from your
sed command ....– RoVo
Feb 19 at 10:33
just remove the 'g' from your
sed command ....– RoVo
Feb 19 at 10:33
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36
|
show 1 more comment
1 Answer
1
active
oldest
votes
Try this,
sed -e '/^"3454343345/ s/item2//' file
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
- Replace
-ewith-ito edit inline, if the output matches your desired result.
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholderitem1(for example) happens to be the same as the string3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will removeitem2as a substring from anywhere on the line (e.g. fromtheitem22). Also, again, while "trying" asedcommand, never use-i. Doing so would mess up the data if the command happens to be wrong.
– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501551%2fremove-text-from-file-after-only-a-specific-match%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this,
sed -e '/^"3454343345/ s/item2//' file
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
- Replace
-ewith-ito edit inline, if the output matches your desired result.
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholderitem1(for example) happens to be the same as the string3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will removeitem2as a substring from anywhere on the line (e.g. fromtheitem22). Also, again, while "trying" asedcommand, never use-i. Doing so would mess up the data if the command happens to be wrong.
– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
add a comment |
Try this,
sed -e '/^"3454343345/ s/item2//' file
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
- Replace
-ewith-ito edit inline, if the output matches your desired result.
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholderitem1(for example) happens to be the same as the string3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will removeitem2as a substring from anywhere on the line (e.g. fromtheitem22). Also, again, while "trying" asedcommand, never use-i. Doing so would mess up the data if the command happens to be wrong.
– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
add a comment |
Try this,
sed -e '/^"3454343345/ s/item2//' file
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
- Replace
-ewith-ito edit inline, if the output matches your desired result.
Try this,
sed -e '/^"3454343345/ s/item2//' file
"1234567890|item2",
"1234567895|item2",
"3454343345|item1|",
"7854343345|item1|item3",
"0454343555|item1|item4|item6",
- Replace
-ewith-ito edit inline, if the output matches your desired result.
edited Feb 19 at 11:07
answered Feb 19 at 10:59
msp9011msp9011
4,38044067
4,38044067
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholderitem1(for example) happens to be the same as the string3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will removeitem2as a substring from anywhere on the line (e.g. fromtheitem22). Also, again, while "trying" asedcommand, never use-i. Doing so would mess up the data if the command happens to be wrong.
– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
add a comment |
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholderitem1(for example) happens to be the same as the string3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will removeitem2as a substring from anywhere on the line (e.g. fromtheitem22). Also, again, while "trying" asedcommand, never use-i. Doing so would mess up the data if the command happens to be wrong.
– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
Thanks this works ! :)
– user4555
Feb 19 at 11:01
Thanks this works ! :)
– user4555
Feb 19 at 11:01
If the placeholder
item1 (for example) happens to be the same as the string 3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will remove item2 as a substring from anywhere on the line (e.g. from theitem22). Also, again, while "trying" a sed command, never use -i. Doing so would mess up the data if the command happens to be wrong.– Kusalananda
Feb 19 at 11:01
If the placeholder
item1 (for example) happens to be the same as the string 3454343345, this would remove the wrong things. You should anchor the pattern to the start of the line. Also, note that this will remove item2 as a substring from anywhere on the line (e.g. from theitem22). Also, again, while "trying" a sed command, never use -i. Doing so would mess up the data if the command happens to be wrong.– Kusalananda
Feb 19 at 11:01
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
@Kusalananda ur right....
– msp9011
Feb 19 at 11:05
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501551%2fremove-text-from-file-after-only-a-specific-match%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
You say you want to match with the numbers, then why don't you use the numbers for matching? What should the output look like?
– Kusalananda
Feb 19 at 10:26
Yes i tried to pipe using grep, this infact does what i want in the output but i need to do this as a file edit not stdout grep -w "3454343345" file | sed -i "s/|pattern//g" output: "3454343345|pattern2",
– user4555
Feb 19 at 10:30
ok its done - thanks
– user4555
Feb 19 at 10:33
just remove the 'g' from your
sedcommand ....– RoVo
Feb 19 at 10:33
Are your lines quoted like that?
– Kusalananda
Feb 19 at 10:36