Explode a string into two parts using Bash [duplicate] - bash

This question already has answers here:
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 1 year ago.
I'm building a small script to automate something I do pretty regularly.
The script takes in a single parameter, for example: vendor/package. I'd like to take that and split/explode it into two separate strings: vendor and package.
I've looked at solutions on here and various other places but can't get anything to work. I'm sure it's really simple but I'm not great at bash so some help would be greatly appreciated. Thanks!

You can use cut to achieve this
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$ echo "value/package" | cut -d"/" -f1
value
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$ echo "value/package" | cut -d"/" -f2
package
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$
in terms of its implementation in a script -
part1=`echo $1 | cut -d"/" -f1`
part2=`echo $1 | cut -d"/" -f2`

If using bash:
#!/bin/bash
echo ${1#*/}
echo ${1%/*}
Test:
$ bash foo.bash vendor/package
package
vendor

Related

How do I parse a cmd line variable into 2-parts?

This is my first post here. Absolute newbie here - plz be gentle :)
(%2=mydomain.ddns.net and mydomain.com into $SUB_DOMAIN . DOMAIN
./test.sh <name> <dydomain.com>
./test.sh <name> <mysub.domain.com>
would both produce the same result.
var1=<name>
var2=<subdomain> # if present
var3=<domain>
url=<$var1.$var2.$var3>
Regardless of the existance of a subdomain or not, I need $url to be complete with
<name>.(subdomain).<domain>
UPDATE: Thanks everyone for prompt responses, I am currently reading through the answers provided and testing. This script will be run remotely on a VPS HOST so I would need the script to fail and exit if there are unbound variables. I could not prompt for user input.
Thank you.
boo="mydomain.ddns.net"
using '-d .' with 'cut' , given string divided in multiple fields with '.' as delimiter.
'-f1' with cut displays only field 1 and '-f2,3' displays 2 and 3 fields.
echo $boo | cut -f1 -d.
mydomain
echo $boo | cut -f2,3 -d.
ddns.net
var1=$(echo $boo | cut -f1 -d.)
var2=$(echo $boo | cut -f2,3 -d.)
echo $var1
mydomain
echo $var2
ddns.net

How can I set a variable to be the output of cat command? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I have a file named "myout" in my current directory.
I wish to set the variable x to be the output of that line:
cat myout | cut -d" " -f1 | cut -d"/" -f1
I searched all over the site and couldn't find an answer,
any help would be appreciated!
x=$(cut -d" " -f1 ./myout | cut -d "/" -f1)
Updated as per Cyrus's comment.

How to write a shell script that reads all the file names in the directory and finds a particular string in file names?

I need a shell script to find a string in file like the following one:
FileName_1.00_r0102.tar.gz
And then pick the highest value from multiple occurrences.
I am interested in "1.00" part of the file name.
I am able to get this part separately in the UNIX shell using the commands:
find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
1
2
3
1
find /directory/*.tar.gz | cut -f2 -d'_' | cut -f2 -d'.'
00
02
05
00
The problem is there are multiple files with this string:
FileName_1.01_r0102.tar.gz
FileName_2.02_r0102.tar.gz
FileName_3.05_r0102.tar.gz
FileName_1.00_r0102.tar.gz
I need to pick the file with FileName_("highest value")_r0102.tar.gz
But since I am new to shell scripting I am not able to figure out how to handle these multiple instances in script.
The script which I came up with just for the integer part is as follows:
#!/bin/bash
for file in /directory/*
file_version = find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
done
OUTPUT: file_version:command not found
Kindly help.
Thanks!
If you just want the latest version number:
cd /path/to/files
printf '%s\n' *r0102.tar.gz | cut -d_ -f2 | sort -n -t. -k1,2 |tail -n1
If you want the file name:
cd /path/to/files
lastest=$(printf '%s\n' *r0102.tar.gz | cut -d_ -f2 | sort -n -t. -k1,2 |tail -n1)
printf '%s\n' *${lastest}_r0102.tar.gz
You could try the following which finds all the matching files, sorts the filenames, takes the last in that list, and then extracts the version from the filename.
#!/bin/bash
file_version=$(find ./directory -name "FileName*r0102.tar.gz" | sort | tail -n1 | sed -r 's/.*_(.+)_.*/\1/g')
echo ${file_version}
I have tried and thats worth working below script line, that You need.
echo `ls ./*.tar.gz | sort | sed -n /[0-9]\.[0-9][0-9]/p|tail -n 1`;
It's unnecessary to parse the filename's version number prior to finding the actual filename. Use GNU ls's -v (natural sort of (version) numbers within text) option:
ls -v FileName_[0-9.]*_r0102.tar.gz | tail -1

using cut on a line having multiple instances of the same delimiter - unix

I am trying to write a generic script which can have different file name inputs.
This is just a small part of my bash script.
for example, lets say folder 444-55 has 2 files
qq.filter.vcf
ee.filter.vcf
I want my output to be -
qq
ee
I tried this and it worked -
ls /data2/delivery/Stack_overflow/1111_2222_3333_23/secondary/444-55/*.filter.vcf | sort | cut -f1 -d "." | xargs -n 1 basename
But lets say I have a folder like this -
/data2/delivery/Stack_overflow/de.1111_2222_3333_23/secondary/444-55/*.filter.vcf
My script's output would then be
de
de
How can I make it generic?
Thank you so much for your help.
Something like this in a script will "cut" it:
for i in /data2/delivery/Stack_overflow/1111_2222_3333_23/secondary/444-55/*.filter.vcf
do
basename "$i" | cut -f1 -d.
done | sort
advantages:
it does not parse the output of ls, which is frowned upon
it cuts after having applied the basename treatment, and the cut ignores the full path.
it also sorts last so it's guaranteed to be sorted according to the prefix
Just move the basename call earlier in the pipeline:
printf "%s\n" /data2/delivery/Stack_overflow/1111_2222_3333_23/secondary/444-55/*.filter.vcf |
xargs -n 1 basename |
sort |
cut -f1 -d.

Given a string I want to retrieve sections of the string based on delimiters using a cut command?

I have the following string:
MrRelatedTests/ubsr064412_01.tst,GREEN,verified skipped,lwks08,31s,1
I want to retrieve it into the following three subsections:
MrRelatedTests/ubsr064412_01.tst
GREEN,verified skipped
lwks08,31s,1
I have to use this stuff in a shell script. Thus if
$string=MrRelatedTests/ubsr064412_01.tst,GREEN,verified skipped,lwks08,31s,1 I should be able to retrieve the following
$a=MrRelatedTests/ubsr064412_01.tst
$b=GREEN,verified skipped
$c=lwks08,31s,1
Thanks for the help in advance.
a=$(echo ${STR} | cut -d, -f1)
b=$(echo ${STR} | cut -d, -f2-3)
c=$(echo ${STR} | cut -d, -f4-6)

Resources