delete the lines that don't have 4 columns - sed

I have some files as follows.I would like to delete the lines that don't have 4 columns.
263 126.9 263 50.2
264 76.6 264 6.2
265 62.3 265 49.9
266 84.2 266 18.3
7 63.8
8 59.7
9 36.4
11 12.0
Desired output
263 126.9 263 50.2
264 76.6 264 6.2
265 62.3 265 49.9
266 84.2 266 18.3

That's straight forward with awk
awk 'NF==4' input.txt
NF is the number of fields, if it is equal to 4, the default action is executed, which is printing the whole line.

sed '/\([^[:blank:]]\{1,\}[[:blank:]]\{1,\}\)\{3\}[^[:blank:]]\{1,\}/ !d'
in this sample, optimization with
sed '/\([0-9.]\{1,\}[[:blank:]]\{1,\}\)\{3\}[0-9.]\{1,\}/ !d'
including remark of sudo_O for 4 column ONLY (other allow more to be printed) and a parameter variable for number of column (must be at least 2)
ColNbr=4
ColBefore=$(( ${ColNbr} - 1 ))
sed "/^\([^[:blank:]]\{1,\}[[:blank:]]\{1,\}\)\{${ColBefore}\}[^[:blank:]]\{1,\}[[:blank:]]*$/ !d"
As he state, sed is not the optimize tools to use in this case (read comment for arguments)

This might work for you (GNU sed):
sed -r 's/\S+/&/4;t;d' file

Related

Delete lines in a file that start with specific strings

I have some files that look like this:
Node Present
1 243
2 445
10 65
4 456
43 8
...
I need to remove the values corresponding to specific nodes and I have a file specifying this nodes that looks like this:
1
4
...
The idea is to delete the lines that start with the values specified in my second file. I know that "sed" can do something like this, but I do not know how to apply it for all the values specified in the second file. More over, I want to delete node 1, but not node 100, and I am seeing that node 100 will also get erased with my approach.
sed '/^1/d'
This sort of problem is typically done with awk, and is quite common. Read the first file into an array, and then use it to process subsequent files. For example:
$ cat skip
1
4
$ cat input
Node Present
1 243
2 445
10 65
4 456
43 8
...
$ cat input2
Node Present
1 243
2 445
4 456
43 8
4 587
...
$ awk 'NR==FNR{a[$1] = 1; next} ! a[$1] && FNR > 1' skip input input2
2 445
10 65
43 8
...
2 445
43 8
...
The initial NR == FNR causes those command to only be executed on the first file, loading the array with the ids you wish to skip. Subsequent commands print lines in which the first column did not appear in the first file and the first line of each file, in which FNR > 1. (FNR is the "File Record Number", aka the line number in the file.)
sed is not the right tool for this job. I suggest using awk like this:
awk 'NR == FNR {ids[$1]; next} NR == 1 || !($1 in ids)' ids nodes
Node Present
2 445
10 65
43 8
Where input files are:
cat ids
1
4
cat nodes
Node Present
1 243
2 445
10 65
4 456
43 8
If you want to stick with sed and bash, here is one possible way how you would do it. Please note that, for large files, that would take orders of magnitude longer to run than the awk answers (similar to time complexity of linear search vs binary search).
sed -f <(sed 's/^/\/^/;s/[[:blank:]]*$/[[:blank:]]\/d/' ids) nodes
ids is filename of single-column file and nodes is that of two-column.

AHK WinWait Memory Usage

