Is there a way to split this line on the last _
to create two variables?
The folder in Linux is of the form Project_Data1_xxx
or Project_Data1_Sub1_xxx
. I would like to create two variables :
- for the first two or three octets
- for what comes after the last
_
.
I have spent quite a bit of time on this and apologize as I am new to Linux and I am not yet understanding all the differences between windows and linux.
Question: do I break it up into two statements to get the front and the back?
I am doing ls -1
of a folder into a variable. So I do
#!/bin/bash
srcIn=/scripts/server1
dstIn=/scripts/server2
data=test
sourcein=$(ls $srcIn/ | grep $data)
destinationin=$(ls $dstIn/ | grep $data)
I then get:
echo $destinationin
test_data1_xxx test_data2_xxx test_data3_xxx
echo $sourcein
test_data1_ccc test_data2_ccc test_data3_ccc
In the end. I need to create a copy line that goes:
cp /test_data1_ccc/* /test_data1_xxx
cp /test_data2_ccc/* /test_data2_xxx
and so on... There may be 2 or 3 underscores but the format is always the same.
The strings xxx
and ccc
could be abc123
or ccc567
and do not necessarily have the same number of characters. The one thing that doesn't change is the only difference in the two locations is what follows the last _
- but they will always be different.