简体   繁体   English

Linux Shell命令将文本数据从文件复制到另一个文件

[英]Linux shell command to copy text data from a file to another

file_1 contents: file_1内容:
aaa 111 222 333 aaa 111222333
bbb 444 555 666 bbb 444555666
ccc 777 888 999 ccc 777888999

file_2 contents: file_2内容:
ddd ddd
eee ee
fff fff

how do i copy only part of the text from file_1 to file_2 我如何只将部分文本从file_1复制到file_2
so that file_2 would become: 这样file_2就会变成:
ddd 111 222 333 ddd 111222333
eee 444 555 666 eee 444555666
fff 777 888 999 fff 777888999

Try with awk: 尝试使用awk:

awk 'NR==FNR{a[FNR]=$2FS$3FS$4;next} {print $0, a[FNR]}' file_1 file_2

Explanation: 说明:

NR is the current input line, FNR is the number of input line in current file, you can see that by NR是当前输入行, FNR是当前文件中输入行的数量,可以看到

$ awk '{print NR,FNR}' file_1 file_2
1 1
2 2
3 3
4 1
5 2
6 3

So, the condition NR==FNR is only true when reading the first file, and that's when the columns $2 , $3 , and $4 get saved in a[FNR] . 因此,条件NR==FNR仅在读取第一个文件时为true,即当列$2$3$4保存在a[FNR] After reading file_1 , the condition NR==FNR becomes false and the block {print $0, a[FNR]} is executed, where $0 is the whole line in file_2 . 读取file_1 ,条件NR==FNR变为false,并执行块{print $0, a[FNR]} ,其中$0file_2的整行。

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

相关问题 在文件中查找文本并将其复制到另一个路径(linux 命令) - Find text in a file and copy it to another path(linux command) 将模式文本从一个文件复制到 Linux 中的另一个文件 - Copy pattern text from one file to another file in Linux 如何使用Linux Shell脚本读取文本文件列并将文件复制到子目录中的另一个路径 - How to read the column of text file using Linux shell script and copy the files to another path with in sub directories 使用Python将数据从文本文件复制到Linux中的目录 - Copy data from a text file to a directory in Linux using Python Linux脚本可将特定范围的数据从网页复制到文本文件 - Linux script to copy specific range of data from a webpage to a text file linux或Windows shell脚本上传文件并将其复制到另一台服务器 - linux or windows shell script to upload a file and copy it to another server 我想使用 Linux 命令行将文件中的一列复制到另一个文件中的第一列 - I want to copy a column from file to be the first column in another file using Linux command line 从Shell脚本在另一台Linux服务器上远程执行命令 - Executing command remotely on another linux server from a shell script Linux Shell输出命令到文件 - Linux shell output command to file 将代码从一个文件复制到另一个文件的命令 - Command to copy code from one file to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM