[英]How to copy value from a line in a file and paste it into a new file using Shell?
I have a file containing my personal details (.txt). 我有一个包含我的个人详细信息(.txt)的文件。 How can I, via a shell script, copy only a few lines in the file and bring it to a new .txt file?. 我如何通过Shell脚本仅复制文件中的几行并将其带到新的.txt文件中?
Name: Nik
Date of Birth: 5.06.92
Address: 1234 Main St.
SSN: 123-45-6789
I would like to copy my address 1234 Main St.
to another file. 我想将我的地址1234 Main St.
复制到另一个文件。
怎么样:
awk -F': ' '/^Address/{print $2}' personalinfo.txt | tee newfile.txt
If you want to copy a specific line by its number : 如果要按编号复制特定行:
sed -n '1 p' personal_details.txt > newfile.txt # Where 1 is the line number
If you want to copy a line by its contents : 如果要按行内容复制行:
grep 'Name' personal_details.txt > newfile.txt
If you want to copy a line but only its value, not the name of the field: 如果要复制一行,而只复制其值,而不是字段名:
awk -F':' '/^Name/ {print $2}' personal_details.txt > newfile.txt
Regards! 问候!
Just in case you would like to use only grep
or pcregrep
(in case you are on a macOS): 万一您只想使用grep
或pcregrep
(如果您使用的是macOS):
grep -oP "Address: \K.+" file.txt > newfile.txt
Or in macOS: 或在macOS中:
pcregrep -o "Address: \K.+" file.txt > newfile.txt
The \\K
can be read as exclude everything at the left before it and return only the right part .+
\\K
可以理解为排除在它之前左边的所有内容,而只返回右边的部分.+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.