sed

Insert a character at a specific location using sed

旧城冷巷雨未停 提交于 2020-08-22 04:06:14
问题 How to insert a dash into a string using sed (e.g., 201107 to 2011-07 )? 回答1: echo "201107" | sed 's/201107/2011-07/' Should work. But just kidding, here's the more general solution: echo "201107" | sed 's/^\(.\{4\}\)/\1-/' This will insert a - after the first four characters. HTH 回答2: For uncertain sed version (at least my GNU sed 4.2.2), you could just do: echo "201107" | sed 's/./&-/4' /4 -> replace for the 4th occurrence (of search pattern . ) & -> back reference to the whole match I

sum value of a 3rd row and divide rows in such a way that their sum values matches

风格不统一 提交于 2020-08-10 19:29:45
问题 I have a file as below with n number of rows, I want to total it's sum(based on 3rd column) and distribute rows accordingly in 3 different files(based on sum of each) For example- if we sum all the 3rd column values it's total is coming as 516 and if we divide it by 3 it is 172. So i want to add a rows to a file so it doesn't exceed 172 mark, same with the 2nd file and rest all rows should move to the third file. Just have to make sure sum value of all the 3 files should match(small

sed - Piping a string before the last line in a file

青春壹個敷衍的年華 提交于 2020-08-02 13:12:49
问题 I have a command that prints a single line. I want to add/pipe this line to a file, just above its last line. my_cmd | sed -i '$i' test I just find an empty line in the correct place, above the last line. I notice that when I add any string as '$i foo' , the "foo" gets printed in the correct place, but I want the piped line to be printed. How can I use STDIN instead of "foo"? 回答1: this should do the trick: sed -i "\$i $(cmd)" file test: kent$ cat f 1 2 3 4 5 kent$ sed -i "\$i $(date)" f kent$

sed - Piping a string before the last line in a file

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-02 13:12:28
问题 I have a command that prints a single line. I want to add/pipe this line to a file, just above its last line. my_cmd | sed -i '$i' test I just find an empty line in the correct place, above the last line. I notice that when I add any string as '$i foo' , the "foo" gets printed in the correct place, but I want the piped line to be printed. How can I use STDIN instead of "foo"? 回答1: this should do the trick: sed -i "\$i $(cmd)" file test: kent$ cat f 1 2 3 4 5 kent$ sed -i "\$i $(date)" f kent$

sed - Piping a string before the last line in a file

大城市里の小女人 提交于 2020-08-02 13:11:59
问题 I have a command that prints a single line. I want to add/pipe this line to a file, just above its last line. my_cmd | sed -i '$i' test I just find an empty line in the correct place, above the last line. I notice that when I add any string as '$i foo' , the "foo" gets printed in the correct place, but I want the piped line to be printed. How can I use STDIN instead of "foo"? 回答1: this should do the trick: sed -i "\$i $(cmd)" file test: kent$ cat f 1 2 3 4 5 kent$ sed -i "\$i $(date)" f kent$