In my company style guide it says that bash scripts cannot be longer than 80 lines. So I have this gigantic sed substitution over twice as long. How can I break it into more lines so that it still works? I have
sed -i s/AAAAA...AAA/BBBBB...BBB/g
And I want something like
sed -i s/AAAAA...AAA/
BBBBB...BBB/g
still having the same effect.
Linux Files, Users, and Shell Customization with Bash If you want to break up a command so that it fits on more than one line, use a backslash (\) as the last character on the line. Bash will print the continuation prompt, usually a >, to indicate that this is a continuation of the previous line.
In Windows and DOS, the line break code is two characters: a carriage return followed by a line feed (CR/LF). In the Unix/Linux/Mac world, the code is just the line feed character (LF).
In Bash scripting, a break statement helps provide control inside loop statements. Instead of waiting until the end condition, a break statement helps exit from a loop before the end condition happens.
Moving to Start or End of LinePress ^ to move the cursor to the start of the current line. Press $ to move the cursor to the end of the current line.
1) Put your sed script into a file
sed -f script [file ...]
2) Use Regex shorthand
sed 's!A\{30,\}!BBBBB...BBBB!g'
3) Use Bash variables to help break it up a bit
regex="AAAA.AAAAAA"
replace="BBBB...BBBBBBB"
sed "s/${regex}/${replace}/g"
1) Escape the newline to break it up into multiple lines.
You will end up with a newline in your sed script that you don't want.
sed 's/THIS IS WRONG /\
AND WILL BREAK YOUR SCRIPT/g'
Use the shell continuation character, which is normally \
.
[~]$ foo \
> and \
> bar
Space is not required:
[~]$ foo\
> and\
> bar\
> zoo\
> no space\
> whee!\
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With