1

I have the following comma-delimited data (spaces used for visual clarity):

 1 2014 23 2352 19.24 7.18
 1 2014 23 2353 19.78 4.65
24 2014 23 2354 20.26 25.34
 1 2014 23 2355 26.65 42.76
 1 2014 23 2356 27.34 56.356
 1 2014 23 2357 29.24 45.4
24 2014 23 2358 30.23 345.27
 1 2014 23 2359 23.23 345.21
 1 2014 24 0000 12.53 1.98
 1 2014 24 0001 7.24  12.3
24 2014 24 0002 15.68 90.8

I would like to remove only every line that starts with 24 (or keep every line that starts with 1, either way).

So the desired output would be:

 1 2014 23 2352 19.24 7.18
 1 2014 23 2353 19.78 4.65
 1 2014 23 2355 26.65 42.76
 1 2014 23 2356 27.34 56.356
 1 2014 23 2357 29.24 45.4
 1 2014 23 2359 23.23 345.21
 1 2014 24 0000 12.53 1.98
 1 2014 24 0001 7.24  12.3

I have tried

sed '/24/d' (filename)

But that removes every instance of 24 throughout the file, like this (not desired!):

1 2014 23 2353 19.78 4.65
1 2014 23 2355 26.65 42.76
1 2014 23 2356 27.34 56.356
1 2014 23 2359 23.23 345.21
1
  • 3
    Use it with ^ (starting anchor): sed '/^24/d' filename or sed '/^1/!d' filename ? Dec 28 '14 at 12:11
1

With awk (prints everything with 1 at the beginning of the line):

awk '/^1/{print $0}'

With sed (-n option to suppress printing of everything):

sed -n '/^1/p'

With grep (perhaps not the best choice for text processing, but good enough in this case):

grep '^1'
1
  • excellent, thank you! I especially liked the explanations. Dec 28 '14 at 19:52
1

grep -v ^24 to keep only lines that do not start with 24

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.