Hot answers tagged

13

This perl script builds a hash with words (read one per line from stdin, and/or from any filenames listed on the command line) as keys, and syllable counts as the values. Then it prints the hash keys, sorted by the syllable counts. #! /usr/bin/perl use strict; use Lingua::EN::Syllable; my %words = (); while(<>) { chomp; $words{$_} = ...


5

sed does not understand \d. You can use [0-9] or, more generally, [[:digit:]] in its place: $ sed -r 's/.*(X[[:digit:]])(.*)45.*/\1\2/' test.txt X1yad X2fad X3had X4wad X5mad


3

This is a perfect use case for ex, the POSIX-specified tool of choice for file editing. (If you've ever used vi, by the way, you are likely familiar with ex since everything you type in vi that starts with a colon : is an ex command. ex is the predecessor of vi.) printf %s\\n 'g/NAME#AAAA/ /AGE/t- | s/^/#/ | /AGE/s/.*/AGE NIL/' x | ex input.txt If you ...


2

For each line you can just use substr($line,30,2) to get the two characters you want. So something like #!/usr/bin/perl use warnings; use strict; open(my $file1,">file1"); open(my $file2,">file2"); open(my $file3,">file3"); while(<>) { my $ch=substr($_,30,2); if ($ch eq '0A') { print $file1 $_; } elsif ($ch eq '0B') { print $file2 ...


2

To replace Helloby Hii you could use: s/Hello/Hii/ tr works different. Using tr both character sequences are handled as lists. The first character of the first list is replaced by the first character of the second list. So, H is replaced by H, e is replaced by i and so on.


2

You can do this with awk, keeping track of numeric versus non-numeric columns and summarizing at the end: #!/usr/bin/awk -f BEGIN { width = 0; } { if (width < NF) width = NF; for (n = 1; n <= NF; ++n) { if ( $n ~ /^[0-9]+$/ ) { number[n] += $n; total[n] += 1; } else { others[n] ...


2

You can enumerate the directory contents as follows: my @files = <*.xml>; # or: my @files = </your/path/to/*.xml>; foreach my $file (@files) { $twig->parsefile($file); }


2

One way would be to save the previous line, and print when the current and previous line both match: bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) a pat 1 pat 2 b pat 3 bash-4.1$ (echo a; echo pat 1; echo pat 2; echo b; echo pat 3) | \ perl -nle 'print "$prev\n$_" if /pat/ and $prev =~ /pat/; $prev=$_' pat 1 pat 2 This will ...


2

I'll use the same test file as thrig: $ cat file a pat 1 pat 2 b pat 3 Here is an awk solution: $ awk '/pat/ && last {print last; print} {last=""} /pat/{last=$0}' file pat 1 pat 2 How it works awk implicitly loops over every line in the file. This program uses one variable, last, which contains the last line if it matched regex pat. ...


1

For the record, you could also do this with sed: sed -s '$!N /.*PATTERN.*\n/{/\n.*PATTERN/{x;/^1$/!s/.*/1/;b v};//!{x;/^1$/{s/./0/;b v};//!D}} //!{${/PATTERN/{x;/^1$/{b v}}};D;};: v;x;P;D' file1 file2 ... fileN That's gnu sed. With other seds you'd have to process one file at a time: sed '$!N # if not on the last line pull in the next ...


1

Use find command to do that, find /xxx -name '*.jsp' -exec perl -pi.13.04.2016 -w -e "s/\b1800 102 6022\b/1860 266 2666/g;" {} \;


1

perl -pe 'BEGIN{$/=""} s/^(NAME#AAAA.*\n)(AGE.*?)(\n+)$/#$2\n$1AGE NIL$3/s' ex1 very brief explanations: For all the registers in input | perl -p separator= one or more empty lines | BEGIN{$/=""} do: | substitute | s/ | ^(NAME AAAA.*\n)(AGE.*?)(\n+)$ | ...


1

When your are manually running the ssh command, I assume you have configured password-less ssh and ~user/.ssh has the necessary keys. When running this via web page, the ssh is executed as user apache (or user httpd) and ~apache/.ssh is used for the keys. That user does not have password-less ssh so "Permission Denied". You are viewing the web-page from ...


1

You can use Vim in Ex mode: replace string ALF with BRA in all files in the current directory? for CHA in * do ex -sc '%s/ALF/BRA/g' -cx "$CHA" done do the same recursively for sub directories? find -type f -exec ex -sc '%s/ALF/BRA/g' -cx {} ';' replace only if the file name matches another string? for CHA in *.txt do ex -sc ...


1

Not sure awk is the correct tool for that job since your search is over multiple lines based. I would think it's a job for perl. Going through your file you can use (similar to your awk statement): if (/^START/ .. /^END/){} in there you store your lines in an array, that you're going to print if say Frankfurt is met (use a boolean here): push @lines, $_; ...


1

Definitely we can use awk, try this one awk -v RS="END" '$0~/Frankfurt/{print $0 RS}' file


1

sed 's/abcd\(X[0-9][a-z]ad\)45das/\1/g' your_file_name should do it.



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