I am passing variable $$PSTCMT to a script as an argument:
test.sh $$PSTCMT
in the script the value is show as below:
PSTCMT1=$1
echo PSTCMT1
the output is:
abc,def,fgh
I want to replace above as below
PSTCMT ='abc','def','fgh'
echo $PSTCMT will give below output as
'abc','def','fgh'
If you are in BASH then you can do:
pstcmt1='abc,def,fgh'
pstcmt="'${pstcmt1//,/\',\'}'"
echo "$pstcmt"
'abc','def','fgh'
Without BASH you can use sed:
pstcmt="'$(sed "s/,/','/g" <<< "$pstcmt1")'"
Related
I have two strings saved in a bash variable delimited by :. I want to get extract the second string, prepend that with THIS_VAR= and append it to a file named saved.txt
For example if myVar="abc:pqr", THIS_VAR=pqr should be appended to saved.txt.
This is what I have so far,
myVar="abc:pqr"
echo $myVar | cut -d ':' -f 2 >> saved.txt
How do I prepend THIS_VAR=?
printf 'THIS_VAR=%q\n' "${myVar#*:}"
See Shell Parameter Expansion and run help printf.
The more general solution in addition to #konsolebox's answer is piping into a compound statement, where you can perform arbitrary operations:
echo This is in the middle | {
echo This is first
cat
echo This is last
}
I have a script:
$cat ifile.sh
extension="name.f"
parameter_list="abc,xyz,stuv,ptq,rhu"
echo newfiles are $parameter_list
I would like to execute the ifile.sh and output should replace the comma , with extension i.e. _name.f. So my desire output will be
$sh ifile.sh
newfiles are abc_name.f xyz_name.f stuv_name.f ptq_name.f rhu_name.f
So I need to modify the echo newfiles are $parameter_list. I am trying with the follwoing
$cat ifile.sh
extension="name.f"
parameter_list="abc,xyz,stuv,ptq,rhu"
echo newfiles are sed -i 's/,/$extension/g' $parameter_list
Use a parameter expansion.
echo "newfiles are ${parameter_list//,/_$extension }_$extension"
I have a variable with a bunch of data.
text = "ABCDEFGHIJK"
file = garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf
What I would like to do is replace the ] charachter in file with text. I've tried using sed but I keep getting odd errors.
output should be:
//iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
Just need to escape the ] character with a \ in regex:
text="ABCDEFGHIJK"
sed "s/\(.*\)\]\(.*\)/\1$text\2/" file > file.changed
or, for in-place editing:
sed -i "s/\(.*\)\]\(.*\)/\1$text\2/" file
Test:
sed "s/\(.*\)\]\(.*\)/\1$text\2/" <<< "iiuhdsfiuhdsihf]sdiuhdfoidsoijsf"
# output => iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
There is always the bash way that should work in your osx:
filevar=$(cat file)
echo "${filevar/]/$text}" #to replace first occurence
OR
echo "${filevar//]/$text}" #to replace all occurences
In my bash i don't even have to escape ].
By the way, the simple sed does not work?
$ a="AA"
$ echo "garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf" |sed "s/]/$a/g"
garbage.txt //iiuhdsfiuhdsihfAAsdiuhdfoidsoijsf
So I am trying to output a file with the name of like: lastlogin-"yyyymmdd" where it contains the current date.
I figured out the date should be : date +"%Y%m%d" and I tried to do a variable
now = date +"lastlogin-%Y%m%d.txt"
filename = ${now}
xxxxx > ${filename}
but nothing seems to work
Please help
Use command substitution:
lastlogin-"$(date '+%Y%m%d')".txt
To save in a variable:
filename="lastlogin-"$(date '+%Y%m%d')".txt"
and then do:
echo 'foobar' >"$filename"
You should use $() for command execution and storage of result:
now=$(date +"lastlogin-%Y%m%d.txt")
In Shell script I want to achieve something like below:
str="india,uk,us,uae"
I want to split it and concatenate each item as below and assign to some variable
newstr = '-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"'
so that I can use above concatenated string in my next command as below
curl "admin/admin" "localhost" $newstr.
I found a way using local IFS and for loop but the variable updated inside loop is not retaining value outside of loop because it runs in a separate bash.
str="india,uk,us,uae"
var=-myparam=\"${str//,/\" -myparam=\"}\"
echo $var
Read the params into an array:
IFS=, read -a params <<< "$str"
And then loop through them and store the command in an array:
for i in "${params[#]}"; do
command+=(-myparam=\"$i\")
done
Now you can expand it using printf "${command[#]}":
$ printf "%s " "${command[#]}"
-myparam="india" -myparam="uk" -myparam="us" -myparam="uae"
That is, now you have to say:
curl "admin/admin" "localhost" "${command[#]}"
This is based on this answer by chepner: command line arguments fed from an array.
Below code would do :
$ str="india,uk,us,uae"
$ newstr=$(awk 'BEGIN{RS=","}{printf "-myParam=\"%s\" ",$1}' <<<"$str")
$ echo "$newstr"
-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"
Also when you pass new string as parameter to curl, double quote it to prevent word splitting and globbing, so do :
curl "admin/admin" "localhost" "$newstr"
Note: <<< or herestring is only supported in a few shells (Bash, ksh, or zsh) if I recall correctly. If your shell does not support it use echo,pipe combination.
IFS=',' read -ra a <<< "${str//,/\",}";
curl "admin/admin" "localhost" "${a[#]/#/ -myParam=\"}\""
Explanation:
Starting with:
str="india,uk,us,uae";
Next, split the string into an array, using parameter substitution to insert " before each comma:
IFS=',' read -ra a <<< "${str//,/\",}";
Finally, we can get newstr through parameter substitution (while also appending the final "):
newstr="${a[#]/#/ -myParam=\"}\"";
newstr is now set to '-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"'. We can skip the previous step and go straight to:
curl "admin/admin" "localhost" "${a[#]/#/ -myParam=\"}\""