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

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)

Related

looping into files having same string in second part of its name

I am using loop instruction to zip many csv file based in their prefix (first element of its name)
printf '%s\n' *_*.csv | cut -d_ -f1 | uniq |
while read -r prefix
do
zip $ZIP_PATH/"$prefix"_"$DATE_EXPORT"_M2.zip "$prefix"_*.csv
done
And it works very well, as an input I have
123_20211124_DONG.csv
123_20211124_FINA.csv
123_20211124_INDEM.csv
123_20211202_FINA.csv
123_20211202_INDEM.csv
and the zip loop will pack all these files because they have the same prefix
Or, I would like to pack only those which has $DATE_EXPORT= 20211202, in other word, I want to pack only those which has second element in file name=20211202 == DATE_EXPORT variable
I tried using grep function like :
printf '%s\n' *_*.csv | grep $DATE_EXPORT | cut -d_ -f1 | uniq |
while read -r prefix
do
zip $ZIP_PATH/"$prefix"_"$DATE_EXPORT"_M2.zip "$prefix"_*.csv
done
But, does not work, any help, please ?
"$prefix"_*.csv in the zip command in your second example is not filtered for "$DATE_EXPORT". Try "${prefix}_${DATE_EXPORT}_"*.csv or similar. You can also use *"_${DATE_EXPORT}_"*.csv with printf, instead of grep.
Also, I'm not sure what's going on with $cut, but obviously cut is the usual name.

How to assign values in bash after running commands

NAME=aaa/bbbbb:0.1.2
How can I assign the new string?
NEW_VAR=$NAME | sed 's/.*\///' | cut -f1 -d":"
Assuming you are using bash, you don't actually need to shell out to other commands, there is built in string mangling functions:
NEW_VAR=${NAME%%:*}
If you would prefer to, it'll be something like this:
NEW_VAR=$(echo -n $NAME | cut -f1 -d":")

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 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

shell script to extract text from a variable separated by forward slashes

I am trying to find a way to to extract text from a variable with words separated by a forward slash. I attempted it using cut, so here's an example:
set variable = '/one/two/three/four'
Say I just want to extract three from this, I used:
cut -d/ -f3 <<<"${variable}"
But this seems to not work. Any ideas of what I'm doing wrong? Or is there a way of using AWK to do this?
You need to remove the spaces before and after to = during string or variable assignment. And tell the cut command to print the 4th field.
$ variable='/one/two/three/four'
$ cut -d/ -f4 <<<"${variable}"
three
With the delimiter /, cut command splits the input like.
/one/two/three/four
| | | | |
1 2 3 4 5
that is, when it splits on first slash , you get an empty string as first column.
I think that the main problem here is in your assignment. Try this:
var='/one/two/three/four'
cut -d/ -f4 <<<"$var"
Here is an awk version:
awk -F\/ '{print $4}' <<< "$variable"
three
or
echo "$variable" | awk -F\/ '{print $4}'
three
PS to set a variable not need for set and remove spaces around =
variable='/one/two/three/four'

Resources