Tag Info

Hot answers tagged

12

You're not anchoring the expression. It can match in the middle, so any vowels "outside" your match are allowed. Add a ^ and $ to prevent that. $ echo abstemiousnesses | grep -iE '[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*' abstemiousnesses $ echo abstemiousnesses | grep -iE '^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$' ...


6

paste: {...pipeline...} | paste -d " " - - That says: "read a line from stdin (the first -), read another line from stdin (the second -), then join them with a space" a bash-specific technique: $ x=$(grep -o pattern. test.txt) $ echo "$x" pattern1 pattern2 $ mapfile -t <<< "$x" $ echo "${MAPFILE[*]}" pattern1 pattern2 ref: ...


6

A regular expression is not a glob. I am guessing that you want files that start with "d". In that case, you need: ls /etc | sort | grep '^d' What went wrong How the following statement behaves depends on the files in the current directory: ls /etc | sort | grep p* Since it is unquoted, the shell will try to expand the glob p*, replacing it with ...


5

I'll put three versions different methods in a row AWK printf %s\\n pattern1 pattern2 | awk -vRS="\n" -vORS=" " '1; END {print RS}' SED printf %s\\n pattern1 pattern2 | sed '$!N;s/\n/ /' TR printf %s\\n pattern1 pattern2 | tr '\n' ' '; echo And there are many more.


3

The GNU awk manual (sec. 3.5) documents that the regex \< is gawk-specific and thus one should not expect it to work in other implementations. According to man mawk, if you place a backslash in front of a nonspecial character, then the backslash is removed. Thus, under mawk, \< is interpreted simply as an angle bracket character. Examples I ...


3

You can do something like: awk '{print} $0 == "<dict>" && previous == "<key>servers</key>" { system("cat other-file.xml") } {previous = $0}'


2

you can do it using shell script or in command-line, just put the output of the command in a variable then echo it: # x=$(grep -e "pattern1\|pattern2" test) # printf '%s\n' "$x" pattern1 pattern2


2

If you only accept two possibilities, you can explicitly state them: ^y$\|^yes$ or, depending on what you mean by "without making another conditional statement", you can use: ^y(es)?$ The ? is equivalent to {'0,1'} and the parentheses groups the es characters together.


2

In bash 3.2 or above: shopt -u compat31 [[ ! $DEBUG_PACKAGE_LIST =~ [^-[:alnum:]]'libmagick++5-dbg' ]]; In bash 3.1: [[ ! $DEBUG_PACKAGE_LIST =~ '[^-[:alnum:]]libmagick\+\+5-dbg' ]]; (note that [a-z]* is redundant since it also matches the empty string so will always match). Works in both: re='[^-[:alnum:]]libmagick\+\+5-dbg' [[ ! $DEBUG_PACKAGE_LIST ...


2

Remove quotes if ! [[ "$scale" =~ ^[0-9]+$ ]]


2

Use -eq operator of test command: read scale if ! [ "$scale" -eq "$scale" ] 2> /dev/null then echo "Sorry integers only" fi It not only works in bash but also any POSIX shell. From POSIX test documentation: n1 -eq n2 True if the integers n1 and n2 are algebraically equal; otherwise, false.


1

A simple way, pipe output to xargs: $ echo -e 'a\nb' | xargs a b This only works with small ouput, because it's limited by maximum characters per command line. The largest value depends on system, you can get this value using getconf ARG_MAX.


1

With sed, you can do this: <your previous commands> | sed '{N; s/\n/ /}' N; tells sed to add the next line into the pattern space, so now sed is working with both lines. s/\n/ / replaces the newline character with a space, "merging" the two lines together.


1

Would something like this work? sed '\|<key>servers</key>|{n \|<dict>| r other-file.xml }' file.xml


1

sed '/keys_line_1/,/keys_line_last/{/keys_line_last/{ h;s/unique_split_point.*//;r /path/to/insert/file x;s/.*unique_split_point//;G }}' sed is not exactly forgiving when it comes to requiring adjustments to an hypothesis. Everything sed does is a direct result of the thing it has just done, and so a very minor error in detail can drastically alter ...


1

With GNU grep when built with PCRE support, you can do: grep -Px '\X{6}' While . matches a character, \X matches an ideogram/graphem. In a UTF-8 locale: $ locale charmap UTF-8 $ printf '\u00e9tuis\n\u00e9tudes\n' | grep -Px '\X{6}' études $ printf 'e\u0301tuis\ne\u0301tudes\n' | grep -Px '\X{6}' études In that latter études, there are 7 characters, ...


1

grep's idea of a character is locale-dependent. If you're in a non-Unicode locale and you grep from a file with Unicode characters in it then the character counts won't match up. If you echo $LANG then you'll see the locale you're in. If you set the LC_CTYPE and/or LANG environment variables to a value ending with ".UTF-8" then you will get the right ...


1

Try this: LC_ALL=C.UTF-8 grep -x '[_[:alnum:]]\{6\}' file -x use to match whole line, and defined by POSIX (See grep). See here for good explanation of what LC_ALL does. You can set LANG or LC_CTYPE to use utf-8 to get the same behaviour. The order taking affect is LC_ALL => LANG => LC_CTYPE.


1

sed -e '/./{H;$!d;}' -e 'x;P;y/\n/ /'



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