0

Basically I need what the OP of this question got but didn't want. How to delete everything after a certain pattern or a string in a file?

So I got the data like:

Something and something ........................... 23
Another one .......................................123
Somethingelse Inc. .................................243

So what I want is to delete the whole bunch of dots (and the numbers) after the space.

So far, I tried to use the OP's command there but it didn't quite work as I expected because it deletes everything after the first dot it encounters. Which means that the dot on the data of the sort Somethinelse Inc. is also deleted.

I tried to make the first (sed) answer from that question work by replacing the first .com with three dots and the second one with an empty space but I failed. This time everything after the first space is deleted or three dots left.

The second answer (ex -sc ...) worked like the OP wanted but it leaves some dots behind for me so I had no luck with it either.

I also tried to tweak the command from this answer but also failed at that. How can I delete everything until a pattern and everything after another pattern from a line?

  • sed 's/ \.\..*$//' /path/to/file should work. – DopeGhoti Nov 1 '16 at 16:58
  • It works. Thank you. Why didn't you put it as an answer? – Shaban Shneta Nov 1 '16 at 17:00
  • Because you said sed didn't work, I was expecting clarification as to what is didn't do properly. I'll copy it into an answer shortly. – DopeGhoti Nov 1 '16 at 17:01
1

sed 's/ \.\..*$//' /path/to/file should work:

 \.\. - A space followed by two literal periods
.*    - One or characters of any type
$     - End of line 
0

Another but unelegant solution could be:

cat path/to/yourfile | sed -E "s/[\.]{2,}//g" | sed "s/[0-9]//g" > path/to/new_file

where:

  • -E Interpret regular expressions as extended (modern) regular expressions
  • [\.]{2,} match a dot per two or more occurrences
  • [0-9] match all digits

  • s/expr//g means substitute matched expr with nothing as much as you can (g)

0

How about this:

sed 's/\.\+ *[[:digit:]]\+[[:space:]]*$//g'

which means:

one+to+many (\+) dots (\.) AND zero-to-many (*) spaces ( ) AND one+to+many (\+) digits([[:digit:]]) AND just-in-case any (*) whitespace ([[:space:]]) AND End of this line ($)

Tested with (Note that i inserted "...234" in 2nd row, and trailing Tab/spaces):

Something and something ........................... 23<Tab>
An ...234 other one .......................................123<space>
Somethingelse Inc. .................................243<some spaces>

And the result:

xb@dnxb:/tmp$ sed 's/\.\+ *[[:digit:]]\+[[:space:]]*$//g' sample.txt
Something and something 
An ...234 other one 
Somethingelse Inc. 
xb@dnxb:/tmp$ 
  • I accepted the other answer because it worked for me but it'd be nice if you could explain a bit what your answer does or what does it do differently. – Shaban Shneta Nov 1 '16 at 17:09
  • @ShabanShneta updated. – 林果皞 Nov 1 '16 at 17:18

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.