开发者

Linux file splitting

开发者 https://www.devze.com 2023-03-26 23:41 出处:网络
I am using sed to split a file in two I have a file that has a custom separator \"/-sep-/\" and I want to split the file where the separator is

I am using sed to split a file in two

I have a file that has a custom separator "/-sep-/" and I want to split the file where the separator is

currently I have:

sed -n '1,/-sep-/ {p}' /export/data.temp > /export/data.sql.md5

sed -n '/-sep-/,$ {p}' /export/data.temp > /export/data.sql

but the file 1 contains /-sep-/ at the end and the file two begins with /-sep-/

how can I handle this?

note that on the file one I shoul开发者_如何学Pythond remove a break line and the /-sep-/ and on the file 2 remove the /-sep-/ and a break line :S


Reverse it: tell it what to not print instead.

sed '/-sep-/Q' /export/data.temp > /export/data.sql.md5
sed '1,/-sep-/d'/export/data.temp > /export/data.sql

(Regarding that break line, I did not understand it. A sample input would probably help.)

By the way, your original code needs only minor addition to do what you want:

sed -n '1,/-sep-/{/-sep-/!p}' /export/data.temp > /export/data.sql.md5
sed -n '/-sep-/,${/-sep-/!p}' /export/data.temp > /export/data.sql


$ cat >testfile
a
a
a
a
/-sep-/
b
b
b
b

and then

$ csplit testfile '/-sep-/' '//'
8
8
8
$ head -n 999 xx*
==> xx00 <==
a
a
a
a

==> xx01 <==
/-sep-/

==> xx02 <==
b
b
b
b


sed -n '/-sep-/q; p' /export/data.temp > /export/data.sql.md5
sed -n '/-sep-/,$ {p}' /export/data.temp | sed '1d' > /export/data.sql 

Might be easier to do in one pass with awk:

awk -v out=/export/data.sql.md5 -v f2=/export/data.sql '
  /-sep-/ { out=f2; next}
  { print > out }
' /exourt/data.temp
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号