Tuesday, May 7, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2412  / 3 Years ago, sun, may 23, 2021, 5:46:10

I have a string abc_d_e75_f10_xyz_l_11 I want to split it so that I want to construct a string of an existing directory from this string to cd to it.



So in this example abc_d_e75_f10_xyz_l_11 I want to split using the _ character where I want to write this command cd /home/userA/abc/d_e75_f10 and neglect the rest of the string.


More From » command-line

 Answers
0

You can do:



echo "abc_d_e75_f10_xyz_l_11" | sed 's/_///' | cut -d "_" -f1-3



This will replace the first _ with / as well as trim the string.



Output:



abc/d_e75_f10


[#23030] Tuesday, May 25, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ngthmated

Total Points: 12
Total Questions: 115
Total Answers: 113

Location: Saint Vincent and the Grenadines
Member since Wed, Apr 21, 2021
3 Years ago
;