I'm trying to figure out how to insert text before and after every line in a text file using sed
on OS X, but something is obviously wrong with my approach.
This is what I have:
sed 's/\(^\).\($\)/A \1\2 Z/g' input
This is what I want:
input:
bbb
ccc
ddd
output:
A bbb Z
A ccc Z
A ddd Z
EDIT: Also, don't match blank lines (or those containing only spaces).
You can use:
sed 's/.*/A & Z/' file
A bbb Z
A ccc Z
A ddd Z
&
is back-reference of complete match in regex pattern.
Or using awk:
awk '{print "A", $0, "Z"}' file
You could use:
sed 's/.*/A \0 Z/' input
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.