0

I have many files with wrong opened { brackets from end of line and need to replace it to new line by conditon [a-z)] [{]|[a-z)][{].

input example:

public class Test {

}

expected output:

public class Test 
{

}

What is wrong in my solution?

sed -e '/[a-z)] [{]|[a-z)][{]/s/{/N{/g' *.java -r 
  • Can you provide samples of your input and your expected output? Best regards. – maulinglawns Nov 30 '16 at 12:39
1

Since you are doing this on a Mac, this sed will do it: sed 's/{$/\'$'\n{/'

Example:

echo "public class Test {

}" | sed 's/{$/\'$'\n{/'
public class Test 
{

}

If you want to edit the file "in place" execute this command:

sed -i "" 's/{$/\'$'\n{/' java

Result:

cat java 
public class Test 
{

}

Here is a reference on inserting newlines in sed on Mac.


Tested in OS X 10.11.6

1

You can simply use this command

sed -e s/{/\\n{/g *.java

it will replace any { by a \n{ and show the output in the screen. To replace the text in the files you can use the option in-place

sed -i s/{/\\n{/g *.java
  • Sirst command put \n as characters without replacing to new line, second show error ' sed: 1: "Test.java": invalid command code T – Artyom Nov 30 '16 at 13:14
  • are you sure you type double \ before n. this command worked on the example you made – Dababi Nov 30 '16 at 13:18
  • ➜ example sed -i '' s/{/\\n{/g *.java ➜ example cat Test.java public class Test n{ } – Artyom Nov 30 '16 at 13:23
  • ok try using double quotes (not single) sed -e "s/{/\\n{/g" *.java – Dababi Nov 30 '16 at 13:29
  • OP is using a Mac, and therefore BSD sed. – maulinglawns Nov 30 '16 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.