Extract directory path and filename - shell

I have a variable which has the directory path, along with the file name. I want to extract the filename alone from the Unix directory path and store it in a variable.
fspec="/exp/home1/abc.txt"

Use the basename command to extract the filename from the path:
[/tmp]$ export fspec=/exp/home1/abc.txt
[/tmp]$ fname=`basename $fspec`
[/tmp]$ echo $fname
abc.txt

bash to get file name
fspec="/exp/home1/abc.txt"
filename="${fspec##*/}" # get filename
dirname="${fspec%/*}" # get directory/path name
other ways
awk
$ echo $fspec | awk -F"/" '{print $NF}'
abc.txt
sed
$ echo $fspec | sed 's/.*\///'
abc.txt
using IFS
$ IFS="/"
$ set -- $fspec
$ eval echo \${${##}}
abc.txt

You can simply do:
base=$(basename "$fspec")

dirname "/usr/home/theconjuring/music/song.mp3"
will yield
/usr/home/theconjuring/music.

bash:
fspec="/exp/home1/abc.txt"
fname="${fspec##*/}"

echo $fspec | tr "/" "\n"|tail -1

Using bash "here string":
$ fspec="/exp/home1/abc.txt"
$ tr "/" "\n" <<< $fspec | tail -1
abc.txt
$ filename=$(tr "/" "\n" <<< $fspec | tail -1)
$ echo $filename
abc.txt
The benefit of the "here string" is that it avoids the need/overhead of running an echo command. In other words, the "here string" is internal to the shell. That is:
$ tr <<< $fspec
as opposed to:
$ echo $fspec | tr

Related

Echo command containing both double and single quotes

I have this command
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
and at some point, I want to echo it into a file but a naive approach echo command_above doesn't work.
If I put the command into single quotes, then $3 expands to whitespace.
Is it possible to print that command char-by-char as it is after echo command without any expansion and substitution?
The common approach is to use the << operator to read until some delimiter:
# "cat" just prints what it reads
cat << 'EOF' > output_file
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
EOF
Use xargs to pass file names list to cp
ldd MyApp.out | awk '$3!=""{print $3}' | xargs -d'\n' -I{} cp {} lib/
For debugging and logging purposes you can use set -x or set -v:
set -v # dump commands below
cp $(ldd MyApp.out | awk '{print $3}' | sed -E '/^$/d') lib/
set +v # stop dumping

Cannot assign output of sed to a variable

I refered this
https://unix.stackexchange.com/questions/251388/prefix-and-suffix-strings-to-each-output-line-from-command
to add prefix to output of ls.
I want to store this output of ls:
file1
file2
file3
in to a variable with value:
/../file1 /../file2 /../file3
This is my .sh file:
PREFIX="/../"
OUTPUT_INLINE=$(ls | tr "\n" " ")
OUTPUT="${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
echo "${OUTPUT_INLINE}"
echo "${OUTPUT}"
Output is:
file1 file2 file3
It means variable OUTPUT contains nothing.
Even if I do:
echo "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
I will get:
/../file1 /../file2 /../file3
What is wrong here ?
You are assigning OUTPUT variable this command
"${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
Which means nothing.
Do as you are already doing with OUTPUT_INLINE variable to assign the output of command.
OUTPUT=$(echo -n "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g")
OUTPUT="${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g"
That pipes OUTPUT="${OUTPUT_INLINE}" into sed "s|\<|$PREFIX|g", which doesn’t do anything. I think you meant:
OUTPUT=$(printf '%s' "${OUTPUT_INLINE}" | sed "s|\<|$PREFIX|g")
but there’s lots of fragility here around different delimiter types, and you should be able to avoid all that:
PREFIX="/../"
for filename in *; do
printf '%s%s ' "$PREFIX" "$filename"
done

Sed replace substring only if expression exist

In a bash script, I am trying to remove the directory name in filenames :
documents/file.txt
direc/file5.txt
file2.txt
file3.txt
So I try to first see if there is a "/" and if yes delete everything before :
for i in **/*.scss *.scss; do
echo "$i" | sed -n '^/.*\// s/^.*\///p'
done
But it doesn't work for files in the current directory, it gives me a blank string.
I get :
file.txt
file5.txt
When you only want the filename, use basename instead of sed.
# basename /path/to/file
returns file
here is the man page
Your sed attempt is basically fine, but you should print regardless of whether you performed a substitution; take out the -n and the p at the end. (Also there was an unrelated syntax error.)
Also, don't needlessly loop over all files.
printf '%s\n' **/*.scss *.scss |
sed -n 's%^.*/%%p'
This also can be done with awk bash util.
Example:
echo "1/2/i.py" | awk 'BEGIN {FS="/"} {print $NF}'
output: i.py
Eventually, I did :
for i in **/*.scss *.scss; do
# for i in *.scss; do
# for i in _hm-globals.scss; do
name=${i##*/} # remove dir name
name=${name%.scss} # remove extension
name=`echo "$name" | sed -n "s/^_hm-//p"` # remove _hm-
if [[ $name = *"."* ]]; then
name=`echo "$name" | sed -n 's/\./-/p'` #replace . to --
fi
echo "$name" >&2
done

bash: Grab fields 5 and 7 from a Unix path?

Given paths like this:
/data/mirrors/third-party/centos/5/projectA/x86_64
/data/mirrors/third-party/centos/5/projectA/i386
/data/mirrors/third-party/centos/5/projectA/noarch
/data/mirrors/third-party/centos/4/projectB/x86_64
/data/mirrors/third-party/centos/4/projectB/i386
/data/mirrors/third-party/centos/4/projectB/noarch
/data/mirrors/third-party/centos/4/projectC/x86_64
/data/mirrors/third-party/centos/4/projectC/i386
/data/mirrors/third-party/centos/4/projectC/noarch
How can I grab the values from field 5 and 7 ('5' and 'x86_64') using Bash shell commands?
I have something like this so far, but I'm looking for something more elegant, and without the need to capture the 'junk*':
cd /data/mirrors/third-party/centos/5/project/x86_64
echo `pwd` | tr '/' ' ' | while read junk1 junk2 junk3 junk4 version junk5 arch; do
echo version=$version arch=$arch
done
version=5 arch=x86_64
This works for me:
pwd | awk -F'/' '{print "version=" $6 " arch=" $8}'
You can use IFS and an array to split the directory into its components:
#!/bin/bash
saveIFS=$IFS
IFS='/'
dirs=($(pwd))
IFS=$saveIFS
version=${dirs[5]}
arch=${dirs[7]}
> p=$(pwd)
> echo $p
/data/mirrors/third-party/centos/5/projectA/x86_64
> basename ${p}
x86_64
> basename ${p%/*/*}
5
You can also use something like:
echo `expr match "$p" '<regular-expression>'`
...perhaps someone might help me with that regular expression ;)
try this
echo `pwd` | cut -d'/' -f6,8 | tr '/' ' '
to display field
or to display with sring version and arch
echo `pwd` | cut -d'/' -f6,8 | sed -e 's/\(.*\)\/\(.*\)/version=\1 arch=\2/'

Redirect output to a bash array

I have a file containing the string
ipAddress=10.78.90.137;10.78.90.149
I'd like to place these two IP addresses in a bash array. To achieve that I tried the following:
n=$(grep -i ipaddress /opt/ipfile | cut -d'=' -f2 | tr ';' ' ')
This results in extracting the values alright but for some reason the size of the array is returned as 1 and I notice that both the values are identified as the first element in the array. That is
echo ${n[0]}
returns
10.78.90.137 10.78.90.149
How do I fix this?
Thanks for the help!
do you really need an array
bash
$ ipAddress="10.78.90.137;10.78.90.149"
$ IFS=";"
$ set -- $ipAddress
$ echo $1
10.78.90.137
$ echo $2
10.78.90.149
$ unset IFS
$ echo $# #this is "array"
if you want to put into array
$ a=( $# )
$ echo ${a[0]}
10.78.90.137
$ echo ${a[1]}
10.78.90.149
#OP, regarding your method: set your IFS to a space
$ IFS=" "
$ n=( $(grep -i ipaddress file | cut -d'=' -f2 | tr ';' ' ' | sed 's/"//g' ) )
$ echo ${n[1]}
10.78.90.149
$ echo ${n[0]}
10.78.90.137
$ unset IFS
Also, there is no need to use so many tools. you can just use awk, or simply the bash shell
#!/bin/bash
declare -a arr
while IFS="=" read -r caption addresses
do
case "$caption" in
ipAddress*)
addresses=${addresses//[\"]/}
arr=( ${arr[#]} ${addresses//;/ } )
esac
done < "file"
echo ${arr[#]}
output
$ more file
foo
bar
ipAddress="10.78.91.138;10.78.90.150;10.77.1.101"
foo1
ipAddress="10.78.90.137;10.78.90.149"
bar1
$./shell.sh
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
gawk
$ n=( $(gawk -F"=" '/ipAddress/{gsub(/\"/,"",$2);gsub(/;/," ",$2) ;printf $2" "}' file) )
$ echo ${n[#]}
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
This one works:
n=(`grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '`)
EDIT: (improved, nestable version as per Dennis)
n=($(grep -i ipaddress filename | cut -d"=" -f2 | tr ';' ' '))
A variation on a theme:
$ line=$(grep -i ipaddress /opt/ipfile)
$ saveIFS="$IFS" # always save it and put it back to be safe
$ IFS="=;"
$ n=($line)
$ IFS="$saveIFS"
$ echo ${n[0]}
ipAddress
$ echo ${n[1]}
10.78.90.137
$ echo ${n[2]}
10.78.90.149
If the file has no other contents, you may not need the grep and you could read in the whole file.
$ saveIFS="$IFS"
$ IFS="=;"
$ n=$(</opt/ipfile)
$ IFS="$saveIFS"
A Perl solution:
n=($(perl -ne 's/ipAddress=(.*);/$1 / && print' filename))
which tests for and removes the unwanted characters in one operation.
You can do this by using IFS in bash.
First read the first line from file.
Seoncd convert that to an array with = as delimeter.
Third convert the value to an array with ; as delimeter.
Thats it !!!
#!/bin/bash
IFS='\n' read -r lstr < "a.txt"
IFS='=' read -r -a lstr_arr <<< $lstr
IFS=';' read -r -a ip_arr <<< ${lstr_arr[1]}
echo ${ip_arr[0]}
echo ${ip_arr[1]}

Resources