S
S
Sergey Titov2015-10-03 02:06:40
linux
Sergey Titov, 2015-10-03 02:06:40

How to replace using sed?

How to replace the first character of each line with the first character of the previous line, leaving the first line unchanged? Awk cannot be used, only sed :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2015-10-03
@jcmvbkbc

#! /bin/sed -nf

1{p;h;}
2~1{H;x;s/\n.//;p;}
x
s/^\(.\).*$/\1/
h

Now try to understand how it works.

A
abcd0x00, 2015-10-04
@abcd0x00

sed '
1 { h; s/^\(.\).*$/\1/; x; }
H
s/^\(.\).*$/\1/
x
s/^\(.\)../\1/
'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question