Remove string using certain values (-)

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have following text
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
I want to remove last strings that have two "-".
Expected output:
foo-11.11-fo.o
bar-foo
I have tried multiple methods using cut, but nothing works.
shell-script text-processing sed scripting cut
add a comment |Â
up vote
0
down vote
favorite
I have following text
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
I want to remove last strings that have two "-".
Expected output:
foo-11.11-fo.o
bar-foo
I have tried multiple methods using cut, but nothing works.
shell-script text-processing sed scripting cut
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have following text
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
I want to remove last strings that have two "-".
Expected output:
foo-11.11-fo.o
bar-foo
I have tried multiple methods using cut, but nothing works.
shell-script text-processing sed scripting cut
I have following text
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
I want to remove last strings that have two "-".
Expected output:
foo-11.11-fo.o
bar-foo
I have tried multiple methods using cut, but nothing works.
shell-script text-processing sed scripting cut
shell-script text-processing sed scripting cut
edited Sep 25 '17 at 12:51
Jeff Schaller
32.4k849110
32.4k849110
asked Sep 25 '17 at 11:52
Buvanesh Kumar
106110
106110
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
$ cat ip.txt
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
$ rev ip.txt
rab-oof-o.of-11.11-oof
11.22-11.11-oof-rab
$ rev ip.txt | cut -d- -f3- | rev
foo-11.11-fo.o
bar-foo
Reverse each line, then use cut to select all fields except first two and then reverse the output again
You can also use perl, but would print empty lines if any input line has less than 3 fields
$ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt
foo-11.11-fo.o
bar-foo
Specify - as input delimiter and then print all but last two fields
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
add a comment |Â
up vote
4
down vote
Using sed:
$ sed 's/-[^-]*-[^-]*$//' file
foo-11.11-fo.o
bar-foo
This will remove -X-X at the end of every line in file, where X is any string that does not include a -.
If the strings are in a shell variable:
$ s='foo-11.11-fo.o-foo-bar'
$ printf '%sn' "$s%-*-*"
foo-11.11-fo.o
$ s='bar-foo-11.11-22.11'
$ printf '%sn' "$s%-*-*"
bar-foo
or in a bash array:
$ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
$ printf '%sn' "$s[@]%-*-*"
foo-11.11-fo.o
bar-foo
This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
$ cat ip.txt
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
$ rev ip.txt
rab-oof-o.of-11.11-oof
11.22-11.11-oof-rab
$ rev ip.txt | cut -d- -f3- | rev
foo-11.11-fo.o
bar-foo
Reverse each line, then use cut to select all fields except first two and then reverse the output again
You can also use perl, but would print empty lines if any input line has less than 3 fields
$ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt
foo-11.11-fo.o
bar-foo
Specify - as input delimiter and then print all but last two fields
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
add a comment |Â
up vote
2
down vote
accepted
$ cat ip.txt
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
$ rev ip.txt
rab-oof-o.of-11.11-oof
11.22-11.11-oof-rab
$ rev ip.txt | cut -d- -f3- | rev
foo-11.11-fo.o
bar-foo
Reverse each line, then use cut to select all fields except first two and then reverse the output again
You can also use perl, but would print empty lines if any input line has less than 3 fields
$ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt
foo-11.11-fo.o
bar-foo
Specify - as input delimiter and then print all but last two fields
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
$ cat ip.txt
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
$ rev ip.txt
rab-oof-o.of-11.11-oof
11.22-11.11-oof-rab
$ rev ip.txt | cut -d- -f3- | rev
foo-11.11-fo.o
bar-foo
Reverse each line, then use cut to select all fields except first two and then reverse the output again
You can also use perl, but would print empty lines if any input line has less than 3 fields
$ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt
foo-11.11-fo.o
bar-foo
Specify - as input delimiter and then print all but last two fields
$ cat ip.txt
foo-11.11-fo.o-foo-bar
bar-foo-11.11-22.11
$ rev ip.txt
rab-oof-o.of-11.11-oof
11.22-11.11-oof-rab
$ rev ip.txt | cut -d- -f3- | rev
foo-11.11-fo.o
bar-foo
Reverse each line, then use cut to select all fields except first two and then reverse the output again
You can also use perl, but would print empty lines if any input line has less than 3 fields
$ perl -F'-' -lane 'print join "-", @F[0..$#F-2]' ip.txt
foo-11.11-fo.o
bar-foo
Specify - as input delimiter and then print all but last two fields
answered Sep 25 '17 at 12:00
Sundeep
6,9711826
6,9711826
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
add a comment |Â
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
rev that's what I missed. thanks a lot, working great.
â Buvanesh Kumar
Sep 25 '17 at 12:18
add a comment |Â
up vote
4
down vote
Using sed:
$ sed 's/-[^-]*-[^-]*$//' file
foo-11.11-fo.o
bar-foo
This will remove -X-X at the end of every line in file, where X is any string that does not include a -.
If the strings are in a shell variable:
$ s='foo-11.11-fo.o-foo-bar'
$ printf '%sn' "$s%-*-*"
foo-11.11-fo.o
$ s='bar-foo-11.11-22.11'
$ printf '%sn' "$s%-*-*"
bar-foo
or in a bash array:
$ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
$ printf '%sn' "$s[@]%-*-*"
foo-11.11-fo.o
bar-foo
This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.
add a comment |Â
up vote
4
down vote
Using sed:
$ sed 's/-[^-]*-[^-]*$//' file
foo-11.11-fo.o
bar-foo
This will remove -X-X at the end of every line in file, where X is any string that does not include a -.
If the strings are in a shell variable:
$ s='foo-11.11-fo.o-foo-bar'
$ printf '%sn' "$s%-*-*"
foo-11.11-fo.o
$ s='bar-foo-11.11-22.11'
$ printf '%sn' "$s%-*-*"
bar-foo
or in a bash array:
$ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
$ printf '%sn' "$s[@]%-*-*"
foo-11.11-fo.o
bar-foo
This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Using sed:
$ sed 's/-[^-]*-[^-]*$//' file
foo-11.11-fo.o
bar-foo
This will remove -X-X at the end of every line in file, where X is any string that does not include a -.
If the strings are in a shell variable:
$ s='foo-11.11-fo.o-foo-bar'
$ printf '%sn' "$s%-*-*"
foo-11.11-fo.o
$ s='bar-foo-11.11-22.11'
$ printf '%sn' "$s%-*-*"
bar-foo
or in a bash array:
$ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
$ printf '%sn' "$s[@]%-*-*"
foo-11.11-fo.o
bar-foo
This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.
Using sed:
$ sed 's/-[^-]*-[^-]*$//' file
foo-11.11-fo.o
bar-foo
This will remove -X-X at the end of every line in file, where X is any string that does not include a -.
If the strings are in a shell variable:
$ s='foo-11.11-fo.o-foo-bar'
$ printf '%sn' "$s%-*-*"
foo-11.11-fo.o
$ s='bar-foo-11.11-22.11'
$ printf '%sn' "$s%-*-*"
bar-foo
or in a bash array:
$ s=( 'foo-11.11-fo.o-foo-bar' 'bar-foo-11.11-22.11' )
$ printf '%sn' "$s[@]%-*-*"
foo-11.11-fo.o
bar-foo
This removes anything matching the pattern -*-* at the end of the string in the variable s through a suffix pattern match/removal. In the case where s in an array, the removal is done on all elements of the array.
edited Sep 25 '17 at 12:30
answered Sep 25 '17 at 12:02
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394305%2fremove-string-using-certain-values%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password