# Tag Info

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

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

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" ... 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: ... 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 ] } ... 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 ... 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; ... 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 ... 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 ...

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

0

Fixed with a change of configuration ;) foreach my $u (qw(years weeks days hours minutes)) 1 type this in the terminal sudo apt-get install libapache2-mod-perl2 , then : sudo a2enmod cgi and restart apache. 0 Apparently there is a problem in nix. There is an issue on github with a proposed workaround by setting the LOCALE_ARCHIVE variable. If you already have nix installed just do: nix-env -i glibc-locales And in you bash profile: export LOCALE_ARCHIVE="$(nix-env --installed --no-name --out-path --query glibc-locales)/lib/locale/locale-archive"

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

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.

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] { ... 0 It actually ended up being two answers. Warning: I'm relatively new to Linux, and I'm not a developer/coder of any kind. This will probably seem like child's play, in which case it's free to be deleted. I fixed the error in the original file, fixing { to \{ (@thrig had a slight error in his comment to the main post, which led me to making that same error ... 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 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 ...

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

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

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

1

Almost standard sed procedure sed '$!N;s/\n$$\s*[^<[:blank:]]$$/\1/;P;D' log.xml 0 if you want to handle only the 'line' object and text inside it , you can do : grep -v '<line>' logfile > newlogfile grep -n '</line>' newlogfile | grep -Eo '^[^:]+' | while read n do sed -i "$n s|^| <line>|g" newlogfile done

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

1

That's not a system issue but the perl itself. -CIO only set UTF-8 encoding on STDIN and STDOUT, two of three perl predefined filehandle (you have -E for STDERR as well). When you use: perl -CIO -pe 's/.*(..)$/$1/' file perl use diamond operator <> for processing file. Since when diamond operator <> used open (with two arguments form) to ...

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

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

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

0

If your file is test.txt you may use: sed ':a;N;$!ba;s/\n/ /g' test.txt|sed 's/\\{{$}.*{$}\\}//' the first sed removes all newlines, the second remove the text inside the tags. I don't know if you need a more general solution 0 The sed way As sed is a lot lighter than mosts alternatives, there is another solution sed -ne ' /CREATE/{h;d}; s/^ *$$[^ ]\+$$ .*$$)$$;\? *$/\1\2/; ta; s/^ *$$[^ ]\+$$ .*$/\1/; H; bb; :a; H; x; s/\n//; s/\n/,/g; s/ (/(/;p ; :b' This could be written: sed -ne '/CREATE/{h;d};s/^ *$$[^ ]\+$$ .*$$)$$;\? ... 0 This perl script prints the full line (without a \n) if it matches /^CREATE TABLE/ the first word followed by ); and a newline if the line ends with ; and the first word followed by a , if the line ends with a ,. All other input lines are ignored. #! /usr/bin/perl use strict; while(<>) { chomp; if (m/^CREATE TABLE/) { print$_; } ...

2

Lookup tables can be difficult - and expensive - because you have to search both ends of the pattern space simultaneously. It can, at least, be implemented more or less straightforwardly, though. You have to consider that no matter what you do, you can only reliably handle a single match at a time, and so you might as well give up any hope of a global result ...

4

That was a hard one. Assuming you have a file like this: \$ cat file word line with a word and words and wording wordy words. Where: Line 1: is the search pattern that should be held in the hold space and quoted to word. Line 2: is the line to seach and replace globally. The sed command: sed -n '1h; ...

0

I used this: grep -r "old_string" -l | tr '\n' ' ' | xargs sed -i 's/old_string/new_string/g' List all files that contain old_string. Replace newline in result with spaces (so that the list of files can be fed to sed. Run sed on those files to replace old string with new. Update: The above result will fail on filenames that contain whitespaces. ...

0

Crossposted question, therefore crossposted answer: OK, looking at it - your problem is with that split - because by default, it splits on whitespace. Your second file has 3 fields by that yardstick, not two. But also - you're not actually crossreferecing the same things, so your while ( <> ) { loop isn't going to do the trick. In file 1 - you ...

1

In perl v5.22, using a literal { in a regular expression was deprecated, and will emit a warning if it isn't escaped: \{. In v5.26, this won't just warn, it'll cause a syntax error. The author of the software will need to fix this before the 5.26 release. For now, you can safely ignore this warning. See perldelta for details.

Top 50 recent answers are included