简体   繁体   English

BASH从终端复制特定行

[英]BASH copy specific lines from terminal

Let's say currently my terminal looks like the following: 假设当前我的终端如下所示:

neekoy@mypc:~/some/folder$ echo "lala nana"
lala nana
neekoy@mypc:~/some/folder$ l
entrypoint.js  helpers/  node_modules/  package.json  package-lock.json
neekoy@mypc:~/some/folder$

I want to copy the fourth line from the bottom (the one that contains "lala nana"). 我要从底部复制第四行(包含“ lala nana”的那一行)。

I can obviously write a script that uses tput to save the cursor position, switch my cursor's position to a specific line, copy it, and then return to the previous cursor position. 我显然可以编写一个脚本,使用tput保存光标位置,将光标的位置切换到特定行,复制它,然后返回到先前的光标位置。

Is there an existent solution or an easier way to accomplish this though? 是否有一个现有的解决方案或更简单的方法来完成此任务?

I'm not sure I understand your problem. 我不确定我是否理解您的问题。 I would solve it this way, though. 我会这样解决的。

I would put the command into a variable to access it again later. 我会将命令放入变量中,以便稍后再次访问。 ( http://www.tldp.org/LDP/abs/html/varassignment.html ) http://www.tldp.org/LDP/abs/html/varassignment.html

Here is an example: 这是一个例子:

neekoy@mypc:~/some/folder$ c=$(echo "lala nana")
neekoy@mypc:~/some/folder$ echo $c
lala nana

Or like in your example: 或像您的示例所示:

neekoy@mypc:~/some/folder$ echo "lala nana"
lala nana
neekoy@mypc:~/some/folder$ l
entrypoint.js  helpers/  node_modules/  package.json  package-lock.json

neekoy@mypc:~/some/folder$ c=$(!-2) #related to https://unix.stackexchange.com/a/33552
neekoy@mypc:~/some/folder$ echo $c
lala nana

Or save the output as text in a file. 或将输出另存为文本文件。

neekoy@mypc:~/some/folder$ echo "lala nana"
lala nana
neekoy@mypc:~/some/folder$ l
entrypoint.js  helpers/  node_modules/  package.json  package-lock.json

neekoy@mypc:~/some/folder$ !-2 > test.txt
echo "lala nana" > test.txt
neekoy@mypc:~/some/folder$ cat test.txt
lala nana

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM