0

I need to search a line of numbers for any columns that end with 999 and then delete that entire column while preserving any leading number (i.e. 8599999.... --> 85)

Data Line:

66 72 79 84 87 90 91 92 92 93 92 91 89 85999999999999999999999999999999999

Desired Output:

66 72 79 84 87 90 91 92 92 93 92 91 89 85
Aaron Perry
  • 881
  • 1
  • 10
  • 29

3 Answers3

3

Using sed

$ s='66 72 79 84 87 90 91 92 92 93 92 91 89 85999999999999999999999999999999999'

$ # POSIX sed
$ echo "$s" | sed 's/999\{1,\}//g'
66 72 79 84 87 90 91 92 92 93 92 91 89 85

$ # sed with ERE support
$ echo "$s" | sed -E 's/999+//g'
66 72 79 84 87 90 91 92 92 93 92 91 89 85

$ # with GNU sed
$ echo "$s" | sed 's/999\+//g'
66 72 79 84 87 90 91 92 92 93 92 91 89 85

This deletes all occurrences of 99 followed by one or more 9 anywhere in the line

See also: Reference - What does this regex mean?

Sundeep
  • 19,273
  • 2
  • 19
  • 42
  • 1
    You need to anchor that with `/999\+$/` or you'll turn `85999123` into `85123` the first time it occurs on the line. – Ed Morton Jul 28 '16 at 17:25
  • well, question said `any column`.. and there are other cases like you point out.. if OP clarifies, I'll change accordingly – Sundeep Jul 29 '16 at 01:03
  • no, it won't remove succeeding columns.. only particular sequence of 999 followed by any number of 9s, anywhere in the line gets deleted.. for ex: `echo '66 7999 :9999: 99' | sed 's/999\+//g'` gives `66 7 :: 99` – Sundeep Jul 29 '16 at 01:07
1

In AWK:

awk '{gsub(/999+$/,"")}1' test.in

Let's see:

$ cat > test.in
1 2 3
4 5 69999999
$ awk '{gsub(/999+$/,"")}1' test.in
1 2 3
4 5 6
James Brown
  • 31,411
  • 6
  • 31
  • 52
  • You're only doing 1 substitution so that should be `sub()` instead of `gsub()` but more importantly that will delete any line that doesn't end in `999+`. To avoid surprises like that, only use the result of an action as a condition if it's required and you've thought through all of the consequences, otherwise use `{action}1` instead of `action`. – Ed Morton Jul 28 '16 at 17:29
  • Lol, so it seems. Fixed. Thanks for pointing it out. Too many edits in too short time. – James Brown Jul 28 '16 at 18:51
1

with sed;

sed  -e 's/999\+$//' data.txt
sozkul
  • 655
  • 4
  • 7
  • That will delete from the first `999` to the end of the line, not a chain of 3 or more 9s at the end of a line as required. – Ed Morton Jul 28 '16 at 17:30