I have an ahk script I use to enable the Printer button on a Crystal Reports dialog that for what ever reason is not enabled by default when used in Server 2008 R2. Anyways... the issue I am having is the process when running continues to stack memory each cycle. Its not like I am storing any contents to a variable that happens to not get cleared. What in this process uses memory resources that don't get released and is there anything I can implement to prevent this from happening?
You can see in this listing that the private memory just grows as usage goes on. I ended up having it initiate about 5 times and it went from about 1000k to 2000k.
The top entry is my test version I converted from WinWaitActive that was causing unnecessary CPU usage.
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
58 8 2312 6216 68 0.62 7828 showprinterbutton
55 8 1788 5508 67 32.39 6840 ShowPSPrinter
57 8 1864 6028 79 33.12 7184 ShowPSPrinter
55 8 1396 5084 67 1.29 7604 ShowPSPrinter
55 8 1796 5536 67 36.36 7856 ShowPSPrinter
55 8 1772 5444 67 37.27 9848 ShowPSPrinter
55 8 1740 5424 67 26.33 10300 ShowPSPrinter
55 8 1396 4992 67 0.84 11348 ShowPSPrinter
55 8 1396 5024 67 1.14 11460 ShowPSPrinter
55 8 1736 5604 67 355.93 11676 ShowPSPrinter
55 8 1396 4984 67 1.06 13364 ShowPSPrinter
55 8 1396 5132 67 0.81 13516 ShowPSPrinter
72 9 2048 6500 73 66.36 14072 ShowPSPrinter
55 8 1792 5504 67 59.92 15736 ShowPSPrinter
55 8 1400 4960 67 0.61 16340 ShowPSPrinter
57 8 1496 5848 79 0.98 18516 ShowPSPrinter
57 8 1500 5404 79 0.98 19048 ShowPSPrinter
55 8 1400 5000 67 0.51 22020 ShowPSPrinter
Here is the script contents I have that is then compiled to run as an EXE.
; Version: 1.2
; Dated: 03/31/2015 - Created
; Description: Enable a watch for page setup dialog and activate the print button for crystal reports
; Only allow one instance to run
#SingleInstance force
; Run with out a tray icon
#NoTrayIcon
; Getting loose with not requiring direct title menu values
SetTitleMatchMode, RegEx
; Start active watch for quick post menu
WaitForPS:
WinWait,Page Setup
{
Control,Show,,Button8,Page Setup,(optimize for screen display)
GoSub WaitForPS
}
; End of Script...
Right after the moment the window appears and the loop ran one time, WainWait immediately continues to the next statement because the window already exists, enables the control, and recursively invokes the loop again (gosub), so the code allocated for example 100 stack frames per second thus eventually exhausting the call stack.
Instead use an indefinite loop and before continuing wait for the window to close:
Loop
{
WinWait,Page Setup
Control,Show,,Button8,Page Setup,(optimize for screen display)
WinWaitClose
}

Xvnc process at 100% CPU when idle

I have a very annyoing problem when running TigerVNC 1.3.1 in a Debian 7 virtual machine. After about one minute doing nothing in the VNC window, the Xvnc process goes up to 100% CPU usage. Once I move my mouse into the VNC window again, the CPU usage returns to normal. I believe that the function call select() is the culprit. Doing an "strace -p " gives me tons of this:
select(256, [0 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74], NULL, NULL, {0, 0}) = 0 (Timeout)
And "strace -c -p ":
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
78.19 0.001760 0 98445 select
21.81 0.000491 0 196889 setitimer
------ ----------- ----------- --------- --------- ----------------
100.00 0.002251 295334 total
I'm not an expert on system function calls, but all other processes I checked with these commands do not show that kind of behavior. Is it a bug in the tigervnc code or is there a way I can fix it?
I'd recommend you reset to default settings to see if everything becomes fine. I was a tightvnc user for a long time, until I switched to RealVNC (free edition). I'd suggest you give a try of it. The settings are almost identical to tightvnc. And it supports real cross machine text copy-and-paste.

Why does perldoc evaluate 'Münster' as 'Muenster'

I have a simple POD text file:
$ cat test.pod
=encoding UTF-8
Münster
It is encoded in UTF-8, as per this literal hex dump of the file:
00000000 3d 65 6e 63 6f 64 69 6e 67 20 55 54 46 2d 38 0a |=encoding UTF-8.|
00000010 0a 4d c3 bc 6e 73 74 65 72 0a |.M..nster.|
0000001a
The "ü" is being encoded as the two bytes C3 and BC.
But when I run perldoc on the file it is turning my lovely formatted UTF-8 characters into ASCII.
What's more, it is correctly handling the German language convention of representing "ü" as "ue".
$ perldoc test.pod | cat
TEST(1) User Contributed Perl Documentation TEST(1)
Muenster
perl v5.16.3 2014-06-10 TEST(1)
Why is it doing this?
Is there an additional declaration I can put into my file to stop it from happening?
After additional investigation with App::perlbrew I've found the difference comes from having a particular version of Pod::Perldoc.
perl-5.10.1 3.14_04 Muenster
perl-5.12.5 3.15_02 Muenster
perl-5.14.4 3.15_04 Muenster
perl-5.16.2 3.17 Münster
perl-5.16.3 3.19 Muenster
perl-5.16.3 3.17 Münster
perl-5.17.3 3.17 Münster
perl-5.18.0 3.19 Muenster
perl-5.18.1 3.23 Münster
However I would still like, if possible, a way to make Pod::Perldoc 3.14, 3.15, and 3.19 behave "correctly".
Found this RT ticket http://rt.cpan.org/Public/Bug/Display.html?id=39000
This "bug" seems to be introduced with Perl 5.10 and perhaps this was solved in later versions.
Also see: How can I use Unicode characters in Perl POD-derived man pages? and incorrect behaviour of perldoc with UTF-8 texts.
You should add the latest available version of Pod::Perldoc as a dependency.

