How can I clean up lines in a text file using sed or awk?

0
2
Asked By CuriousCat123 On

I'm dealing with some text files containing lists of numbers. Each number is on a separate line. Some numbers have forward slashes included (like 11152/3), and some don't (like 11276), which is okay. However, there are also lines that contain just a mix of slashes and spaces with no actual numbers. Is there any way to use sed or awk to remove those unwanted slashes while keeping the valid numbers intact?

3 Answers

Answered By TechieTinker On

You might want to try using `grep '[0-9]'` to filter out any lines that don’t have any digits. It can effectively remove those unwanted lines with just slashes and spaces.

Answered By CodeWizard99 On

While `grep` is an option, since you’re looking specifically to use `awk` or `sed`, you can do something like `awk '/[0-9]/ {print}'` or for `sed` try `sed -n '/[0-9]/p'`. Both will help in keeping lines with actual numbers.

Answered By DataDabbler On

It’d be really helpful to see some examples of your data. Specifically, share lines that are correct (no slashes), those that are correct with slashes, and lastly, the lines that contain only slashes. This way, we can better understand the patterns you’re working with!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.