Tag Info

Hot answers tagged

16

That depends on how fast the perl script returns. If it returns quickly, you might want to insert a small pause between executions to avoid CPU load, eg: while true do /someperlscript.pl sleep 1 done This will also prevent a CPU hog if the script is not found or crashes immediately. The loop might also better be implemented in the perl script itself ...


12

The other answers, about using inotify, are correct, but not an answer to this question. A process supervisor, such as supervisord, upstart, or runit, is designed for exactly the problem of watching and restarting a service if it crashes. Your distro probably comes with a process supervisor built in.


11

while true is fine as a general-purpose "loop forever" construction. As other answers say, the body of the loop shouldn't be empty, or become empty by virtue of the command inside the loop not working. If you're using Linux, you may want to use a command like inotifywait, which makes the while loop much simpler: while inotifywait -qqe modify "$DIRECTORY" ...


8

You need the write permission on a directory to create or remove files in it, but not to write to a file in it. Most shell commands, when given an output file, simply open that file for writing, and replace the data that was previously in the file. The > redirection operator truncates the existing file (i.e. deletes the existing file content, resulting in ...


5

Move the while 1 into the perl script (Following @roaima suggestion) #!/usr/bin/perl use Linux::Inotify2; my $inotify = new Linux::Inotify2 or die "unable to inotify: $!"; $inotify->watch ("Dir", IN_MODIFY, ## or in_{acess,create,open, etc...} sub { my $e = shift; my $name = $e->fullname; ## whatever print "$name was ...


5

I doubt it will make a difference but, just in case, here's how to do the same thing in Perl: perl -ne 'print if ++$k{$_}==1' out.txt If the problem is keeping the unique lines in memory, that will have the same issue as the awk you tried. So, another approach could be: cat -n out.txt | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- How it works: ...


5

Another perl: print a line if there are 3 commas. perl -i.bak -ne 'print if tr/,/,/==3' file The tr operator returns the number of characters transliterated.


3

#!/usr/bin/perl use DB_File; tie %h, 'DB_File'; while(<>){ not $h{$_} and print and $h{$_}=1 } EDIT 1: Does it really work? (comparing) Sol1 : Terdon et all Schwartzian-transform-like one-liner cat -n _1 | sort -uk2 | sort -nk1 | cut -f2- Sol2 : perl + DB_File (this answer) perl dbfile-uniq _1 Sol3 : PO (John W. Gill solution has a ...


3

With perl: $ perl -F, -i.bak -ane 'print if @F > 3' file With perl > 5.20, you can use -F without -a and -n (-F implies -a and -a implies -n). Or you can use sed: $ sed -i.bak -e '/\([^,]*,\)\{3,\}/!d' file


3

Perl's Tie::File module offers true in-place edit functionality: perl -MTie::File -e ' tie @a,"Tie::File","your_file_here"; # Do something... ' This makes the elements of @a into the lines of your file and any changes done to @a are reflected in the file even if the file is in a read-only directory.


3

If the directory is read-only but the files within that directory are read/write, then there is nothing stopping you from overwriting those files. From a script you can write to the files using usual redirection > and >> as well as overwriting them using cp. What you cannot do is to create a new file in the directory and rename it on top of an ...


3

You can use awk: awk -F',' 'NF==4' file If you can use gawk version >= 4.1.0 you can use inplace, more info. So it could be: gawk -i inplace -v INPLACE_SUFFIX=.bak -F',' 'NF==4' file


3

When your perl script is intended to keep on running all the time, why use the while construction ? When the perl fails in view of some serious problem, the new perl script started by the while might crash just as hard. Again-and-again-and-so-on. if you really want your perl started over, consider crontab and a script that first checks for running instances. ...


3

Let looking into perl source to more details. In perl.c: case '0': { I32 flags = 0; STRLEN numlen; SvREFCNT_dec(PL_rs); if (s[1] == 'x' && s[2]) { const char *e = s+=2; U8 *tmps; while (*e) e++; numlen = e - s; flags = PERL_SCAN_SILENT_ILLDIGIT; rschar = ...


3

Octal is just like decimal in that 0 == 0, and 0000 == 0, 0 == 000000, etc. The fact that the switch here is -0 may make things a little confusing -- I would presume the point about "the special value 00" means one 0 for the switch and one for the value; adding more zeros is not going to change the latter, so you get the same thing... Up to a point. The ...


2

Include the key in your regexp. If you know exactly how it is written, then you can use a positive lookbehind assertion and it's easier: perl -pi -e 's/(?<=^\$VIP_IP=).*/192.17.200.100/' your_file Otherwise, you must include the matched key in the replacement text using $1: perl -pi -e 's/(^\$VIP_IP\s*=).*/$1192.17.200.100/' your_file


2

In general there is no problem using while true since it is a tiny test which is only executed after the perl script is terminated. Keep in mind that depending on which Linux/Unix variant you are using, the script might get terminated at logging off. In such case consider using the loop in a script and call it with nohup and put it in the background i.e. ...


2

The keyword my declares local variables. Therefore, $workbook is local to the body of the following if: if($yes eq "y") { my $workbook = 1; } You could get the scoping that you are looking for by declaring $workbook before the first if, as my $workbook, and then, inside the if or elsif blocks, assigning to it without declaring a new variable ...


2

If you want to manage a process, you might want to look into a process manager to do so. With many recent systems, you can use systemd to monitor your processes (which is one of the benefits of systemd over classical init-scripts). If your distro of choice doesn't use systemd, you could use daemontools or monit.


2

I’ve simplified your input string to qABxBCzABxBCDEFw, where A represents [ B represents - C represents ] D represents {\+ E represents the text between the +s (including the URL) F represents \+} Lower case letters represent everything else. So here are some substitute commands run against the input: Command ...


2

You have to use a negative lookaround: perl -pe 's|\[-((?!-\]).)*-\] {\+(\S+… https://t.co/.*)\+}|\2|' <<<$text The problem is the first occurence of [- matches. Then the non-greediness of the pattern doesn't take the desired effect, it doesn't matter how non-greedy it is. With a negative lookaround you can match everthing except of the string ...


2

The while loop is really not a good idea. There's no escape there - it just runs forever - statically. Any number of things could change in the environment and it will not be affected - and this could be bad. For example, if the shell executable responsible for that while loop is upgraded the kernel will not be able to release the disk space for the old ...


2

There is no need to mix many instruments. Task can be done by sed only sed '/^INFO\|^DEBUG\|^TRACE\|^ERROR/{ /Logger2/{ :1 N /\nINFO\|\nDEBUG\|\nTRACE\|\nERROR/!s/\n// $!t1 D } }' log.entry


1

No. You can't use sed's or perl's -i switch to edit files in place in a read-only directory. As you correctly assumed, you won't be allowed to create the necessary temporary files: $ ls -ld read_only/ dr-xr-xr-x 2 terdon terdon 4096 Apr 13 02:16 read_only/ $ ls -l read_only/file -rw-r--r-- 1 terdon terdon 3 Apr 13 02:16 read_only/file $ sed -i 's/a/A/' ...


1

With GNU sed: sed -nE '/^.+,.+,.+,.+$/p' file > output_file


1

I upvoted a few of these answers, but I instinctively have the warmest feelings for @WalterA's answer. Well, I did until I created my own answer... Personally, I'd tend to update the Perl script so that it writes a descriptive log entry about the failure and sends an alert to an administrator. If the Perl script is meant to continue running, waiting for ...


1

A few random hints depending on the actual data formatting and other issues... How are the data fields separated? (The first three spacings give the impression that there's a TAB character in between, while the last columns seem space separated.) You should be aware that the column information is lost for the culumns 4-N if your field separator is defined ...


1

OK, I think I’ve got it.  Your Source: (?<group>.*/).*\n regex is capturing, in the group group, everything after the Source:  up through the last / on the line.  So, for your example, it is capturing /disk/media/Camera/.  To capture the JPEG image filename, you want Source: .*/(?<group>.*)\n… OK, here we go again.  I believe that you are ...


1

AIX's {smd5} format is a non-standard one. It's a minor variant of the *BSD/Linux/Solaris “MD5” (which is the one generated by openssl passwd -1). I wasn't able to find much information about it. There is code contributed to John the Ripper (present in the 1.8.0 jumbo version) that calculates it, in the file aix_smd5_fmt_plug.c. From reading the code, it ...


1

Are you sure that it’s the “S” that’s causing the problem?  As I explained in my answer to your previous question, the “l” signifies that your operating system and filesystem support mandatory file locking, and mandatory file locking is enabled for this file.  I doubt that it’s a coincidence that both files you give as examples (in this question and the ...



Only top voted, non community-wiki answers of a minimum length are eligible