Why do I get many Moose warnings when I start Catalyst?

I am having an issue (Catalyst related) apparently with Moose/Class::MOP. Starting my server I get the following output... (shown below in all its glory)
The alias and excludes options for role application have been renamed -alias and -excludes at /usr/local/lib/perl/5.10.1/Moose/Meta/Role/Application.pm line 26
Moose::Meta::Role::Application::new('Moose::Meta::Role::Application::ToRole', 'excludes', 'ARRAY(0x9645568)') called at /usr/local/lib/perl/5.10.1/Moose/Meta/Role.pm line 416
Moose::Meta::Role::apply('Moose::Meta::Role=HASH(0x95b3368)', 'Moose::Meta::Role=HASH(0x9446d90)', 'excludes', 'ARRAY(0x9645568)') called at /usr/local/lib/perl/5.10.1/Moose/Util.pm line 133
Moose::Util::_apply_all_roles('Moose::Meta::Role=HASH(0x9446d90)', undef, 'MooseX::Getopt', 'HASH(0x9454a08)') called at /usr/local/lib/perl/5.10.1/Moose/Util.pm line 87
Moose::Util::apply_all_roles('Moose::Meta::Role=HASH(0x9446d90)', 'MooseX::Getopt', 'HASH(0x9454a08)') called at /usr/local/lib/perl/5.10.1/Moose/Role.pm line 26
Moose::Role::with('Moose::Meta::Role=HASH(0x9446d90)', 'MooseX::Getopt', 'HASH(0x9454a08)') called at /usr/local/lib/perl/5.10.1/Moose/Exporter.pm line 294
Moose::Role::with('MooseX::Getopt', 'HASH(0x9454a08)') called at /usr/share/perl5/Catalyst/ScriptRole.pm line 8
require Catalyst/ScriptRole.pm called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 113
Class::MOP::__ANON__() called at /usr/share/perl5/Try/Tiny.pm line 71
eval {...} called at /usr/share/perl5/Try/Tiny.pm line 67
Try::Tiny::try('CODE(0x93c2910)', 'Try::Tiny::Catch=REF(0x93bba38)') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 124
Class::MOP::load_first_existing_class('Catalyst::ScriptRole') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 136
Class::MOP::load_class('Catalyst::ScriptRole', undef) called at /usr/local/lib/perl/5.10.1/Moose/Util.pm line 109
Moose::Util::_apply_all_roles('Moose::Meta::Class=HASH(0x8fc9378)', undef, 'Catalyst::ScriptRole') called at /usr/local/lib/perl/5.10.1/Moose/Util.pm line 87
Moose::Util::apply_all_roles('Moose::Meta::Class=HASH(0x8fc9378)', 'Catalyst::ScriptRole') called at /usr/local/lib/perl/5.10.1/Moose.pm line 58
Moose::with('Moose::Meta::Class=HASH(0x8fc9378)', 'Catalyst::ScriptRole') called at /usr/local/lib/perl/5.10.1/Moose/Exporter.pm line 294
Moose::with('Catalyst::ScriptRole') called at /usr/share/perl5/Catalyst/Script/Server.pm line 14
require Catalyst/Script/Server.pm called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 113
Class::MOP::__ANON__() called at /usr/share/perl5/Try/Tiny.pm line 71
eval {...} called at /usr/share/perl5/Try/Tiny.pm line 67
Try::Tiny::try('CODE(0x8f6dc38)', 'Try::Tiny::Catch=REF(0x8f8d1c8)') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 124
Class::MOP::load_first_existing_class('Catalyst::Script::Server') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 136
Class::MOP::load_class('Catalyst::Script::Server') called at /usr/share/perl5/Catalyst/ScriptRunner.pm line 18
Catalyst::ScriptRunner::run('Catalyst::ScriptRunner', 'Jamaica', 'Server') called at script/boca_server.pl line 8
The old Moose::Util::MetaRole API (before version 0.94) has been deprecated at /usr/local/lib/perl/5.10.1/Moose/Util/MetaRole.pm line 16
Moose::Util::MetaRole::apply_metaclass_roles('for_class', 'Catalyst::Controller', 'metaclass_roles', 'ARRAY(0x97bdff8)', 'method_metaclass_roles', 'ARRAY(0x97ab9f0)', 'wrapped_method_metaclass_roles', 'ARRAY(0x981d650)') called at /usr/share/perl5/MooseX/MethodAttributes.pm line 32
MooseX::MethodAttributes::init_meta('MooseX::MethodAttributes', 'for_class', 'Catalyst::Controller', 'metaclass', undef) called at /usr/local/lib/perl/5.10.1/Moose/Exporter.pm line 355
Moose::Exporter::__ANON__('MooseX::MethodAttributes') called at /usr/share/perl5/Catalyst/Controller.pm line 10
Catalyst::Controller::BEGIN() called at /usr/share/perl5/Catalyst/Controller.pm line 10
eval {...} called at /usr/share/perl5/Catalyst/Controller.pm line 10
require Catalyst/Controller.pm called at /usr/share/perl5/Catalyst.pm line 16
Catalyst::BEGIN() called at /usr/share/perl5/Catalyst/Controller.pm line 10
eval {...} called at /usr/share/perl5/Catalyst/Controller.pm line 10
require Catalyst.pm called at /home/bobek/dev/build_7.2/mvc/Jamaica/script/../lib/Jamaica.pm line 17
Jamaica::BEGIN() called at /usr/share/perl5/Catalyst/Controller.pm line 10
eval {...} called at /usr/share/perl5/Catalyst/Controller.pm line 10
require Jamaica.pm called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 113
Class::MOP::__ANON__() called at /usr/share/perl5/Try/Tiny.pm line 71
eval {...} called at /usr/share/perl5/Try/Tiny.pm line 67
Try::Tiny::try('CODE(0x8fb3230)', 'Try::Tiny::Catch=REF(0x969fee0)') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 124
Class::MOP::load_first_existing_class('Jamaica') called at /usr/local/lib/perl/5.10.1/Class/MOP.pm line 136
Class::MOP::load_class('Jamaica') called at /usr/share/perl5/Catalyst/ScriptRole.pm line 61
Catalyst::ScriptRole::_run_application('Catalyst::Script::Server=HASH(0x969d748)') called at /usr/share/perl5/Catalyst/Script/Server.pm line 181
Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x969d748)') called at /usr/local/lib/perl/5.10.1/Class/MOP/Method/Wrapped.pm line 48
Class::MOP::Method::Wrapped::__ANON__('Catalyst::Script::Server=HASH(0x969d748)') called at /usr/local/lib/perl/5.10.1/Class/MOP/Method/Wrapped.pm line 89
Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x969d748)') called at /usr/share/perl5/Catalyst/ScriptRunner.pm line 20
Catalyst::ScriptRunner::run('Catalyst::ScriptRunner', 'Jamaica', 'Server') called at script/boca_server.pl line 8
I am new at Catalyst (and I do like it, I just want to successfully get it up and running). I have checked the latest versions of Moose and Class::MOP and they are up to date (as far as CPAN is concerned).
Hopefully someone familiar with Moose has seen this before. Any suggestions or a place to start checking would be appreciated.
Michelle B.
You're running an old version of Catalyst. This issue has been fixed in commit r13516 and is already available on CPAN as of version 5.80026 (5.80028 is latest and recommended).
Catalyst was using some deprecated feature of Moose, which just recently started warning.
So, you're right, the current Cat stuff has a fairly broken dependency chain. You can see from the current pass/fail on the CPAN for 5.80030 that fails are much higher than normal.
There are several bumps depending on the state of your current modules/versions. This worked for me this morning (I'm not sure if it's overkill)-
cpanm -v namespace::clean
cpanm -v namespace::autoclean
cpanm -v B::Hooks::EndOfScope
cpanm -v Package::Stash
cpanm -v Class::MOP Moose MooseX::Types
cpanm -v MooseX::Role::WithOverloading::Meta::Role::Application
cpanm -v Catalyst Catalyst::Devel
And if you're using Config::General for your configuration, this is a good idea too:
cpanm -v Config::General
If you get those in and it won't start, just read the stack trace (backwards) looking for names or missing functions/methods. Anything you see as a problem, upgrade. It's a "stable" kit but it's a collection of moving targets and everything has to line up.