How to use grep to print specific words only - shell

I have a variable that contains a string-
$CCSR = "branches/features/arm_and_musl"
I want to pass only the "arm_and_musl" part to a variable, while excluding "branches/features", so something like this-
def dirname= sh " echo $CCSR | grep ????? "
But the main issue is that I don't want to explicitly mention "arm_and_musl" part, I want it to just exclude "branches/features" and print the remaining part whatever that may be.
I'm not sure what to put here so that only the part I want is passed to the variable.
Could you please suggest any solutions for this?

You could use something like this:
echo ${CCSR##*/}
to just output the needed section or if you need the variable to contain it use:
CCSR=${CCSR##*/}
Edit:
Since for some reason author was getting an error, thought i would include a solution with sed:
echo $CCSR | sed "s:.*/::"

Just use basename:
#!/bin/sh
CCSR="branches/features/arm_and_musl"
file=$(basename "$CCSR")
echo "$file"

Related

How to retain numbers in string in shell?

I'm trying below
a = device.1.2
echo $a should print 1.2
Tried with sed 's/[a-z]//g' a
First thing first please make sure your shell variable doesn't have space while assigning value to it, so have it like this: a="device.1.2". With your shown samples, could you please try following once.
Have it with parameter substitution way: Where we need not to use an external program to get the value.
echo "${a#*.}"
OR with sed: Since OP was trying sed so adding one sed solution here, this nice command was given by Benjamin see comments for same.
echo "$a" | sed 's/^[^.]*\.//'

Combine two inputs and a string in bash

I'm trying to make a bash script to add a description line to a .htaccess file.
Specifically, I want to take two inputs where one is the description and the other is the file path. All of this has to go after AddDescription. I need this to output one string that I can then add to a file.
All together, it should come out to something like this:
AddDescription description path
How can I do it?
with echo you can achieve this
echo AddEscription $var $PATH
if you want to write a script for this, there is no need to specify number of arguments. Create an executable file with these contents and run with as many arguments as you want
#!/bin/bash
echo AddEscription "$#"
Try
RESULT=$(paste <(echo "AddDescription") <(echo "$DESCRIPTION_VAR") <(echo "$PATH_VAR") -d ' ')
and then
echo $RESULT
assuming that you assigned your first input to $DESCRIPTION_VAR and your second input to $PATH_VAR.

Sed append to end of line directory

I'm very new to SED and I'm having a hard time trying to append to an end of a directory. What I'm doing involves 2 basic things with sed but for some reason, no changes are made after the script runs. I will show segments of my script
I have a bash script that pulls my home directory from the host and I define the ID variable.
USERNAME="test"
#pull the home directory
dir=$(ssh -n -t $SERVERNAME "echo \$HOME";)
the above example will store /export/home/ID in the dir variable
echo $dir | sed 's/\([/export/home]*\).*/\1/' > olddir
the sed command above stores /export/home/ in the file olddir (takes off the ending)
sed -i 's_/home/$ _\$USERNAME_' olddir
i am now trying to change /export/home/ to /export/home/test using the defined variable with the escaped $.
after the script runs, it still has /export/home/ as the entry in the olddir file.
I'm using the -i to modify the file and I think I'm using the deliminators correctly? what could I be doing wrong? i even took off the $ from the USERNAME variable which didn't do anything. I know I'm missing something small but i just can't figure it out. I really appreciate your time to answer my question.
I think your command line can be modified then redirect output to olddir as follows:
echo $dir | sed 's#\(/export/home\).*#\1#' > olddir
to add the USERNAME VARIABLE
sed -i "s#/export/home#&/$USERNAME#" olddir
After using the provided information and playing around with the code, I found a solution that will work for both /export/home/ID and /home/ID situations.
I used both Xorg and also gniourf_gniourf's suggestions and below is my result.
echo $dir | sed 's_\([/export/home\]*\).*_\1_' > olddir
The above code is the first part of my solution.
I used what Xorg provided for the above code but it looks like i need those [] so i can use the *\ if i have it as echo $dir | sed 's_\(/export/home\).*_\1_' > olddir then the olddir file will contain /export/home/ID/test after the following sed command is used.
Here is the following sed command that I used:
sed -i 's_/home_&/'"$USERNAME"'_' olddir
The above code is the second part of my solution. I figured that I really only need to put focus on /home/ since i'm appending after it. I looked at gniourf_gniourf's comment and used the example where the quotes to isolate $USERNAME with "" that seems to be the way you can tell the script to use $USERNAME as a variable and not just put in the characters $USERNAME.
so after the script is run, depending on the host, I either have export/home/test/ or /home/test/ I can now put either of these into a new variable on the script to use to specify the home directory when creating ID's on remote hosts!
newdir="$(cat olddir)"
Thank you all so much for your help. I wouldn't have been able to figure this out with out your help.
Update: it turns out that the traling / at the end of directory is problem so i found a much easier way to replace the ID in the home directory
newdir="${dir/ID/${USERNAME}}"
i used these if statments for both home directory situations
`if [[ $dir == "/home/dhabinsk" ]]; then
newdir="${dir/dhabinsk/${USERNAME}}"
fi
if [[ $dir == "/export/home/dhabinsk" ]]; then
newdir="${dir/dhabinsk/${USERNAME}}"
fi`
cheers

Bash insert a variable into a backticked command

I need to use this command
/usr/local/bin/mcl find -f .bz2
which returns me this
:???????? '/Cloud Drive/test1.bz2'
:???????? '/Cloud Drive/test2.bz2'
into a BASH script. The problem is that I need the last parameter (.bz2) to be a variable.
I've tried with this
FILENAME=".bz2"
UPLOADED=$(/usr/local/bin/mcl find -f $FILENAME)
# do something with $UPLOADED
But obviously it is not working. After some research on StackOverflow and on the web I have found several ways to do something like that (even using backticks), but still I can't manage to make it work.
What is the correct way to do that?
You mean like this?
uploaded=$(mcl find -f "$FILENAME" | cut -d"'" -f2)
for u in $uploaded; do
echo "$u"
# process "$u"
done
You can try save the following as e.g. ./script.sh
filename="${1:-.bz2}" #<-- your variable as 1st argument, defaults to .bz2
do_my_work() {
local uploaded="$1"
#do whatever you want with the "uploaded"
printf "got:==%s==\n" "$uploaded"
}
while IFS= read -r __mc1path
do
do_my_work "$__mc1path"
done < <(mc1 find -f "$filename" | sed "s/.*'\(.*\)'.*/\1/")
# variable----^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^- keep only stuff inside of quotes
and use it as
./script.sh .bz2 #or anything, defaults to ".bz2"
and will print
got:==/Cloud Drive/test1.bz2==
got:==/Cloud Drive/test2.bz2==
I think you want that :
UPLOADED=`/usr/local/bin/mcl find -f $FILENAME`

Search and replace with sed by both in pattern and line

I went through lots of similar posts but none could be applied to mine.
I would like to search and replace using sed in some particular lines in a way that in only matches the first occurrence; lets say I have this part of the script:
processor <- read.table("../mall_all/adpcm/FULL_DB-constprop", header=TRUE, colClasses=c("reassociate"="factor", "scalarrepl"="factor", "inline"="factor", "sccp"="factor", "loop_reduce"="factor"))
processor<-processor[-c(20:40)]
processor$intensity <- processor$int_high - processor$int_low
processor$performance<- processor$perf_high - processor$perf_low
processor<-processor[-c(1:4)]
processor<-processor[,!names(processor) %in% c("constprop")]
I want to keep changing the $constprop variable in
"../mall_all/adpcm/FULL_DB-constprop"
AND
[,!names(processor) %in% c("constprop")]
in a loop that I wrote, the problem is; I want the colClasses parameteres AND the rest of the scripts remains the same while entering the loop (the loop has the compiler options like: reassociate, inline, constprop, etc)
I was wondering why my search and replace didn't work :
set -x
compilerOptionList="constprop dce inline instcombine licm loop_reduce loop_rotate loop_unroll loop_unswitch loop_unswitch mem2reg memcpyopt reassociate scalarrepl sccp simplifycfg "
stringToBeReplaced=constprop
for compilerOption in $compilerOptionList
do
echo "Using compiler option: $compilerOption"
//here you could see the sed scripts
sed -i "1,15 /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/" r.scr
stringToBeReplaced=$compilerOption
make
mv Rplots.pdf Rplots_adpcm_$compilerOption.pdf
echo "DONE! $compilerOption"
done
Thanks all for your time and help ;)
Amir
I'm not sure having rightly understood your need, but maybe someting like
sed -e "
1,15ba;
/FULL_DB/,/header/ba;
bb;
:a;
s/stringToBeReplaced/$compilerOption/;
:b;
" -i r.scr
could do the job.
This line is problematic
sed -i "1,15 /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/" r.scr
It's not a valid sed command syntax. You'll need to enclose part of it in braces like this
sed -i "1,15 { /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/ }" r.scr
But I think a tidier way is to use separate files for input and output of sed, i.e. change that line to
sed "1,15 s/constprop/$compilerOption/" r.scr_tmp >r.scr
You don't need the stringToBeReplaced variable. This way you always substitute "constprop", and don't have to worry that the string to be replaced appears elsewhere in the code.
r.scr_tmp would contain the same code as r.scr except that the constprop part of r.scr_tmpremains unchanged.

Resources