循环插入固定字符

CT_LXL 2015-01-29 09:25:58
使用一句命令实现在某个文件的前20行插入固定字符"aaaaa"
...全文
215 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
奔跑的路 2015-01-30
  • 打赏
  • 举报
回复
对了,可以看一下man ed ,看看它支持哪些正则表达式匹配
奔跑的路 2015-01-30
  • 打赏
  • 举报
回复
好吧,看了这个我真没找到问题所在! 再这样试试吧,不行的话,要赎能力不够了 sed ‘1,20 s/.*/aaaa&/g’ a.txt > b.txt 或 sed '1,20 s/.*/&aaaa/g' a.txt > b.txt 如果b.txt里面为空,那么添加-n看看能不能输出到终端 这样不行就只好试试把这20行取出来循环处理了!
CT_LXL 2015-01-30
  • 打赏
  • 举报
回复
供你参考 sed Command Purpose A stream editor. Syntax sed [ -n ] [ -u ] Script [ File ... ] sed [ -n ] [ -u ] [ -e Script ] ... [ -f ScriptFile ] ... [ File ... ] Description The sed command modifies lines from the specified File parameter according to an edit script and writes them to standard output. The sed command includes many features for selecting lines to be modified and making changes only to the selected lines. The sed command uses two work spaces for holding the line being modified: the pattern space, where the selected line is held; and the hold space, where a line can be stored temporarily. An edit script consists of individual subcommands, each one on a separate line. The general form of sed subcommands is the following: [address-range] function[modifiers] The sed command processes each input File parameter by reading an input line into a pattern space, applying all sed subcommands in sequence whose addresses select that line, and writing the pattern space to standard output. It then clears the pattern space and repeats this process for each line specified in the input File parameter. Some of the sed subcommands use a hold space to save all or part of the pattern space for subsequent retrieval. When a command includes an address (either a line number or a search pattern), only the addressed line or lines are affected by the command. Otherwise, the command is applied to all lines. An address is either a decimal line number, a $ (dollar sign), which addresses the last line of input, or a context address. A context address is a regular expression similar to those used in the ed command except for the following differences: * You can select the character delimiter for patterns. The general form of the expression is: \?pattern? where ? (question mark) is a selectable character delimiter. You can select any character from the current locale except for the space or new-line character. The \ (backslash) character is required only for the first occurrence of the ? (question mark). The default form for the pattern is the following: /pattern/ A \ (backslash) character is not necessary. * The \n sequence matches a new-line character in the pattern space, except the terminating new-line character. * A . (period) matches any character except a terminating new-line character. That is, unlike the ed command, which cannot match a new-line character in the middle of a line, the sed command can match a new-line character in the pattern space. Certain commands called addressed commands allow you to specify one line or a range of lines to which the command should be applied. The following rules apply to addressed commands: * A command line without an address selects every line. * A command line with one address, expressed in context form, selects each line that matches the address. * A command line with two addresses separated by commas selects the entire range from the first line that matches the first address through the next line that matches the second. (If the second address is a number less than or equal to the line number first selected, only one line is selected.) Thereafter, the process is repeated, looking again for the first address. Flags Item Description -e Script Uses the Script variable as the editing script. If you are using just one -e flag and no -f flag, the -e flag can be omitted. -f ScriptFile Uses the ScriptFile variable as the source of the edit script. The ScriptFile variable is a prepared set of editing commands applied to the File parameter. -n Suppresses all information normally written to standard output. -u Displays the output in an unbuffered mode. When this flag is set, the sed command displays the output instantaneously instead of buffering the output. The default is buffered mode. Note: You can specify multiple -e and -f flags. All subcommands are added to the script in the order specified, regardless of their origin. sed Subcommands The sed command contains the following sed script subcommands. The number in parentheses preceding a subcommand indicates the maximum number of permissible addresses for the subcommand. Note: 1 The Text variable accompanying the a\, c\, and i\ subcommands can continue onto more than one line, provided all lines but the last end with a \ (backslash) to quote the new-line character. Backslashes in text are treated like backslashes in the replacement string of an s command and can be used to protect initial blanks and tabs against the stripping that is done on every script line. The RFile and WFile variables must end the command line and must be preceded by exactly one blank. Each WFile variable is created before processing begins. 2 The sed command can process up to 999 subcommands in a pattern file. Item Description (1) a\Text Places the Text variable in output before reading the next input line. (2)b[label] Branches to the : command bearing the label variable. If the label variable is empty, it branches to the end of the script. (2)c\Text Deletes the pattern space. With 0 or 1 address or at the end of a 2-address range, places the Text variable in output and then starts the next cycle. (2)d Deletes the pattern space and then starts the next cycle. (2)D Deletes the initial segment of the pattern space through the first new-line character and then starts the next cycle. (2)g Replaces the contents of the pattern space with the contents of the hold space. (2)G Appends the contents of the hold space to the pattern space. (2)h Replaces the contents of the hold space with the contents of the pattern space. (2)H Appends the contents of the pattern space to the hold space. (1)i\Text Writes the Text variable to standard output before reading the next line into the pattern space. (2)l Writes the pattern space to standard output showing nondisplayable characters as 4-digit hexadecimal values. Long lines are folded. (2)l Writes the pattern space to standard output in a visually unambiguous form. The characters \\\, \\a, \\b, \\f, \\r, \\t, and \\v are written as the corresponding escape sequence. Non- printable characters are written as 1 three-digit octal number (with a preceding backslash character) for each byte in the character (most significant byte first). This format is also used for multibyte characters. This subcommand folds long lines. A backslash followed by a new-line character indicates the point of folding. Folding occurs at the 72nd column position. A $ (dollar sign) marks the end of each line. (2)n Writes the pattern space to standard output if the default output is not suppressed. It replaces the pattern space with the next line of input. (2)N Appends the next line of input to the pattern space with an embedded new-line character (the current line number changes). You can use this to search for patterns that are split onto two lines. (2)p Writes the pattern space to standard output. (2)P Writes the initial segment of the pattern space through the first new-line character to standard output. (1)q Branches to the end of the script. It does not start a new cycle. (2)r RFile Reads the contents of the RFile variable. It places contents in output before reading the next input line. (2)s/pattern/replacement/flags Substitutes the replacement string for the first occurrence of the pattern parameter in the pattern space. Any character that is displayed after the s subcommand can substitute for the / (slash) separator except for the space or new-line character. See the "Pattern Matching" section of the ed command. The value of the flags variable must be zero or more of: g Substitutes all non-overlapping instances of the pattern parameter rather than just the first one. n Substitutes for the n-th occurrence only of the pattern parameter. p Writes the pattern space to standard output if a replacement was made. w WFile Writes the pattern space to the WFile variable if a replacement was made. Appends the pattern space to the WFile variable. If the WFile variable was not already created by a previous write by this sed script, the sed command creates it.
奔跑的路 2015-01-30
  • 打赏
  • 举报
