Tag Info

Hot answers tagged

12

This works, as indicated by jasonwryan: awk 'BEGIN{print "START"}; {print}; END{print "END"}'


8

This can be done in sed with sed -e $'1i\\\nSTART' -e $'$a\\\nEND' 1i means insert before line 1; $a means append after the last line.  The $'…' syntax is bash-specific.  In other shells, you should be able to do this with: sed -e '1i\Enter START' -e '$a\Enter END'Enter


7

If you're already using sed, you can use 1 to match the first line and $ to match the last line (see Scott's answer). If you're already using awk, you can use a BEGIN block to run code before the first line and an END block to run code after the last line (see Michael Durrant's answer). If all you need to do is add a header and a footer, just use echo and ...


6

If I understand correctly, you want to change _x to X as long as it occurs inside '...' strings. Then, with GNU sed, you could do: sed -E ":1;s/^(([^']|'[^']*')*'[^']*)_([^'])/\1\u\3/;t1" That is replace a _X following '... itself following a sequence of either non-quotes or matched quotes. Which on an input like: foo_bar 'ab_cd_ef gh_ij' zz_zz ...


4

Line 1: Your hashbang line is not correct, use: #!/bin/bash Line 3: Take care with the test utility (it needs a space before the closing ]): if [ -f KeyFile ] Line 5: In the sed command, use -i to activate in-place editing of sed, else the edits are only printed to the stdout: sed -i 's/[0-9][0-9]NM/Okay/g' KeyFile


3

This means capture an underscore. Examples of matches include: A A_ AAA A_123 A_abc Use a site such as https://regex101.com/ to test your regular expressions and get an explanation on what each part means.


3

The sed command appears to be taking the basename of the file. It removes anything before a slash. The execute function must be defined in utils.sh, which I don't see in that repo. It looks like it runs the command given as its first argument then (on success?) prints the message given in its second argument. Looks to me like the upshot is to make, ...


2

^ and $ in BRE always match at the start and end of line, as describe here. Any sed which use BRE will do the same way. In case of using N command, sed saw multi lines as one long line in pattern space, with each real line separated by embedded newline \n (literal \ and n). For confirmation: printf '1\n2\n' | sed '1N;/2$/d' output nothing. And: printf ...


2

POSIXly: sed 's/[^[:alnum:]_-]//g' will remove everything is not alpha numeric characters in your current locale, _ and -. $ echo 'foo-bar |' | sed -e 's/[^[:alnum:]_-]//g' foo-bar But if you want to print everything until first space: sed -e 's/^\([^ ]*\) .*/\1/' or awk: awk '{print $1}'


2

Use one grep to filter all lines with length != 5. Use the second grep to filter all words, where any character repeats. egrep '^.{5}$' greek.txt | egrep -v '^.*(.).*\1.*$' Thanks to @StephaneChazelas for pointing out optimizations in a comment: grep -x '.\{5\}' | grep -v '\(.\).*\1'


2

If your [, ] pairs are always matched and you don't criss-cross [...]s with <...>, and your grep supports the -P option (like GNU grep when built with PCRE support), you can do: grep -P '>(?!((?:[^]]|\[(?1)\])*)$)' That is, look for a > that is not followed by only matched [...] pairs. It uses PCREs' (?1) recursive matching mechanism.


2

Assuming: hour=09 Just use that: grep "\.$hour" file With the single quotes in your example, the variable is not interpreted as variable. Therefore the pattern searches for $hour. Also the dot has to be escaped, else it would match any character.


2

Substitute is not needed, you can simply delete lines starting by a digit : sed '/^[0-9]/d' And if you really must use substitution : sed 's/^[0-9].*//'


2

