How to Cut an Existing Variable and Assign to a New Variable in Bash

How to cut an existing variable and assign to a new variable in bash

Let's define an example var:

$ var='The quick brown fox jumped over the lazy dog on the way to the market'

Now let's select characters 18 through 53 using cut:

$ echo $(cut -c 18-53 <<<"$var")
ox jumped over the lazy dog on the w

Because cut expects to read from standard input (if not a file), we use <<< to tell bash to provide the contents of $var on standard input. This is called a here string.

Alternatively, we can select the same characters using bash alone:

$ echo ${var:17:36}
ox jumped over the lazy dog on the w

The construct ${var:17:36} is called substring expansion. It selects 36 characters starting from position 17. (In bash, unlike cut, the first character is numbered zero.)

We can, of course, assign the selected string to a variable:

$ token=${var:17:36}
$ echo "$token"
ox jumped over the lazy dog on the w

Or:

$ token=$(cut -c 18-53 <<<"$var")
$ echo "$token"
ox jumped over the lazy dog on the w

POSIX

The above commands work in bash. If we want portability to POSIX shells, then we can use neither substring expansion nor here strings. Instead, as Gordon Davisson points out, we can use:

$ echo "$var" | cut -c 18-53
ox jumped over the lazy dog on the w

or:

$ token=$(echo "$var" | cut -c 18-53)
$ echo "$token"
ox jumped over the lazy dog on the w

gniourf_gniourf suggests yet another POSIX method, this one avoiding external processes:

$ printf '%.36s\n' "${var#?????????????????}"
ox jumped over the lazy dog on the w

Comparison of cut and bash substring expansion

As David C. Rankin points out in the comments, there are strong advantages to uses bash's internal string handling. One is that the use of bash's internal commands avoids the spawning of additional subshells and executables. If the additional subshells are spawned within a loop, this can greatly impact performance.

Also, command substitution has the side-effect of removing trailing newlines from its output. This can cause unwanted surprises. Using bash's internal string handling avoids this side-effect.

split or cut and assign values of a variable to another variable in unix

In ksh you can use read:

var="test one two"
echo "$var" | read temp1 temp2 temp3
$ echo "$temp1"
test
$ echo "$temp2"
one
$ echo "$temp3"
two

bash script use cut command at variable and store result at another variable

The awk solution is what I would use, but if you want to understand your problems with bash, here is a revised version of your script.

#!/bin/bash -vx

##config file with ip addresses like 10.10.10.1:80
file=config.txt

while read line ; do
##this line is not correct, should strip :port and store to ip var
ip=$( echo "$line" |cut -d\: -f1 )
ping $ip
done < ${file}

You could write your top line as

for line in $(cat $file) ; do ...

(but not recommended).

You needed command substitution $( ... ) to get the value assigned to $ip

reading lines from a file is usually considered more efficient with the while read line ... done < ${file} pattern.

I hope this helps.

How to bring the output of a cut-command into a variable?

When you assign to EP, the first section, $fixedFilePath leaves nothing on stdout for the pipe to take. What it does is executes the contents of that variable. You need to echo.

echo $fixedFilePath | rev | cut -d '.' -f 1 | rev

Now to capture that output you need to execute it as part of the assignment. There are various ways to go about this but what I found worked in your case was surrounding it in backticks:

EP=`echo $fixedFilePath | rev | cut -d '.' -f 1 | rev`

How do I set a variable to the output of a command in Bash?

In addition to backticks `command`, command substitution can be done with $(command) or "$(command)", which I find easier to read, and allows for nesting.

OUTPUT=$(ls -1)
echo "${OUTPUT}"

MULTILINE=$(ls \
-1)
echo "${MULTILINE}"

Quoting (") does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1) would work fine.

How to assign echo to a variable in Bash?

You can use $() (command substitution):

test=$(echo "foo=bar" | cut -d "=" -f1)
echo "$test"
foo

Bash: How to split a string and assign multiple variables


x="http://web.com/sub1/sub2/sub3/sub4/sub5/sub6"
IFS="/" read -r foo foo foo foo var1 foo var2 var3 foo <<< "$x"
echo "$var1 $var2 $var3"

Output:


sub2 sub4 sub5

Or with an array:

x="http://web.com/sub1/sub2/sub3/sub4/sub5/sub6"
IFS="/" read -r -a var <<< "$x"
echo "${var[4]}"
declare -p var

Output:


sub2
declare -a var='([0]="http:" [1]="" [2]="web.com" [3]="sub1" [4]="sub2" [5]="sub3" [6]="sub4" [7]="sub5" [8]="sub6")'

From man bash:

IFS: The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command.



Related Topics



Leave a reply



Submit