回复
引用 4 楼 zlloct 的回复:
[quote=引用 3 楼 lee244868149 的回复:] sed '1,20 s/^/aaaaa/g' a.txt > b.txt
不行,b.txt为空[/quote] 表示没用过Unix啊,和Linux应该差不多吧,能不能把man sed 发上来看看
CT_LXL 2015-01-30
  • 打赏
  • 举报
回复
引用 8 楼 lee244868149 的回复:
看来我只能循环处理了
CT_LXL 2015-01-29
  • 打赏
  • 举报
回复
引用 1 楼 lee244868149 的回复:
插到行首: sed -i '1,20 s/^/aaaaa/g' a.txt 插到行尾: sed -i '1,20 s/$/aaaaa/g' a.txt
不行,报错如下: 请注意,我的是Unix系统
奔跑的路 2015-01-29
  • 打赏
  • 举报
回复
插到行首: sed -i '1,20 s/^/aaaaa/g' a.txt 插到行尾: sed -i '1,20 s/$/aaaaa/g' a.txt
CT_LXL 2015-01-29
  • 打赏
  • 举报
回复
引用 3 楼 lee244868149 的回复:
sed '1,20 s/^/aaaaa/g' a.txt > b.txt
不行,b.txt为空
奔跑的路 2015-01-29
  • 打赏
  • 举报
回复
引用 2 楼 zlloct 的回复:
[quote=引用 1 楼 lee244868149 的回复:] 插到行首: sed -i '1,20 s/^/aaaaa/g' a.txt 插到行尾: sed -i '1,20 s/$/aaaaa/g' a.txt
不行,报错如下: 请注意,我的是Unix系统[/quote] 可能是-i选项不支持吧,把-i去除了,将输出重定向到一个新文件中 sed '1,20 s/^/aaaaa/g' a.txt > b.txt

2,162

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 UNIX文化
社区管理员
  • UNIX文化社区
  • 文天大人
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