Bash: String manipulation with sed and Regular expression is not working: replace a string by slash

Jochen

I hope you can help me out:

Here is one of my lines that I have to string manipulate:

./period/0.0.1/projectname/path/path/-rw-rw-r--filename.txt 2462

Where the last number is the file size and needed for later calculations.

The sequence -rw-rw-r-- is from a file listing Output where I separated files from directories and skipped all lines starting with "d". Now I need to get rid of the rights sequence in the lines.

Here is my regex, that exactly hits that target: [/][-][-rwx]{9,9} I checked tat with a regex checker and get exact what I want: the string /- including the following 9 characters.

What I want: replace this string " /- including the following 9 characters " by a single slash /. To avoid escaping I use pipe as separator in sed. The following sed command is working correct:

sed 's|teststring|/|g' inputfile > outputfile

The problem:

When I replace "teststring" bei my regex it is not manipulating anything:

sed 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile

I get no errors at all, but have no stringmanipulations in result outputfile.

What am I doing wrong here??

Please help!

Aaron

sed uses the BRE regex flavour by default, where braces should be escaped.

Either escape them :

sed 's|[/][-][-rwx]\{9,9\}|/|g' inputfile > outputfile

Or switch to ERE :

sed -n 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile # for GNU sed
sed -E 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile # for BSD sed & modern GNU sed

As a side note, your regex can be simplified to /-[-rwx]{9} :

sed -E 's|/-[-rwx]{9}|/|g' inputfile > outputfile

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related