d0neall
d0neall

Reputation: 81

Delete character/insert character in a string

I need to delete or replace the third ":" (colon) with a space. I can't do it at a certain index because the entries differ in length.

u:Testuser:rw:/home/user1/temp
g:Testgroup:-:/home/user2/temp

Result should look like this:

u:Testuser:rw /home/user1/temp
g:Testgroup:- /home/user2/temp

Is there a way to 1) delete a specific character and 2) to insert a character before/after a specific character? I couldn´t find a solution, I am a beginner unfortunately.

Upvotes: 1

Views: 131

Answers (3)

mickp
mickp

Reputation: 1819

Using parameter expansion:

$ foo='u:Testuser:rw:/home/user1/temp'
$ printf '%s\n' "${foo%":${foo#*:*:*:}"} ${foo#*:*:*:}"
u:Testuser:rw /home/user1/temp

Upvotes: 0

d0neall
d0neall

Reputation: 81

Thanks for the answer, I did it myself

g:Testgroup:-:/home/user2/temp | sed s/':'/' '/3

Upvotes: 2

Geno Chen
Geno Chen

Reputation: 5214

A dirty solution:

$ cat 54042857.txt
u:Testuser:rw:/home/user1/temp
g:Testgroup:-:/home/user2/temp

$ awk -F ':' ' { print $1":"$2":"$3" "$4 } ' 54042857.txt 
u:Testuser:rw /home/user1/temp
g:Testgroup:- /home/user2/temp

Upvotes: 0

Related Questions