简体   繁体   中英

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").

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.

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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