简体   繁体   English

用bash将一行从一个文件复制到另一个文件

[英]Copying a line from one file to another in bash

I'm trying to copy a line from file1 and write that line into line 10 of file2 using bash. 我正在尝试从file1复制一行并将其使用bash写入file2的第10行。

Here is what I have tried: 这是我尝试过的:

sed -e '10i' file2 <file1

You can do this using awk: 您可以使用awk执行此操作:

awk 'NR==1{a=$0}NR==FNR{next}FNR==10{print a}1' file1 file2

NR keeps a count of the overall line number. NR保留总行数的计数。 This is only equal to 1 for the first line of the first file. 对于第一个文件的第一行,该值仅等于1。 The contents of the line is stored in a variable a . 该行的内容存储在变量a

FNR keeps a count of the line number of the current file. FNR保留当前文件的行号的计数。 NR and FNR are equal (ie we are in file1), next skips to the next line, which means that for every other line in the first file, nothing happens. NRFNR相等(即,我们在file1中), next跳到下一行,这意味着对于第一个文件中的所有其他行,什么都没有发生。

The final block is executed at line 10 of the second file, inserting the value of a . 最后块是在第二个文件的第10行执行的,插入的值a The 1 at the end is a common shorthand which means that every line of the file is printed. 末尾的1是常用的缩写,表示文件的每一行都被打印。

Testing it out: 测试一下:

$ cat file1
line 1 in file1
line 2 in file1    
$ cat file2
line 1 in file2
line 2 in file2
line 3 in file2
line 4 in file2
line 5 in file2
line 6 in file2
line 7 in file2
line 8 in file2
line 9 in file2
line 10 in file2
$ awk 'NR==1{a=$0}NR==FNR{next}FNR==10{print a}1' file1 file2
line 1 in file2
line 2 in file2
line 3 in file2
line 4 in file2
line 5 in file2
line 6 in file2
line 7 in file2
line 8 in file2
line 9 in file2
line 1 in file1
line 10 in file2

To overwrite the original contents of file2, you can redirect the output to a temporary file like awk '...' file1 file2 tmp && mv tmp file2 . 要覆盖file2的原始内容,您可以将输出重定向到一个临时文件,例如awk '...' file1 file2 tmp && mv tmp file2

I'm not sure where you want your lines to come from and go to, but you could do this: 我不确定您希望线路来自何处,但是您可以这样做:

sed 'NUMq;d' file1 >>file2

replacing NUM with the number of the line you want from file1 to append to file2. NUM替换为您要从file1追加到file2的行号。

EDIT 编辑

I think this is what you want: 我认为这是您想要的:

sed -i .bak "10 i\\
`sed 1q\;d file1`
" file2

If you are using GNU sed , this: 如果您使用的是GNU sed ,则:

sed -n '1p' choose-line-here.txt | sed -i '10r /dev/stdin' insert-here.txt

will insert line 1 from choose-line-here.txt after line 10 in insert-here.txt . 将在insert-here.txt第10 choose-line-here.txt之后从choose-line-here.txt插入第1 choose-line-here.txt

Changing line 10 seems to be trickier, surprisingly. 令人惊讶的是, 更改 10号线似乎比较棘手。 I've come up with this so far: 到目前为止,我已经提出了:

sed -n 1p choose-line-here.txt |
sed -e '9!b' -e 'N;s/\n.*//;r /dev/stdin' insert-here.txt

The tricky part is, that the r command inserts the contents of /dev/stdin , that is, standard input, after the current line. 棘手的部分是, r命令在当前行之后插入/dev/stdin的内容,即标准输入。 If that is the line which is to be replaced, then I don't see a way to get rid of it without outputting a spare newline. 如果那是要替换的行,那么在不输出备用换行符的情况下,我看不到摆脱它的方法。 Therefore the trick to start the insertion in the previous line, line 9 in this case. 因此,开始在上一行(在本例中为第9行)插入的技巧。

A simpler solution for changing the target line is possible, if one relies on sed not counting the freshly read line from stdin: 如果依赖于sed而不计算从stdin读取的新行,则可以使用一种更简单的解决方案来更改目标行:

sed -n 1p choose-line-here.txt |
sed -i -e '9r /dev/stdin' -e '10d' insert-here.txt

This might work for you (GNU sed & Bash): 这可能适合您(GNU sed和Bash):

sed $'10{e sed "1!d" file1\n;d}' file2

or another way using a pipe: 或使用管道的另一种方式:

sed '1!d' file1 | sed -e '10{r /dev/stdin' -e 'd}' file2

or a third way (perhaps more intuitively but only works for a singleton replacement line): 或第三种方式(也许更直观,但仅适用于单例替换行):

sed '10\c'$(sed '1!d' file1) file2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用bash脚本将文本从一个文件复制到另一个文件 - Copying text from one file to another using bash script 使用bash脚本选择性地从一个文件复制到另一个文件 - Selective copying from one file to another using bash script 使用ubuntu bash将持续增长的文件从一台服务器复制到另一台服务器 - Copying a continuously growing file from one server to another in ubuntu bash 使用命令行将 XML 元素从一个文件复制到另一个文件 - Copying XML element from one file to another using command line Bash-如何从一个文件中查找行作为另一文件中的行的一部分? - Bash - How to find line from one file as a part of a line from another file? 在Bash中将目录结构从一个目录复制到另一个目录 - Copying directory structure from one directory to another in Bash 通过bash将文件名从一个文件夹的文件复制到另一个文件夹的文件 - copying filenames from files of one folder to files of another via bash 将列从一个CSV文件复制到另一个 - Copying a column from one CSV file to another 用bash中的另一个文件将一个文件中的第n行替换为下一行 - Replacing every nth line in one file with next line from another file in bash Bash脚本从一行复制,然后用副本替换另一行 - Bash script to copy from one line and replace another line with the copy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM