Tag Info

New answers tagged

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

Based on one answer at http://stackoverflow.com/questions/9605232/merge-two-lines-into-one this seems to fit the bill #!/usr/local/bin/bash PATTERN1='TRACE *'; PATTERN2='DEBUG *'; PATTERN3='INFO *'; PATTERN4='ERROR *'; LINEOUT="" while read line; do case $line in $PATTERN1) echo $LINEOUT LINEOUT="$line" ...


1

perl filter for multiline log records (record begin mark) Use the following perl script as a working prototype. Usage script_path regular_expression log_files e.g. script_path "line \d" log_file_1 log_file_2 #!/usr/bin/perl $pattern = qr/(?^s)$ARGV[0]/; shift; # process filtering expression # (?^s) - treats matched string as single line my $line = ''; # ...


1

If your shell supports it, you could use a here document to read the commands from standard input, with a quoted delimiter to prevent shell expansion of the awk variable $5 awk -f- somefile << "EOF" {sum+=$5} END { print "Average = ",sum/NR} EOF Or just put the commands in a file and run that with awk -f.


1

You don't need '' (strong quotes), you can use the weaker form "", except you then need to escape the "s. awk "{sum+=\$5} END { print \"Average = \",sum/NR}" But why?


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 ...


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/' ...


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 ...


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 ...


0

I've used awk -v BINMODE=rw '!($0 in a){a[$0];print}' infile >> outfile BINMODE=rw : to keep end of line terminators happy. (I live in a mixed os environment) Logic is simple. If the current line isn't in the associative array then add it to the associative array and print to output. There may be memory limitations with this approach. For very ...


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 ...


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

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


1

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


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


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 ...


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

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 ...


1

There are a few ways to go about this w/ sed. One way is a delayed read as is recommended in the accepted answer. It could also be written like: sed -e '$!N;P;/\nPointer/r file1' -e D file2 ...with a little explicit look-ahead instead of the look-behind implemented elsewhere with the hold buffer. That will inevitably have the same problem with the last ...


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

The accepted sed answer does work for most cases but if the marker is on the last line, the command won't work as expected: it will insert the content of File1 after the marker. I initially tried with: sed '/Pointer/{r File1 N}' File2 which also works fine (as r will do its magic at the end of the cycle) but has the same problem if the marker is on the ...


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 ...


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" ...


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 ...


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. ...


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. ...


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 ...


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


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 ...


0

Following Anthon's suggestion to use "uniq"... Remove leading, trailing and duplicate blank lines. # Get large random string. rand_str=; while [[ ${#rand_str} -lt 40 ]]; do rand_str=$rand_str$RANDOM; done # Add extra lines at beginning and end of stdin. (echo $rand_str; cat; echo $rand_str) | # Convert empty lines to random strings. sed "s/^$/$rand_str/" ...


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 ...


0

I think you have to add: my $q = CGI->new(); my $username = $q->param('username'); ... So the whole should look like: #!/usr/bin/perl -wT use strict; use warnings; use CGI; my $q = CGI->new(); my $username = $q->param('username'); print $q->header(); print $q->start_html("Perl page"); print $q->h2("Hello, $username!\n"); print ...



Top 50 recent answers are included