This question already has an answer here:
Shell script, saving the command value to a variable
(1 answer)
Closed 9 years ago.
I am trying to cut ABCservice and DEFService dfrom the array and print them.What am I missing here?
urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
for url in "${urlArray[#]}"
do
service=echo $url|cut -f4 -d/
echo $service
done
Expected Output:
ABCService
DEFService
Current Output:
./test1.sh: line 6: http://server:port/ABCservice/services/ABCservice?wsdl: No such file or directory
./test1.sh: line 6: http://server:port/DEFservice/services/DEFservice?wsdl: No such file or directory
What abut this?
service=$(echo $url | cut -d"/" -f4)
echo $service
or directly
echo $(echo $url | cut -d"/" -f4)
The problems in your code:
service=echo $url|cut -f4 -d\
to save a command output in a variable, we do it like this: service=$(command).
your cut had \ as delimiter instead of /. Also it is good to wrap it with brackets: -d "/"
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Using bash string function:
for i in "${!urlArray[#]}"; do
urlArray[i]="${urlArray[i]%\?*}"
urlArray[i]="${urlArray[i]##*/}"
echo ${urlArray[i]}
done
$ urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
$ for i in "${!urlArray[#]}"; do urlArray[i]="${urlArray[i]%\?*}"; urlArray[i]="${urlArray[i]##*/}"; echo ${urlArray[i]} ; done
ABCservice
DEFservice
Your delimiter should be a forward slash
echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
The \ is an escape character. Try \\ instead.
Related
I'm trying to print domain and topLeveldomain variables (example.com)
$line = example.com
domain =$line | cut -d. -f 1
topLeveldomain = $line | cut -d. -f 2
However when I try and echo $domain, it doesn't display desired value
test.sh: line 4: domain: command not found
test.sh: line 5: topLeveldomain: command not found
I suggest:
line="example.com"
domain=$(echo "$line" | cut -d. -f 1)
topLeveldomain=$(echo "$line" | cut -d. -f 2)
The right code for this should be:
line="example.com"
domain=$(echo "$line" | cut -d. -f 1)
topLeveldomain=$(echo "$line" | cut -d. -f 2)
Consider the right syntax of bash:
variable=value
(there are no blanks allowed)
if you want to use the content of the variable you have to add a leading $
e.g.
echo $variable
You don't need external tools for this, just do this in bash
$ string="example.com"
# print everything upto first de-limiter '.'
$ printf "${string%%.*}\n"
example
# print everything after first de-limiter '.'
$ printf "${string#*.}\n"
com
Remove spaces around =:
line=example.com # YES
line = example.com # NO
When you create a variable, do not prepend $ to the variable name:
line=example.com # YES
$line=example.com # NO
When using pipes, you need to pass standard output to the next command. Than means, you usually need to echo variables or cat files:
echo $line | cut -d. -f1 # YES
$line | cut -d. -f1 # NO
Use the $() syntax to get the output of a command into a variable:
new_variable=$(echo $line | cut -d. -f1) # YES
new_variable=echo $line | cut -d. -f1 # NO
I would rather use AWK:
domain="abc.def.hij.example.com"
awk -F. '{printf "TLD:%s\n2:%s\n3:%s\n", $NF, $(NF-1), $(NF-2)}' <<< "$domain"
Output
TLD:com
2:example
3:hij
In the command above, -F option specifies the field separator; NF is a built-in variable that keeps the number of input fields.
Issues with Your Code
The issues with your code are due to invalid syntax.
To set a variable in the shell, use
VARNAME="value"
Putting spaces around the equal sign will cause errors. It is a good
habit to quote content strings when assigning values to variables:
this will reduce the chance that you make errors.
Refer to the Bash Guide for Beginners.
this also works:
line="example.com"
domain=$(echo $line | cut -d. -f1)
toplevel=$(cut -d. -f2 <<<$line)
echo "domain name=" $domain
echo "Top Level=" $toplevel
You need to remove $ from line in the beginning, correct the spaces and echo $line in order to pipe the value to cut . Alternatively feed the cut with $line.
Right Now I am trying to parse the values from my get time and date and break it down by each number
Format of the date/time
#!/bin/bash
prevDateTime=$(date +'%Y-%m-%d-%H:%M:%S')
echo "${prevDateTime}"
I want to be able to list it out like so
echo "${prevYear}"
echo "${prevMonth}"
echo "${prevDay}"
echo "${prevHour}"
echo "${prevMinute}"
echo "${prevSecond}"
and then like
echo "${prevDate}"
echo "${precTime}"
But I am not sure how to parse out the information any help would be great
A regular expression is probably the simplest solution, given the format of prevDateTime.
[[ $prevDateTime =~ (.*)-(.*)-(.*)-(.*):(.*):(.*) ]]
prevYear=${BASH_REMATCH[1]}
prevMonth=${BASH_REMATCH[2]}
# etc.
Technically, there's a "one"-liner to do this using declare:
declare $(date +'prevDateTime=%Y-%m-%d:%H:%M:%S
prevYear=%Y
prevMonth=%m
prevDat=%d
prevHour=%H
prevMinute=%M
prevSecond=%S')
It uses date to output a block of parameter assignments which declare instantiates. (Note that the command substitution is not quoted, so that each assignment is seen as a separate argument to declare. If there was any whitespace in the values to assign, you would have to switch to using eval with slightly different output from date.)
You can use read command with IFS to break down date components:
prevDateTime=$(date +'%Y-%m-%d-%H:%M:%S')
IFS='-:' read -ra arr <<< "$prevDateTime"
# print array values
declare -p arr
# This outputs
# declare -a arr='([0]="2015" [1]="05" [2]="21" [3]="10" [4]="24" [5]="28")'
#assign to other variables
prevYear=${arr[0]}
prevMonth=${arr[1]}
prevDay=${arr[2]}
prevHour=${arr[3]}
prevMinute=${arr[4]}
prevSecond=${arr[5]}
Fast solution using cut:
#!/bin/bash
prevDateTime=$(date +'%Y-%m-%d-%H:%M:%S')
echo "${prevDateTime}"
prevYear=`echo $prevDateTime | cut -d- -f1`
prevMonth=`echo $prevDateTime | cut -d- -f2`
prevDay=`echo $prevDateTime | cut -d- -f3`
prevHour=`echo $prevDateTime | cut -d- -f4 | cut -d: -f1`
prevMinute=`echo $prevDateTime | cut -d- -f4 | cut -d: -f2`
prevSecond=`echo $prevDateTime | cut -d- -f4 | cut -d: -f3`
echo "Year: $prevYear; Month: $prevMonth; Day: $prevDay"
echo "Hour: $prevHour; Minute: $prevMinute; Second: $prevSecond"
test1="one two three four five"
echo $test1 | cut -d $'\t' -f2
I have a string which separated by TAB, and I want to get the second word by cut command.
I've found the question How to split a string in bash delimited by tab. But the solution is not used with cut.
This is happening because you need to quote $test1 when you echo:
echo "$test1" | cut -d$'\t' -f2
Otherwise, the format is gone and the tabs converted into spaces:
$ s="hello bye ciao"
$ echo "$s" <--- quoting
hello bye ciao
$ echo $s <--- without quotes
hello bye ciao
You don't need cut and can save yourself a fork:
$ test1=$(printf "one\ttwo\three\tfour\tfive")
$ read _ two _ <<< "${test1}"
$ echo "${two}"
two
Try to use cut without any -d option:
echo "$test1" | cut -f2
Below is expert from cut man page:
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
I run this:
test1="one;two;three;four;five"
echo $test1 | cut -d \; -f2
and get:
two
and your example:
test1="one two three four five"
echo $test1 | cut -d \t -f2
and get:
wo
Hope that helpful.
It's the problem of \t I think.
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
So, lets say I have the following command:
curl -I http://google.com | head -n 1| cut -d $' ' -f2
This will capture the http status code??
Now I want to assign this to variable.. in bash script
like output = "curl -I http://localhost:8088/tracks?key=9 | head -n 1| cut -d $' ' -f2"
or something like that..
How do I assign the response of above command to a variable called output in bash?
Thanks
You have two options (see this StackOverflow answer here):
Preferred: Surround the invocation in $()
Surround the invocation in back ticks
NOTE: back ticks are legacy, the former method is preferred.
output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";
output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";
My bash script contains the following line:
echo $(echo "$STRING_VAR" | cut -d' ' -f 2) >> $FILE
Here we have two echo calls, but are they really necessary ?
I wrote them, because otherwise the bash would think the string in first place is a command.
Simply echo "$STRING_VAR" | cut -d' ' -f 2 >> $FILE does the same thing.
echo "$STRING_VAR" | cut -d' ' -f 2 >> $FILE
should be all you need
Also, bash has the handy "here-string" redirection mode: you don't need echo at all:
cut -d' ' -f2 <<< "$STRING_VAR" >> "$FILE"