Hot answers tagged

8

Yes. $ spamegg spamegg: command not found $ echo $? 127 You could just do: if software --version &>/dev/null; then ## True, do something else ## False, do something fi


5

The only thing you need to add to your one-liner is the option to slurp the file as a single string: perl -0777 -p -i -e 's/john paul\s*george/pete/g' ~/foo # ^^^^^ See http://perldoc.perl.org/perlrun.html#Command-Switches


4

perl's -n and -p options put variants of while (<>) { ... } around your program, which makes them process input linewise. If you want to replace across multiple lines, you need to read the whole thing into a string, which you need to do yourself. perl -e 'local $/;$_=<>;s/john paul\s*george/pete/g;print' This undefines $/, the record ...


4

Oneliner in Perl (thanks terdon!): perl -0 -pe 's/\s+(\S+)-(<\/page>\s+<page>)(\S+)/$2$1$3/g' filename What it does: matches against regular expression, and uses parts matched to reconstruct your word.


4

Perl to the rescue! #!/usr/bin/perl use warnings; use strict; my $group_size = 3; my @first = split ' ', <>; my @groups; my $start_index = 0; while ($start_index < @first) { my $step = 1; while ( $step < $group_size && $start_index + $step < @first && $first[$start_index] == $first[ ...


3

Here's an awk script. You just change the number 5 to have other groupings. awk ' NR==1{ previous = $1 for(i = 1;i<=NF+1;i++) if($i!=previous){ col[++numcol] = i previous = $i } } { j = 1; start = 1 for(i = 1;i<NF;i++){ printf "%s",$i if(i==col[j]-1){printf " "; start = col[j++]} else if((i-start+1)%5==0)printf " " } ...


3

With sed you can do this without slurping the entire file: sed -e ':top' -e 's/john paul[[:space:]]*george/pete/g;$b' -e '/john paul[[:space:]]*$/!b' -e 'N;btop' input This is much lighter on memory usage; it only slurps multiple lines when there is a possibility of a multi-line match starting from the current line. And then it only slurps until either ...


3

Some kind of a monster) With perl it should be easier cat file ba bla bla hyphe-</page> <page>nated bla bla bla and the output should look like bla bla bla</page> <page>hyphenated bla bla bla It's GNU sed (in some other sed-s -E option is used for extended regular expressions) sed -nr '/[[:alpha:]]+-<\/[[:alpha:]]+>$/{ N ...


3

At the shell, if you've got the Perl rename installed (sometimes called prename): rename -v 's/$/.bad/' * If you have too many files for the shell * glob to handle them all you can mix'n'match with find like this (also replace + with \; if necessary): find . -maxdepth 1 -exec rename -v 's/$/.bad/' {} + For Perl, just use move from a standard module: ...


2

Assuming no spaces in the file names: for file in *; do mv "${file}" "${file}.bad"; done


2

For automatic login, the way to go is using key-based authentication. A nice tutorial here You can then use the default one (the one in the ~/.ssh/id_rsa ) or use another key passing -i MyKeyFile parameter to ssh.


2

One possible way to do it would be to use perl's unpack function, with a template constructed from the first line of the file. After removing whitespace, it uses a regular expression with a repeating backreference to find the longest contiguous strings of identical characters not exceeding the given maxwidth, and stores their positions in an array. The ...


2

Other variant with awk awk ' NR==1{ for(i=2;i<=NF;i++){ count++ if($(i-1)!=$i || count>4){ D[i]=1 count=0 } } } { for(i in D) $i=" "$i print } ' OFS="" data.file >new.file And sed sed -re ' s/ ...


1

This is similar to choroba's answer $ cat file 1 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 2 4 5 8 9 10 13 17 19 29 30 32 33 50 700 800 900 950 $ perl -ane ' if ($. == 1) { for (($n,$i,$j) = (1,0,0); $j < @F; $n++, $j++) { if ($n == 3 || $F[$j] != $F[$j+1]) { $i-- if $i == $j; push @pairs, [$i, $j]; ...


1

If your intent is to check whether a particular command is available, you should do so instead of trying to execute it: if command -v spamegg >/dev/null; then echo spamegg is available else apt-get install spamegg fi Trying to execute spamegg to see if it's available is a bad approach. First, it obfuscates your code, making it look like spamegg is ...


1

IMHO, I don't think your approach is the best way to tackle this problem. The reason being is just because a command returns not found, doesn't mean the program isn't installed. It could simply indicate that the program is not located in any of your PATH locations. Perhaps, a better way would be to this is actually check against the list of installed ...


1

# example you need wget and your PATH is okay then: # bash/ksh/.. will return exit code 127 if command not found # # redirect stdin and stderr to the /dev/null = if exist, # output is not interesting wget --help >/dev/null 2>&1 stat=$? # variable ? include last command exit status echo "exit status:$stat" if ((stat == 127 )) ; then # not exist/found ...


1

I'm assuming your string variable is one long line without newlines. I get a sed error complaining about an unknown option to the s command. That's because your string contains slashes, which is the delimiter for the s command. Using bash parameter substitution to esacape the slashes in the string works. $ cat file this is a _PLACEHOLDER_ here $ ...


1

Pass the variable doublequoted as an argument to Perl, it can handle special characters in variables in the replacement: perl -i~ -pe 'BEGIN { $replace = shift } s/_PLACEHOLDER_/$replace/g ' "$text" "$file"


1

If you still get a certificate error after fixing the SSLversion as shown above, you need to disable certificate checking: if (! IO::Socket::SSL->start_SSL($SERVER, SSL_version => 'SSLv23:!SSLv2', SSL_verify_mode => 0)) {



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