2

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!

3 Answers 3

2

You can use this sed with extended regex:

sed -E 's|/-[-rwx]{9}|/|g' file 

./period/0.0.1/projectname/path/path/filename.txt 2462 
  • No need to use [/] and [-] in your regex
  • Use -E for extended regex matching
  • .{9,9} is same as .{9}
Sign up to request clarification or add additional context in comments.

Comments

2

You may use

sed 's|/-[-rwx]\{9\}|/|g' 

Note that in POSIX BRE patterns, in order to specify a limiting quantifier, you need to escape the braces.

See the Bash demo:

s='./period/0.0.1/projectname/path/path/-rw-rw-r--filename.txt 2462' echo $s | sed 's|/-[-rwx]\{9\}|/|g' 

Output:

./period/0.0.1/projectname/path/path/filename.txt 2462 

NOTE: It is not a good idea to wrap each individual char with a bracket expression, [/] = / and [-] = -.

Comments

1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.