0

This question already has an answer here:

Consider I've a large file containing information like (part of cat file):

33.829037   -0.113737   1.157153
33.830036   -0.113620   1.157157
33.831036   -0.113495   1.157169
33.832035   -0.113365   1.157191
33.833035   -0.113242   1.157228
33.834034   -0.113157   1.157273
33.835033   -0.113071   1.157300

The first column contains float numbers in ascending order and suppose I want to remove all the line after 33.832035 so-that output should be:

33.829037   -0.113737   1.157153
33.830036   -0.113620   1.157157
33.831036   -0.113495   1.157169
33.832035   -0.113365   1.157191

How do I do that with sed or appropriate text-processing tool?

I've tried Deleting all lines after first occurrence of a string in a line but haven't succeed in implementing in my case.

marked as duplicate by Pandya, Community Mar 2 '18 at 16:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

A slight variant of one of the solutions to the linked question will include the first matching line:

sed '/33.832035/q'

This will print all lines up to and including the first line containing “33.832035”, then quit.

Any other text processing language will have a similar equivalent; for instance in AWK:

awk '1; /33.832035/ { exit }'

This prints every line (1, which always matches and causes AWK to apply its default action, which is to print the current line), and exits when the current line matches “33.832035“.

In both cases you can make the regular expression more restrictive, e.g. /^33.832035 / to only match a full field at the start of the line.

  • Thanks. Though Gilles had told me but I didn't understand due to my poor knowledge with sed. – Pandya Mar 2 '18 at 15:42
  • oh! so, p is not necessary? – Pandya Mar 2 '18 at 15:51
  • 1
    Exactly, dropping -n means p isn’t necessary. sed’s prints the pattern space by default, which is what p does too; -n disables that behaviour and is only necessary when you don’t want to print all processed lines (which isn’t the case here). – Stephen Kitt Mar 2 '18 at 15:52
  • Hmm. when I tried what Gilles said, it was not working, now working; don't know why. Anyway, then I think that comment already answered my question! – Pandya Mar 2 '18 at 15:54
1

Awk approach:

awk '$1 == 33.832035{ print; exit }1' file

Sample output:

33.829037   -0.113737   1.157153
33.830036   -0.113620   1.157157
33.831036   -0.113495   1.157169
33.832035   -0.113365   1.157191
  • How does 1 after bracket work? – Pandya Mar 2 '18 at 15:45
  • That’s a common AWK trick to use the default action, which is to print the current line. The AWK command above says “on lines whose first field matches “33.832035”, print the line, then exit; on all other lines, print the line”. – Stephen Kitt Mar 2 '18 at 15:46

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