How can I get the content of this string surrounded by single-quote - shell

I have a string like this:
SOMETHING='abc.abc.abc'
How can I extract the content of it ( abc.abc.abc ), inside the single-quotes?

$ awk -F"=\047|\047" '/SOMETHING/{print $(NF-1)}' file
abc.abc.abc

str="SOMETHING='abc.abc.abc'"
substr=$(echo "$str" | cut -d "'" -f 2)
With bash, you could write
substr=$(cut -d "'" -f 2 <<< "$str")
Or, shell only:
IFS="'"
set -- $str
substr=$2
Or use an array
IFS="'"
fields=($str)
substr=${fields[1]}

You should be able to already extract it. Example:
$ cat test.sh
#!/bin/bash
SOMETHING='abc.abc.abc'
echo $SOMETHING
$ ./test.sh
abc.abc.abc

You have tagged this question as shell. So I guess you're able to use regular expressions in grep. Or perhaps you could do something with javascript regular expressions.
this regex would find it
abc\.abc\.abc

Related

Remove and replace characters from string with one command

I need to remove characters from string and then replace other characters.
This is the initial string:
something/filename.txt
I need to remove the directory "something" (can be any other name) and replace .txt with .gz
The following 2 commmands work perfect:
newfile=${newfile#*/}
newfile=${newfile::-4}.gz
So the output will be: filename.gz
Is there a way to do it in a single command? Something like:
${${$newfile#*/}::-4}.gz
With the above command I get: bad substitution error.
Thank you
Lucas
Perhaps you could use basename, i.e.
name_of_file="something/filename.txt"
newfile=$(basename "${name_of_file%%.*}".gz)
echo "$newfile"
filename.gz
Since your question is tagged bash, you can use Bash builtin regex to capture the group you need like this:
#!/usr/bin/env bash
filepath=something/filename.txt
# Regex group capture basename without dot suffix || exit err if not matching
[[ $filepath =~ .*/(.*)\.[^.]* ]] || exit
# Compose new file name from Regex captured group and new .gz extension
newfilename=${BASH_REMATCH[1]}.gz
# debug dump variables
declare -p filepath newfilename
new_file=something/filename.txt
new_file="${new_file#*/}"
new_file="${new_file%.*}.gz"
Is there a way to do it in a single command?
echo something/filename.txt | sed 's|.*/||;s|\..*$|.gz|'
A combination of cut and sed can help as below
oldfile='somethingelse/filename.txt'
newfile=`echo $oldfile | cut -d "/" -f2 |sed 's!.txt!.gz!g'`
echo $newfile
This displays filename.gz
EDIT
In case there are subdirectories and you want only file name
oldfile='somethingelse/other/filename.txt'
newfile=`echo $oldfile | rev| cut -d "/" -f1 |rev |sed 's!.txt!.gz!g'`
echo $newfile
The cut command gets the last field delimited by "/" .
Happy to be corrected and learn.

How to remove special characters from strings but keep underscores in shell script

I have a string that is something like "info_A!__B????????C_*". I wan to remove the special characters from it but keep underscores and letters. I tried with [:word:] (ASCII letters and _) character set, but it says "invalid character set". any idea how to handle this ? Thanks.
text="info_!_????????_*"
if [ -z `echo $text | tr -dc "[:word:]"` ]
......
Using bash parameter expansion:
$ var='info_A!__B????????C_*'
$ echo "${var//[^[:alnum:]_]/}"
info_A__BC_
A sed one-liner would be
sed 's/[^[:alnum:]_]//g' <<< 'info_!????????*'
gives you
info_
An awk one-liner would be
awk '{gsub(/[^[:alnum:]_]/,"",$0)} 1' <<< 'info_!??A_??????*pi9ngo^%$_mingo745'
gives you
info_A_pi9ngo_mingo745
If you don't wish to have numbers in the output then change :alnum: to :alpha:.
My tr doesn't understand [:word:]. I had to do like this:
$ x=$(echo 'info_A!__B????????C_*' | tr -cd '[:alnum:]_')
$ echo $x
info_A__BC_
Not sure if its robust way but it worked for your sample text.
sed one-liner:
echo "SamPlE_#tExT%, really ?" | sed -e 's/[^a-z^A-Z|^_]//g'
SamPlE_tExTreally

How to add [ ] brackets around first character of a word using bash

My question is described here Add square bracket to the first character of string .
Using the above link reference, I am trying from past 4 hours to get the result using bash.
Any workaround using sed.
#!/bin/bash
DESC="openerp-server"
initial="$(echo "$DESC" | sed 's/(.)/'[\1]'/g')"
echo $initial
Thanks.
With sed:
echo "string" | sed 's/^\(.\)/[\1]/'
echo "string" | sed 's/./[\0]/' # The same but simplified
Output:
[s]tring
bash provides an extension to the standard parameter expansion operators that lets you easily access the first character and the remaining characters of a parameter.
$ DESC="openerp-server"
$ DESC="[${DESC:0:1}]${DESC:1}"
$ echo "$DESC"
[o]penerp-server
A POSIX-compatible version is slightly longer, requiring a temporary variable to hold the tail.
$ DESC="openerp-server"
$ DESC_tail=${DESC#?}
$ DESC="[${DESC%$DESC_tail}]$DESC_tail"
$ echo "$DESC"
[o]penerp-server
An awk version:
echo "string" | awk '{$1="["$1"]"}8' FS= OFS=
[s]tring

Use sed to replace all ' with \' and all " with \"

I want to use sed to replace all ' with \' and all " with \". Example input:
"a" 'b'
Output:
\"a\" \'b\'
There's no ? character in your post, but I'll assume your question is "How do I do such a replacement?". I just made a quick test file with your input, and this command line seems to work:
sed -e 's#"#\\"#g' -e "s#'#\\\'#g"
Example:
$ cat input
"a" 'b'
$ sed -e 's#"#\\"#g' -e "s#'#\\\'#g" input
\"a\" \'b\'
While using sed is the portable solution, all this can be done using Bash's builtin string manipulation functions as well.
(
#set -xv
#str=$'"a" \'b\''
str='"a" '"'b'" # concatenate 'str1'"str2"
str="${str//\"/\\\"}"
str="${str//\'/\'}"
echo "$str"
)

Combine path + list of files to list of absolute files

If I have this data in my shell script:
DIR=/opt/app/classes
JARS=a.jar:b.jar:c.jar
How can I combine this to the string
/opt/app/classes/a.jar:/opt/app/classes/b.jar:/opt/app/classes/c.jar
in Shell/Bash scripting?
Here's a very short one:
$ echo "$DIR/${JARS//:/:$DIR/}"
/opt/app/classes/a.jar:/opt/app/classes/b.jar:/opt/app/classes/c.jar
If you don't mind an extra semicolon at the end:
[~]> for a in `echo $JARS | tr ":" "\n"`;do echo -n $DIR/$a:;done&&echo
/opt/app/classes/a.jar:/opt/app/classes/b.jar:/opt/app/classes/c.jar:
Use translate and iterate through the results. Then trim the result ':' character at the beginning of the string.
#! /bin/bash
DIR=/opt/app/classes
JARS=a.jar:b.jar:c.jar
for i in $(echo $JARS | tr ":" "\n")
do
result=$result:$DIR/$i
done
echo ${result#:} // Remove the starting :
Result:
/opt/app/classes/a.jar:/opt/app/classes/b.jar:/opt/app/classes/c.jar
Pure Optimized Bash 1-liner
IFS=:; set -- $JARS; for jar; do path+=$DIR/${jar}:; done; echo "$path"
Output
/opt/app/classes/a.jar:/opt/app/classes/b.jar:/opt/app/classes/c.jar:
Pure Bash, no external utilities:
saveIFS=$IFS
IFS=:
jararr=($JARS)
echo "${jararr[*]/#/$DIR/}"
IFS=saveIFS
Original Answer (before question was revised):
IFS=: read -ra jararr <<<"$JARS"
newarr=(${jararr[#]/#/$DIR/})
echo "${newarr[0]}:${newarr[1]}"

Resources