Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

7

Perl to the rescue! perl -pe 'if (s/^\t(\t*)//) { $n[ length $1 ]++; splice @n, 1 + length $1; } print $1, join(".", @n), ": " ' -- file s/^\t(\t+)// removes the leading tabs, but captures them (without the first one) into $1. The @n array is used to keep the numbers. The number corresponding to the current ...


5

sed -r 'H;$!d;x;s:\n::g;:l;s:(\\x..)(.*)\1:\2:;tl' allHexChars.txt allowedChars.txt > missingChars.txt The above GNU sed script assumes two things as I understood them from the question: inside the files no hex character is listed more than one time the first file contains all the hex characters from the second file To visualize the differences, use: ...


4

With GNU awk for multi-char RS and RT: $ awk -v RS='.{4}' 'NR==FNR{a[RT];next} !(RT in a){d=d RT} END{print d}' allowed all \x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x3a\x3f\x40\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\...


4

You want to quote the word EOF. From the docs (man bash), If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. So it'll look like, $ perl <<'EOF' EOF If EOF is unquoted, the $! is interpolated from the shell before being sent to Perl. $ cat <<EOF ...


3

With sed: sed 's/\[-.*-]//g' Example: ~ echo 'foo[-.(D_)-]bar' | sed 's/\[-.*-]//g' foobar If you don't escape the [, [-.*-] will be seen as the start of a bracket expression (containing a list or range of characters, in this case the list -, ., *).


1

Is using regular expressions an option? I still had to shell out to sed to cleanly replace %...% with (.*), though: $ psource=/backup/monthly.1/where/somehost-someshare $ tsource=/backup/%PERIOD%/where/%HOST%-%SHARE% $ # replace labels with '(.*)', need sed to avoid greediness of * in ${../../..} $ tsource_re=$(sed 's/%[^%]*%/(.*)/g' <<<"${tsource}"...


1

this should do the trick: tsource='/backup/%HOST%/%SHARE%/%PERIOD%' psource='/backup/somehost/someshare/monthly.1' IFS=$'\n' read -d '' -ra var_names <<< $(grep -Po "(?<=%)[[:upper:]]+(?=%)" <<< "$tsource") IFS='/' read -ra var_values <<< $(echo ${psource#/*/}) declare -A vars=( ["${var_names[0]}"]="${var_values[0]}" ["${...


1

If you're using Perl (the language) and not some tool using Perl-compatible regexes, there's no reason to restrict yourself to just one regex. Something like this would match just the uncommented line: $ perl -ne 'print if not m,^\s*//, and /\bfoobar\b/' < file.txt foobar # match me Even with other utilities, or non-Perl regexes, it might be ...


1

I think the issue of negation should not be over complicated. e.g. say I am looking to reject these lines: "^(\s*//\s*).*(\bfoobar\b).*$" ie. having the comment signature '//' anywhere in whitespace, at the beginning, followed by anything which includes 'foobar' as a whole word (included brackets for clarity). to negate this we do: "^(?!\s*//\s*).*(\...


1

It's your use of double quotes. You cannot use double quotes "inside" double quotes unless they are escaped. Instead you could either escape your double quotes or more preferrably alternate quotes by using single quotes instead: #!/usr/bin/perl use warnings; use strict; exec("sed -i -r '4{s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt");


1

A one-liner: awk '{ if ( NF > 0 && $1 !~ /^\/\// ) { bl=0; for (i=1;i<=NF;i++) printf "%s ", $i; }; if ( NF == 0 && bl == 1 ) {printf "\n" }; if ( NF == 0 && bl == 0 ) {bl=1; printf "\n\n" } } END { if ( bl == 0 ) { printf "\n" } } ' /tmp/my_file Explanation: if ( NF > 0 && $1 !~ /^\/\// ) { bl=0; for (i=1;i<=NF;...


1

With bash: inCol=${postmsg%%$'\n'*} inVal=${postmsg#*$'\n'} The first removes the longest suffix pattern \n*, the second removes the shortest prefix pattern *\n.


1

An alternative way to deal with Hex values of Characters is to convert those hex values back to characters. That is an easy conversion with printf: printf "$(cat allHexChars.txt)" The same could be done to the allowedChars.txt file. Then, it is a matter of deleting characters with tr and converting back to hex values with hexdump: printf $(<...


1

1) For a pattern with many slashes, you should use a different delimiter for the s command to make it more readable (you don't need to escape the slashes then). 2) You seem to be using extended regular expressions, so you have to set the -E option to sed 3) If you use alternate strings for a part of the pattern, you need to surround this with () like (...


1

You can do something like: s/#.*|((?<!\S)parameter\h*=.*)/$1 ? "parameter=replaced" : $&/ge; The idea is that the #.* (which we replace with itself) will munch away all the comments. And in the second part of the alternation, we look for your pattern (here parameter=... provided its not preceded by a non-whitespace) in what's left. Another approach ...


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