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{$_} = ...


3

Since arithmetic is required, sed is not the right tool. The solution below uses awk. Answer for revised question You say the unwanted hashes always have names that include a number followed by a letter. In that case: $ awk '/^interface .*[[:digit:]][[:alpha:]]/{$0="interface ethernet" ++f} 1' file1 ! interface ethernet1 xxxx yyyy zzzz ! interface ...


2

With perl: perl -n -e 'if (m/^Trailer/) {print "\n"} else {chomp; print $_," "}' arun.txt Output: Hdr1 Dtl1.1 Dtl1.2 Dtl1.3 Dtl1.4 Hdr2 Dtl2.1 Dtl2.2 Dtl2.3 Dtl2.4 Dtl2.5 HdrN DtlN.1 DtlN.2 DtlN.3 Note: the output has a trailing space character on each line. If you don't want that, get rid of it by piping the output through sed -e 's/ $//'. Or use ...


2

The reason this doesn't work can be inferred from the error message (that you omitted to provide): sed: -e expression #1, char 14: unterminated `s' command The sed command does not accept a multi-line value. You have to collapse your multiple lines into a single line. You could do this with a script such as this: #!/bin/ksh S_ids="'$(cut -d'|' -f1 ...


2

All you need to do is parse the command's output and replace the first newline (\n) with a comma. This should work: ssh test01 "hostname && cat /etc/sysconfig/rhn/rhnsd " | perl -00pe 's/\n/,/' Here we're using perl to slurp the entire file into memory (-0) and replace the first \n with a comma. The -p tells perl to print each input "line" ...


1

Try awk to achieve this : awk '/dn/{if(b)print b;b=$0;next}NF{b=b "|" $0}END{print b}' filename > file.out


1

Unless you have GNU awk 4.1.0 or later... You won't have such an option as sed's -i option so instead do: for file in * do awk -v lines=2 'BEGIN { ignore = -1 } /radius-server/ { ignore = NR + lines } NR != ignore { print }' "$file" done > result.txt This works as follows: BEGIN { ignore = -1 } # initialize ignore with -1 so NR will never ...


1

sed seems like the right tool: sed -i '/radius-server/!b;n;n;d' filename How it works: /radius-server/!b # as long as it's NOT 'radius-server' do nothing (branch to end) n # read next line (replace current line with next line) n # same as above - we now read 2 lines d # delete the current line UPDATE - to modify multiple files, simply use glob ...


1

With gawk, PROCINFO["identifiers"] is an array with information about variables. Use it like: PROCINFO["identifiers"]["your_variable_name"]. The possible value returned is one of "array", "builtin", "extension", "scalar", "untyped", "user". There is only a general scalar, which includes both strings and numbers. The gawk interpreter just tries its best ...


1

AFAICT from your sample input and output, you want to round the first field to the nearest 10, round down the second field down to the nearest 10, and leave the third field alone. awk '{printf "%5.0f\t%6.0f\t%.2f\n", int($1/10+0.5)*10, int($2/10)*10, $3}' file Output: 50460 170400 0.01 50440 170380 0.03 50460 170380 0.04 50480 170380 0.01 ...



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