user8108049 user8108049 - 3 months ago 16
Bash Question

removed entry based on the length of the values

If the last column consists of less than 2 values then the whole row will be removed

sample data:

18106|1.0.4.0/22|223 121 1836
3549|1.0.10.0/24|421 21
5413|1.0.0.0/16|789
2152|1.4.0.0/16|745 89 1876
3549|1.0.8.0/22|680


Expected output:

18106|1.0.4.0/22|223 121 1836
3549|1.0.10.0/24|421 21
2152|1.4.0.0/16|745 89 1876


Is there any way to do it?

Answer Source

If there are no spaces after the single value, you can just eliminate lines with no space after the last |:

grep -v '|[^ |]*$'
  • [...] is a character class. [ |] matches a space or |.
  • ^ inside a character class negates it, i.e. [^ |] matches anything but space or |.
  • * means "repeated zero or more times"
  • $ matches the end of line
  • -v shows the lines not matching the pattern
  • So the whole thing means "skip lines that contain vertical bar followed by characters different to space and vertical bar till the end of the line"

It doesn't work for your sample data, though, as there's a space after 789. So, check there's a space followed by non-space after the last |:

grep '|[^ |]\+ [^ |]\+' 

Here, \+ means "repated one or more times".