Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need urgent help with sed 2

Status
Not open for further replies.

zaxxon

MIS
Dec 12, 2001
226
DE
Hi,

I got following string:

u=r-x,g=r-x,o=--- "/home/jupp/ping-pong"

I need following output:

u=rx,g=rx,o= "/home/jupp/ping-pong"

I was able to address the string into 2 parts with:

sed 's/^\(.*\) \(\".*\"\)/\1 \2/g'

Now I only need that \1 to be removed of all minus symbols. Tried a lot already, but no success. I need a solution or hint for sed only, thanks in forward!

laters
zaxxon
 
The awk way:
echo 'u=r-x,g=r-x,o=--- "/home/jupp/ping-pong"' | awk '{gsub(/-/,"",$1);print}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

when PHV chooses the awk way, I think I should give up for an sed solution for now. Thanks PHV :)

@hoinz:
For "cut", I am using AIX and I don't know or see any switches in it's man page for making it able to cut a single character out of a field, but just whole fields. Maybe there are any other versions of "cut" with Linux or other Unix'es allowing more extended possibilities. Thanks though.

laters
zaxxon
 
zaxxon,
I think 'cut -c' should be the same for all kinds of unix.
It cuts columns, but not characters.
From your original question:
echo 'u=r-x,g=r-x,o=--- "/home/jupp/ping-pong"' | cut -c1-3,5-
would remove the first -, as long as it is in column 4.
For me, it was not clear from your question, whether the positions of the - could vary. If so, me idea would be useless.
But anyway, thanks to awk and PHV!
 
One possible sed way....

sed 's/\([ugo]=[^-]*\)--*/\1/g'
 
Hi hoinz,

since my string looks like the input for chmod or at least some Unix permissions, they can vary for sure. -c is static, not usable for me, sorry.

laters
zaxxon
 
Ygor, what happens with input like this ?
u=-w-,g=r-x,o=--x "/home/jupp/ping-pong
 
Or perhaps:
Code:
echo 'u=r-x,g=r-x,o=--- "/home/jupp/ping-pong"' | tr -d '-'
Unfortunately this removes the '-' between ping and pong..
 
To prevent the '-' from ping-ping being removed, perhaps you can use:
Code:
echo 'u=r-x,g=r-x,o=--- "/home/jupp/ping-pong"' | perl -p -e 's/([ugo]=[^-]*?)-+/$1/g'
Cheers, Neil
 
I see your point PHV. This is simpler and works better...

sed 's/\([^- ]*\)--*/\1/g'
 
Looks ugly, but should work...

sed -e :a -e 's/^\([^- ]*\)--*/\1/;ta'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top