0

I'm trying to delete a character that is at the start of the line of multiple string, but, also have another pattern just before the start of the line, which i don't want to delete:

1:string:test...
2:string test
3:etc...

to

1 string:test...
2 string test
3 etc...

Some string can also contain the character : which make things interesting. So far i tried:

echo "1:test:" | grep -Pv '(?<=\d):'

which does match the : character (at least it highlight it, so I'm guessing it's matched) but it doesn't get omitted when using the -v flag, maybe because it's on the same line as the match? (Here i only did experiment on how to match it and remove it instead of replacing it with a space, as the aforementioned example showed)

echo "1:test:" | sed 's/^.:./ /g'

This seems to be the closest i got, but it cut off the rest of the start beside the : character, which give:

est:

instead of

1 test:

9
  • You say you want to delete a pattern after another pattern.  I guess the second pattern (the one that you want to delete) is : (colon); what’s the first one?  What do you want to do with input like 42:days, foo:bar, The quick:brown fox or :quux?  For that matter, what do you want to do with input that doesn’t contain any colons?  Also, you say that you want to delete the colon, but then you show the output you want, and the colons have been replaced by spaces. Aug 17 '20 at 3:31
  • This is a trivial problem (unless there’s more here than meets the eye, which seems unlikely).  And the OP already knows the answer; they don’t need our help.  And the title is so arcane, and the wording of the question is so unclear, that nobody who has this problem will ever find this question. Aug 17 '20 at 3:31
  • it is, but i didn't have the solution right away, and only until a couple minutes/hour pass, and so i just answered myself...thought it was a waste to delete this since it might help someone. Aug 17 '20 at 5:05
  • And to be honest, there is dozen more of more popular and "trivial" question, especially about regex, so i wouldn't say it's useless, even if it's easy to answer from a certain perspective. Aug 17 '20 at 5:06
  • btw...why should it be closed when it's already answered? Aug 17 '20 at 5:08
0

this works:

echo -e "1:test:\n2:test\n3:test:\n" | sed '/:/s// /'

Print

1 test:
2 test
3 test:
1
0

Python

#!/usr/bin/python
import re
k=re.compile(":")
m=open('filename','r')
for i in m:
    print i.replace(":"," ",1).strip()

awk command works only for mentioned example

awk  -F ":" '{print substr($0,1,1),substr($0,3)}' filename

sed command

  sed "s/:/ /1" filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.