I'd use perl for this: perl -pe "1 while s/('\w+)_([^']*)/\$1\\u\$2/" <<END my_variable = 'a_string_with_snake_case_inside' delta_llimits=Limits(general_settings['signal_lower_limit'] END my_variable = 'aStringWithSnakeCaseInside' delta_limits=Limits(general_settings['signalLowerLimit'] That means: while the search and replace continues to find ...


1

POSIXly, you could do it with sed: sed ' h; # make a copy of the pristine line on the hold space :1 /\[[^]]*<[^]]*>[^]]*\]/{ # found a [...<x>...] g; # retrieve our saved copy and branch off b } s/\[\([^]]*\)\]/\1/g; # remove inner [...]s # and loop if that s command was successful t1 # no [...] left to ...


1

You can set record separator to # empty line # empty line [ just need to escape [ with double backslashes. After that search for {N} string and if you find one print the whole record adding back \n\n[ if the record is not the first one. Here is how I would do that: awk 'BEGIN{RS="\n\n\\["}/\{N\}/{if(NR!=1) printf ...


1

sed operates on pattern space. I think POSIX sed page is pretty clear: APPLICATION USAGE Regular expressions match entire strings, not just individual lines, but a <newline> is matched1 by '\n' in a sed RE; a <newline> is not allowed by the general definition of regular expression in POSIX.1-2008. Also note that '\n' cannot be ...


1

Not super elegant, but here you go: sed -ne 's/.*"test-summary".* \([0-9][0-9]* right [^&].*exceptions\)&nbsp.*/\1/p' For example: $ echo '<script>document.getElementById("test-summary").innerHTML = "<strong>Test Pages:</strong> 1 right, 0 wrong, 0 ignored, 0 exceptions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' | sed ...


1

A Perl solution: perl -pe 's/^( *- )(.+)/$1."X"x length($2)/e' This uses "X" x length($2) to get the correct number of Xs in the replacement. Test input: - Hello World - Earth This is not - censored output: - XXXXXXXXXXX - XXXXX This is not - censored


1

You can do it with sed too: sed '/^[[:blank:]]*-[[:blank:]]/{ h s/// s/./X/g x s/\([[:blank:]]*-[[:blank:]]\).*/\1/ G s/\n// }' infile This copies the line over the hold buffer, removes the first part [[:blank:]]*-[[:blank:]], replaces the remaining characters with an X, then exchanges pattern/hold space so now the censored string is in the hold pattern ...


1

$ awk '/^[ ]*- /{gsub(/[^ -]/,"X",$0)}1' <<EOM - Hello - World 2015 This is not - censored EOM - XXXXX - XXXXX XXXX This is not - censored The awk expression looks for any lines that begins with a - character, after optional whitespaces. For matching lines, the gsub() command replaces all characters except for whitespaces and the - character. ...


1

Your issue is the -f option. Instead of specifying the file to search, -f specifies a file to read a list of patterns from. OS X grep's man page explains it, though not very clearly: -f file, --file=file Read one or more newline separated patterns from file. Empty pattern lines match every input line. Newlines are not considered part of ...


1

This removes all pure-alphabetic words from the beginning of the line: $ sed -r 's/^([[:alpha:]]* )*//' filename.tsv AHF123432 | 123432 | dhfshfjdh AFG23412 |23412 | dfshjhfjdhj Or, to save the output in a new file: sed -r 's/^([[:alpha:]]* )*//' filename.tsv > final.tsv How it works [[:alpha:]]* matches any number of alphabetic characters ...


1

vim regex engine support Bracket Expression, you can defined a class of characters as a sequence of characters enclosed by square brackets [...]: /[+\-*/%(=]/ will match any character from those characters (You need to escape -, which defined a range between characters, to match it literally) To check current line contains any in set of characters: if ...


1

You could use find with two -execs, the second one will be executed only if the first one is successful, e.g. searching only in .cpp, .c and .cs files: find_code() { find ${SRCDIR} -type f \ \( -name \*.cpp -o -name \*.c -o -name \*.cs \) \ -exec grep -l "= ${1}" {} \; -exec grep -n -C5 "= ${1}" {} \; } so the first grep prints the filenames that contain ...



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