Changes
Jump to navigation
Jump to search
← Older edit
Newer edit →
Linux command: sed
(view source)
Revision as of 19:16, 19 June 2017
674 bytes added
,
19:16, 19 June 2017
→Replace New Line (\n) with ", "
Line 10:
Line 10:
Example:
Example:
<nowiki>echo "esto es un ejemplo" | sed 's#e#R#g'</nowiki>
<nowiki>echo "esto es un ejemplo" | sed 's#e#R#g'</nowiki>
+
+
== Replace New Line Characters ==
+
sed ':a;N;$!ba;s/\n/ /g'
+
This will read the whole file in a loop, then replaces the newline(s) with a space.<br />
+
Explanation:<br />
+
Create a label via :a.<br />
+
Append the current and next line to the pattern space via N.<br />
+
If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).<br />
+
Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).<br />
+
+
Here is cross-platform compatible syntax which works with BSD sed (as per @Benjie comment):<br />
+
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'
== Replace New Line (\n) with ", " ==
== Replace New Line (\n) with ", " ==
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/", "/g'
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/", "/g'
Rafahsolis
Bureaucrats
,
Administrators
2,306
edits
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
Variants
Views
Read
View source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
Special pages
Printable version