[SOLVED] sed or awk to remove $8 in a | delimited file if non-numerical value exists
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed or awk to remove $8 in a | delimited file if non-numerical value exists
Hello,
I have a question for you all, I have a text file that is delimited with | between fields. The file has roughly half a million lines in it. I am looking for the fastest way to remove the lines in this file or generate a new file if the pattern in field $8 is non-numerical.
I could write a for loop with an if statement to do this with cut, but it would take forever to run, so I'm looking for a good awk or sed one liner that could do this instead.
There are many ways to do this, either managing the file directly with a sed statement, or by awk'ing it and outputting everything to another file - but I'm asking the community on what they think the most efficient way to do this, please.
So you want to keep the entire line if $8 is numerical or you want to remove those?
Do you need it to handle large numbers or just one or two digits?
This will be far easier if you paste an example of how the data is now and how you wish it to end up: e.g.,
"I have this:"
Code:
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
a | b | c | d | e | f | g | h | i | j
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
"I want this:"
Code:
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
a | b | c | d | e | f | g | h | i | j
Actually we just figured it out - basically it was to exclude the line entirely if it contained anything other than strictly numbers (including alpha-numeric values), here is what we came up with which did the trick:
Actually we just figured it out - basically it was to exclude the line entirely if it contained anything other than strictly numbers (including alpha-numeric values), here is what we came up with which did the trick:
Thanks again everyone for your insight and recommendations, the Linux community never ceases to amaze me with the combined knowledge, have a good one folks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.