Delete parts of a Text

5

Galera is the following I wanted to be able to extract only the user name in a Shell SCRIPT however it has to be mandatory through the text that I inform.

I wanted to exactly display on screen only: meucliente From the text: / home / meucliente / publlic_html

This is for a custom backup script that I am putting together, basically I want to always delete the / home / text and the / public_html text from any text I am reporting, however I am getting caught up.

    
asked by anonymous 13.09.2017 / 15:40

3 answers

2

String Manipulation (Bash):

#!/bin/bash
txt='/home/meucliente/public_html'
aux=${txt%/*}
aux=${aux##*/}
echo $aux

Regular Expressions (Bash):

#!/bin/bash
txt='/home/meucliente/public_html'
[[ $txt =~ /([^/]+)/[^/]*$ ]]
echo "${BASH_REMATCH[1]}"

Using awk :

#!/bin/bash
txt='/home/meucliente/public_html'
awk -F "/" '{ print $3 }' <<< $txt

Using sed :

#!/bin/bash
txt='/home/meucliente/public_html'
sed 's,/[^/]\+/\([^/]\+\)/.*,,' <<< $txt

Using cut :

#!/bin/bash
txt='/home/meucliente/public_html'
cut -d/ -f3 <<< $txt

Bash String Split Array:

#!/bin/bash
txt='/home/meucliente/public_html'
IFS='/' arr=( $txt )
echo "${arr[2]}"
    
20.09.2017 / 16:16
1

You can do this:

#!/bin/bash
caminho='/home/meucliente/publlic_html'

from="/home/"
replace=""
caminho=${caminho//$from/$replace}

from="/publlic_html"
replace=""
caminho=${caminho//$from/$replace}

echo $caminho
    
13.09.2017 / 15:58
1

You can do this with cut :

echo "/home/meucliente/public_html" | cut -d'/' -f3
    
14.09.2017 / 10:58