New answers tagged

2

First step Replace spaces with line ends using sed Second step Filter the output using grep Example sed -e s/\ /\\n/g file | grep .swf


1

A few options depending on the shell: POSIX: t=${t%?} Bourne: t=expr " $t" : ' \(.*\).' zsh/yash: t=${t[1,-2]} bash/zsh: t=${t:0:-1} ksh93/bash/zsh/mksh: t=${t:0:${#t}-1} ksh93/bash/zsh/mksh: t=${t/%?} ksh93: t=${t/~(E).$/}


0

Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use: SRC_IP_MSG=${SRC_IP_MSG%??}


8

Use $^array. It turns the array into a sort of brace expansion of the array. As in when a=(foo bar baz), $^a would be a bit like {foo,bar,baz}. $ a=(foo bar baz) $ echo prefix${^a}suffix prefixfoosuffix prefixbarsuffix prefixbazsuffix For multiplexing arrays: $ a=(1 2 3) b=(a b c) $ echo $^a$^b 1a 1b 1c 2a 2b 2c 3a 3b 3c Naturally, if the prefix or ...


2

You probably meant !~, "does not match regular expression", rather than !=, "is not equal to". For $var =~ regexp and $var !~ regexp, the pattern on the right should not be quoted. For == and !=, you may quote the part on the right (and in fact I'd encourage this). Since != expects a string, "[yY] was not parsed as a regular expression. Rather, the shell ...


3

Pattern matching is done with case statements in all Bourne-like shells. is_absolute() { case "$1" in ///* | //) true;; //*) false;; # on some systems, //foo is special and is # not an absolute path. // alone is / /*) true;; *) false esac } Remove the first two entries on systems that don't ...


1

Just check the first character of the string using substring syntax: [[ ${var:0:1} = / ]] || return 1


1

POSIX define absolute path as a pathname beginning with a single or more than two /. There's a utility called pathchk to check pathname, so you can do: [ -z "${1%%/*}" ] && pathchk -pP "$1" -p tells pathchk to perform check for path that: Is longer than 256 bytes (See _POSIX_PATH_MAX) Contains any component longer than 14 bytes (See ...


2

If by absolute path you mean that it starts with /, and we are talking about bash (as tag suggest): $ var1='/tmp/foo' $ var2='tmp/foo' $ [[ "$var1" =~ ^/ ]] && echo yes || echo no yes $ [[ "$var2" =~ ^/ ]] && echo yes || echo no no


2

An absolute path would begin with / not contain any /../ or /./ not begin with ../ or ./ not end with /.. or /. so you could do this (portably) with a case statement: case "x$1" in (x*/..|x*/../*|x../*|x*/.|x*/./*|x./*) rc=1 ;; (x/*) rc=0 ;; (*) rc=1 ;; esac return $rc This ...


5

[ "$1" != "${1#/}" ] || return 1 There may be a better way (that's why I asked). This code strips off any leading / in $1 and checks that the result is not the same as $1 itself.


0

With gnu sort having input.txt as input (explain): cat input.txt | sort -t _ -k6n,6.4 -k2M -k3n -k4.1n,4.2 -k4.4n,4.5 -k4.7n,4.8 Add --debug flag to see fields used for sorting. Useful when you have to tweak substrings: Wed_Dec_30_16:00:41_PHT_2015.zip ____ ___ __ __ __ __


0

I know you have your data in an array , but I tested with a file. Shouldn't matter - logic is the same. Just convert to seconds since epoch, test which epoch number is biggest, and remember that number - thats your most recent date. Then obviously you need to reconcile that string against your file structures to get the specific file you were looking for. ...


1

declare is a bash-only command, that's the equivalent of typeset in ksh or zsh. dash has no equivalent command and has no array support other than with "$@". The way to go here is to store the list of user names in a newline delimited list: users="me you someone" And use: who | grep -F "$users" However, that will look for me, you or someone anywhere ...


2

Your function was trying to evaluate $returnString on the left-hand side during the assignment; instead, you want: eGrepUsernames(){ returnString=""; for username in "${usernames[@]}" do returnString="$returnString -e $username" # this line changed done printf '%s ' $returnString; ## so did this one }


0

This will do what the title of this question ask: "split string at first occurrence of a delimiter" #!/bin/bash while read -r line; do [[ $line =~ :(.*) ]] && echo "${BASH_REMATCH[1]}" done <<<"$(grep "100200300" my.log*)" However, I believe that what you need is: grep -h "100200300" my.log* The -h option will result in file ...


1

As steeldriver commented, the preferred way is to use the -h (--no-filename) option. If your implementation of grep didn't include this option, you could use sed or cut to alter the output: grep [OPTIONS] PATTERN [FILE...] | sed 's/:\(.*\)/\1/' grep [OPTIONS] PATTERN [FILE...] | cut -d : -f 2- Note, however, that this solution is not general, as it breaks ...


6

In addition to above use newline by itself: echo "${string/abab/ }" note the quoting to avoid \newline substitution by space For old bash version can be suitable: printf "%s\n" "${string%%abab*}" "${string#*abab}"


9

Use ANSI C style escape sequence $'\n' to indicate newline: $ string=xyababcdabababefab $ echo "${string/abab/$'\n'}" xy cdabababefab Or use zsh to use just \n: % string=xyababcdabababefab % echo "${string/abab/\n}" xy cdabababefab


5

You should use -e with echo as follows: echo -e ${string/abab/'\n'} From manpage: -e enable interpretation of backslash escapes If -e is in effect, the following sequences are recognized: \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage ...


1

I tend to use sed to do the replacement and test before making changes: for i in */pre.nii; do j=$(echo $i | sed 's/\//_/') echo $j done then run it for i in */pre.nii; do j=$(echo $i | sed 's/\//_/') mv -v $i $j done


4

To replace one / (escaped with \) by _: for i in */pre.nii; do echo mv "$i" "${i/\//_}"; done If everything looks fine, remove echo.


6

Read about bash's parameter expansion. In your examples you're using the search and replace syntax which substitutes the first occurrence of the given pattern. ${parameter/pattern/string} Example: > X="aaaa" > echo ${X/a/b} # substitute first occurence of a with b > baaa The syntax to replace all occurrences is: ${parameter//pattern/string} ...



Top 50 recent answers are included