# Tag Info

## Hot answers tagged perl

6

With sed: sed 's/^Question Nr\..*/\\item/; s/^$$[A-Z] .*$$/\n\1/' file The first s/// replaces Question Nr. with \item similar to the sed command in your question. The second one replaces line that start with a capital letter from A to Z, but only one followed by a space. This whole line is replaced with itself \1 repending a newline \n. The output: ...

5

If it doesn't need to be sed, Perl's "paragraph mode" is perfect for this. From man perlrun: -0[octal/hexadecimal] specifies the input record separator ($/) as an octal or hexadecimal number. [...] The special value 00 will cause Perl to slurp files in paragraph mode. [...] So, using -00 tells perl to define "lines" ... 5 sed -e'/./!d;$!G;/^Q/c\' -e'\\item' <in >out That will delete every blank line in input, Get a blank line out of hold space and append it to all non-blank lines which are ! not the $last, and change any pattern space ^beginning with the character Q to the one-line fixed-string \item on output. When run on your example input, the output is: \item ... 5 Another way with tr+sed: tr -s \\n <infile | sed '$!G;s/Question Nr.*/\\item/' tr squeezes all newlines and then sed appends hold space content (empty newline) to each line except the last one, replacing Question Nr.* with \item. With this method you won't be able to edit the file in-place. I chose tr here as it's faster then sed's regex (even if it's ...

4

The issue you are describing is standard behaviour on the systems I tested on. I and O affect stdin and stdout so this should work: → cat data | perl -CIO -pe 's/.*(..)$/$1/' ρε 小红 Whereas this might not: → perl -CIO -pe 's/.*(..)$/$1/' data Îµ º¢ There are two more options to perl -C that get your behaviour. i 8 UTF-8 is the default PerlIO ...

4

Now with awk: awk '$1 ~ /[ABCDEM]/ {print$0"\n"} $1 ~ /Question/ {print "\\item"}' inputfile If the line starts with A, B, C, D, E or M (for Main), it prints that line and an extra \n. If the line starts with "Question", it simply prints \item. 4 With perl: perl -0777 -pe 's/\Q\{{[}\E.*?\Q{]}\}\E//gs' Note that the whole input is loaded in memory before being processed. \Qsomething\E is for something to be treated as a literal string and not a regular expression. To modify a regular file in-place, add the -i option: perl -0777 -i -pe 's/\Q\{{[}\E.*?\Q{]}\}\E//gs' file.txt With GNU awk or ... 3 As an illegible awk one-liner$ awk 'NR>1{a[0]=$3;a[1]=$4;getline<f;for(i=1;i<=NF;i++)$i=a[$i];print}' f=file2 file1 A C A C A A A C C C T T C T T C G A A A G G G A More readable: awk ' # skip the header in file1 NR == 1 {next} { # read the values from the file1 line a[0] = $3 a[1] =$4 # replace the ...

3

With awk: awk 'function p(){print l,c,d; delete a; delete b; c=d=0} NR!=1&&l!=$1{p()} ++a[$2]==1{c++} ++b[$3]==1{d++} {l=$1} END{p()}' file Explanation: function p(): defines a function called p(), which prints the values and deletes the used variables and arrays. NR!=1&&l!=$1 if its not the first line and the variable l equals the ... 3 Use the -F options to make grep treat pattern as fixed string: grep -F 'abc.def.ghi' <file And also note that you don't need to invoke egrep. 3 You could get a count of substitutions per file with: find . -type f -exec perl -pi -e '$count{$ARGV} += s/http//g; END {for (keys %count) {print "$_: $count{$_}\n" if $count{$_}}}' {} + Note that in that and in your original solution, perl will rewrite the files regardless of whether it does substitution in them or not. Don't use ; to terminate ...

3

You might want to use these tools to increase the efficiency of perl scripts. You would want to do this if you had a larger perl program and you wanted to integrate the functionality of an awk script without calling a subprocess. You would call a2p and integrate the generated code into an existing perl script. There's a similar utility, find2perl which ...

2

sed -e:t -e'y/\n/ /;/\\{{$}/!b' \ -e:N -e'/\\{{\[.*{$}\\}/!N' \ -e's/$$\\{{$}$$.*\n/\1/;tN' \ -e'y/ /\n/;s/\\{{\[}/& /;ts' \ -e:s -e's/$$\[} [^ ]*$$$${$}\\}$$/\1 \2/' \ -ets -e's/..... [^ ]* .....//;s/ //g;bt' \ <<"" #Bla Bla {]}\} bla bla \{{[} more bla ...

2

sed " /^admin::common::passwords:$/,/^[^ ].*:$/ { /^ $ACCOUNT:$/ { N;d } }" < "$YAML" That is, enclose your deleting stanza in a section that's matched between admin::common... and the next whatever-section:. Beware that the . character, common in user names is also a regular expression operator, so john.doe would match john.doe ... 2 ssed is much smaller binary than perl. /usr/bin/ssed is 123K with PCRE linked statically. /usr/bin/perl is 1.7M, and that's not including any of the standard modules that get installed with it. ssed could fit on a tiny distro, or a rescue / installer ISO, whereas perl might not. It's hard to see the point, though, because PCRE is mostly just a ... 2 perl -lane 'undef$h{ $F[0] }[$_ - 1 ]{ $F[$_] } for 1,2 }{ for my $k (keys %h) { print join " ",$k, map scalar keys $_, @{$h{$k} } }' < input Basically, you create a hash like this: 'apple' => [ { 'abc' => undef, 'xyz' => undef, ... 2 You can do this fully in awk but as a variation here's an awk + paste solution. You'll need bash or another shell that supports process substitution paste <(tail -n +2 file1) file2 | awk '{a["0"]=$3; a["1"]=$4; for (i=5; i<=NF; ++i) printf "%s%s", a[$i], i==NF?"\n": " "}' The tail -n +2 is needed to skip the header line from file1.

2

{ printf '[13*%d-n[bs]pc]s%c\n' 9 a 7 b 5 c 3 d 1 e tr -s ' \n' lx <file.txt; } | dc | sed -f- -eb -e:s -e's/.*/\\textbf{&}/' exam.txt That uses the dc reverse-polish-notation calculator to generate a sed script that looks like: 8bs 17bs ...which is then concatenated with the sed scripts entered on the command-line and so results in ...

2

I would probably tackle it something like this: #!/usr/bin/env perl use strict; use warnings; use XML::Twig; my %count_of_col2; my %count_of_col3; #iterate data while (<DATA>) { #split on whitespace my ( $key,$col2, $col3 ) = split; #update counts$count_of_col2{$key}{$col2}++; $count_of_col3{$key}{$col3}++; } foreach my$key ...

2

I like whitespace and descriptive variable names. What else is there to say? It's been a while since I've written a lot of awk, I even forgot about the -f on the shebang. However, as I did this I really felt like I was in the zen of it. Haiku code. I like this solution because there's a minimum of coded logic. Only two for loops iterating over array ...

1

As promised, here is the approach I did work out before writing this question. It works and maybe it is a good approach, but it seemed overly convoluted for this apparently simple task. Now it seems it's actually not so bad. :) function printcounts() { printf "%s", currentf1 for (i = 2; i <= 3; i++ ) { printf "%s", FS countuniq [ i ] } ...

1

type this in the terminal sudo apt-get install libapache2-mod-perl2 , then : sudo a2enmod cgi and restart apache.

1

You do have to do it yourself: this sed command escaped any non-alphanumeric character, which is what quotemeta does (IIRC) str="abc.def.ghi" escaped=$(sed 's/[^[:alnum:]]/\\&/g' <<< "$str") echo "$escaped" # => abc\.def\.ghi we expect this to match because it's using the original string as a regular expression$ echo "foo bar ...

1

OK, here you go. The key is to have the awk program read the valid list too. Process the score and input files. Then loop over the valid combinations, not all the permutations. Uses GNU awk for arrays of arrays gawk ' # validlist FILENAME == ARGV[1] { valid[$1 FS$2 FS $3] next } # scorefile FILENAME == ARGV[2] { ... 1 It would be far safer to use pre-built libraries to parse the whole file (like this, for example http://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file) make your modifications and then re-save the modified file. Doing somewhat blind text modifications on a structured text file like this is just asking for something to go wrong. 1 Why it got installed by default ? : Its upto developer/maintainer of the distro that you are using. Why would you need a2p There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always ... 1 You can get a report of what files are sent to perl by using both -print and -exec: find . -name "*.txt" -print -exec perl -pi -e 's/http//g' '{}' \; If you want to find out which files were modified, then something like find . -name "*.txt" \ -exec perl -i.bak -pe 's/http//g' '{}' \; \ -exec sh -c 'cmp -s "$1" "$1.bak" && echo "$1" || ...

1

I think I've said this before - but at risk of sounding like a stuck record - DON'T use regular expressions to parse XML. It's brittle and prone to breaking. I would ask first though - why are you trying to do what you're doing? Because it should be irrelevant when working with your XML. Instead use a parser: #!/usr/bin/env perl use strict; use warnings; ...

1

Almost standard sed procedure sed '\$!N;s/\n$$\s*[^<[:blank:]]$$/\1/;P;D' log.xml

1

Perl to the rescue! perl -pe 'print "\n" if /^\s*+</; chomp;' input > output i.e. newline is removed from each line, and it's printed when the next line starts with whitespace followed by a <. To keep the final newline, change chomp to chomp unless eof or add END { print "\n" }

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