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

With sed: $ sed -e '1i\ Description of the following table: ' <file Description of the following table: TABLE1 1234 9555 87676 2344


4

printf "%s\n" 1 i "Description of the following table:" . w | ed filename The printf outputs ed commands (one per line) which are then piped into ed filename. ed edits the file as instructed: 1 # go to line 1 i # enter insert mode Description of the following table: # ...


4

Actually echo and cat are enough to do what you want : echo "Description of the following table:" | cat - file The - argument tells cat to read from stdin.


4

Anchor the text in the regex sed -i '/^name$/d'


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

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


2

AIX sed needs each command on a separate line. See man page and try sed '/some_pattern/{ N N s/word1/word2/ }'


2

awk allows you to use a record separator other than newline. You can use it as follows: awk 'BEGIN { RS=\x06 } ...' input-file Where ... is whatever you want to do with the data.


2

The awk option would be : gawk ' BEGIN{print "Description of the following table:"} {print $0}' file > temp && mv temp file A bit more work than sed here because sed has got an in-place edit option -i by which you could directly write to file.


1

Yes, your understanding is correct. From your format, I assume you are using GNU sed (other implementations might need a space between the -i and the .bak and some might not support -i at all). Its -i works as follows (from info sed): -i[SUFFIX] --in-place[=SUFFIX] This option specifies that files are to be edited in-place. GNU `sed' does this by ...


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

Following terdon's advice, im adding an alternative approach using sed, i mainly include it for completeness, however unlike the awk '{printf "%s,", $1}' which aggregates whilst ommitting the newline, with sedwe are actively replacing the newlines, thus it would also fit in when we wanted to replace a different character. It is inefficient though. The ...


1

You missed the last /, it's s/string3/string3_replaced/: sed -e '/string1/{N;s/string2/string2_replaced/;N;s/string3/string3_replaced/}' file1 Note that not all sed implementations support multiple commands in the same line within { and }. For portability: sed -e '/string1/ { N s/string2/string2_replaced/ N s/string3/string3_replaced/ }' file1


1

Just use "alternatives" using the \|: sed 's/\(:\(20\|25\|28C\|60F\|64\):\)/\n\1/g'



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