Issue
I have a string like this: file-name-example-data
.
I want to remove only the first dash (-
) found in the string.
Basically I want this output: filename-example-data
.
What bash command do you propose to use? Could any sed or awk command work?
The string can be have any length and any amount of dash.
Solution
I would use sub
function of GNU AWK following way, let file.txt
content be
file-name-example-data
then
awk '{sub("-","");print}' file.txt
output
filename-example-data
Explanation: sub
function replaces leftmost occurence of -
with empty string, i.e. remove it, as I used 2 argument form it work on whole line. print
alone does print whole line as changed. If you want to know more about sub
read String Functions manual.
(tested in gawk 4.2.1)
Answered By - Daweo