Tag Info

New answers tagged

1

You can do task by sed on-liner: sed '/(\?\([0-9]\+\))\?$/s//; number=\1/ t n s/$/; number=0/ :n /^.*, [0-9]\?\([WURBG]\)/{ s//color=\1/ s/[WURBG]\{2,\}/M/ } /[Ll]and/s/^[^;]*/color=L / /color/!s/^[^;]*/color=C /' file But I'd like to ...


0

While you can do this sort of thing by wrapping the more primitive Unix tools — grep, sed, awk, etc. — in a shell script, this sort of problem really wants to be handled in a full programming language that has a powerful regular expression system. Personally, I'd reach for Perl: #!/usr/bin/perl -w use strict; my $line = 0; my ($junk, $color, $number); ...


4

Read Why does my shell script choke on whitespace or other special characters? to understand the why. The 1-sentence version is: always use double quotes around variable substitutions. echo "$firstcontent" >/tmp/myfirstcontentfiles.txt mostly works: it doesn't collapse whitespace or expand wildcards in the value of the variable. However this still ...


0

You could use the read built-in read -r _ y <<<"$x" printf "%s\n" "$y" true


0

you can use awk: echo "rtcpOnNbActive true" | awk '{print $NF}' true NF number of field in the current record using sed: echo "rtcpOnNbActive true" | sed 's/.* //g' true using string expression: a="rtcpOnNbActive true" echo ${a##* } true using grep: echo "rtcpOnNbActive true" | grep -Eo "[a-z]+$" true -o is ...


6

Assuming that there's at least one space before the substring you wish to extract (and that the substring does not contain any spaces), you can do this with a simple parameter expansion: x="rtcpOnNbActive true" y="${x##* }" echo "[$y]" output [true]


0

Try this way: y=$(echo $x | awk '{print $2}') echo $y echo $x display the value of x. awk '{print $2}' prints the second field of the previously displayed x. $(...) hold the output and let assign it to y.


0

in bash v=foo-bar && v=$(IFS=- read -ra x <<<"$v"; printf '%s' "${x[*]^}") && printf '%s\n' "$v" Foo Bar In python v=foo-bar python -c 'import os, string print string.capwords(" ".join(os.environ["v"].split("-"))) ' Foo Bar


2

For the splitting in a POSIX shell you could do: set -f; IFS=-; set -- $1; IFS=' ' There - now all of your dashes are spaces and you can have the entire string in "$*" or else each space - previously dash - separated string in $1 $2 $3 ... (the total count of which is available to you in "$#") or you can get them as an expandable argument list in "$@". ...


1

Step 1: Replace the - with a space For this you can use tr command as follows: tr "-" " " Step 2: Make first letter of every word capital For this, you can find the word boundary using \b and make immediate letter capital. The . represents immediate letter after word boundary \b, \U makes it capital and & will keep all other letters in the letters as ...


2

Start by changing dashes to spaces, like: sed 's/-/ /g' Uppercasing the first letter is already solved (go upvote them; or just mark this question a duplicate of that one). Then combine them: sed -e 's/-/ /g' -e 's/\b\(.\)/\u\1/g' $ echo "foo-bar-baz-nonce" | sed -e 's/-/ /g' -e 's/\b\(.\)/\u\1/g' Foo Bar Baz Nonce $


1

One solution using perl, not restricted to only `foo-bar` $ cat file aaa-zzz-eee-rrr foo-bar code $ perl -ne 'print join " ", map { ucfirst } split /-/' file Aaa Zzz Eee Rrr Foo Bar Another solution using pure bash while IFS='-' read -r -a words; do printf '%s\n' "${words[@]^}" | paste -sd ' ' done < file Output: Aaa Zzz Eee Rrr Foo Bar


0

If that string is part of a file or output: sed 's/foo-bar/Foo Bar/g' file Assuming there are no "strange" characters in the string (/, \, &): string="foo-bar" set -- $(echo "${string//-/ }") replstr="$(for word in $*; do echo -n "${word^} "; done)" replstr="${replstr% }" sed "s/${string}/${replstr}/g' file


0

Following the solution giving by http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable I fixed my script this way: echo "8:X${BASH_REMATCH[1]%"${BASH_REMATCH[1]##*[![:space:]]}"}X" eval "$1='${BASH_REMATCH[1]%"${BASH_REMATCH[1]##*[![:space:]]}"}'" This is a double string manipulation and the first/inner part removes ...


1

%% does glob matching, not regex. That means ${foo%% } will remove the longest trailing string matching a single space character, which of course is just a single space character, and ${foo%% *} will remove the longest trailing string starting with a space character. You'll probably be better off using awk to split the string into fields.



Top 50 recent answers are included