Using Sed to delete characters between the 10th character and the first period after the 10th

StackOverflow https://stackoverflow.com/questions/23619541

  •  21-07-2023
  •  | 
  •  

سؤال

I want to remove all characters between the 10th character of each line and the first period that comes after.

Say I have input that looks like

0123456789 foo. foo....
1234566789 bar. bar...
0912309299 foobar. foobar..

Output will be something like

0123456789. foo....
1234566789. bar...
0912309299. foobar..

Is there a sed command that does this?

Thanks

هل كانت مفيدة؟

المحلول

This is one way to make it:

$ sed -r 's/^([0-9]{10})[^.]*/\1/' file
0123456789. foo....
1234566789. bar...
0912309299. foobar..

Or also (to get 10 first chars in general, no matter if they are a number or not):

sed -r 's/^(.{10})[^.]*/\1/'

Explanation

  • -r allow extended regex.
  • sed 's/find/replace/' is the basic substitution: replace find with replace once. In this case, we use the following:
  • ^([0-9]{10})[^.]* catch first 10 digits. Also, catch all characters up to a dot ..
  • \1 print back the first block.

This is a schema of the substitution:

0123456789 foo. foo....
1234566789 bar. bar...
0912309299 foobar. foobar..
^^^^^^^^^^       ^^^^^^^^^^
     |    ^^^^^^^     |
     |         |      |
^([0-9]{10})  [^.]*   |
      |               |
      |          |----
^^^^^^^^^^-------------
0123456789. foo....
1234566789. bar...
0912309299. foobar..

نصائح أخرى

I think this should work.

sed -P 's/(?<=^.{10}).*?\.//'

EDIT: This is obviously wrong, as pointed out by Kent below, I mistakenly thought that sed supported -P for perl compatible regexes, this sent me down a rabbit hole of figuring out how to get perl compatible regexes in sed, one solution offered by kent was ssed but I didn't want to do that. long story short, here's the perl script:

perl -pe 's/(?<=^.{10}).*?(?=\.)//' example.txt
0123456789. foo....
1234566789. bar...
0912309299. foobar..

and the post that sullied my hithertoo clean mind with perl knowledge. I'd never checked out perl but that's a handy post for when you want to extend your bash-fu just that little bit beyond sed or awk. As is obviously pointed out by the answer above me, none of this is necessary in this case, just for interest.

For your data this awk gives correct output.

awk '{$2="";sub(/ /,".")}1' file
0123456789. foo....
1234566789. bar...
0912309299. foobar..

Just remove second field and change first space to a .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top