Linux paste text into file

Append text to file from command line without using io redirection

this sounds like an XY problem (perlmonks.org/index.pl?node_id=542341), why do you need append without redirection?

@Joel : could be true. I asked the question for a colleague, and really don’t know what the exact problem is.

Typically useful when manipulating files with sudo (the IO are under the user’s environment), and you sometimes have a limited set of tools allowed via sudo.

5 Answers 5

If you don’t mind using sed then,

$ cat test this is line 1 $ sed -i '$ a\this is line 2 without redirection' test $ cat test this is line 1 this is line 2 without redirection

As the documentation may be a bit long to go through, some explanations :

  • -i means an inplace transformation, so all changes will occur in the file you specify
  • $ is used to specify the last line
  • a means append a line after
  • \ is simply used as a delimiter

I get sed: -i may not be used with stdin on macOS 13.3 bash shell. Probably cos of stackoverflow.com/a/21243111/259453

If you just want to tack something on by hand, then the sed answer will work for you. If instead the text is in file(s) (say file1.txt and file2.txt):

perl -e ‘open(OUT, «>>», «outfile.txt»); print OUT while (<>);’ file*.txt

N.B. while the >> may look like an indication of redirection, it is just the file open mode, in this case «append».

You can use the —append feature of tee :

cat file01.txt | tee --append bothFiles.txt cat file02.txt | tee --append bothFiles.txt 
cat file01.txt file02.txt | tee --append bothFiles.txt 

I assume the request for no redirection ( >> ) comes from the need to use this in xargs or similar. So if that doesn’t count, you can mute the output with >/dev/null .

Piping will also step out of xargs.. So not much better than >> , the best approach here is using sh -c

@Tofandel, I meant before xargs . You can pipe as much as you want before xargs ; but >> will require you to end the pipe, which then can’t continue to xargs .

You can use Vim in Ex mode:

I like the approach in this answer, but it doesn’t seem to append to the file. A simple example like this: for fidx in $(seq 1 6); do ex -sc «a|Something$» -cx test.txt; done produces text that is not ordered:cat test.txt Something2 Something1 Something3 Something4 Something5 Something6. How do I make it go to the end of the file before appending? Interestingly, it only seems to happen for the first two lines of an empty file. If I run the example multiple times everything is ordered correctly.

Try putting a $ before the a, which references the end of the file: ex -sc «$a|Something$» -cx test.txt

Источник

How to Cut, Copy and Paste Text in Nano editor?

While Nano is more beginner-friendly than Vim and Emacs, you still need to know the keyboard shortcuts for even the basic operations such as cut, copy and paste in Nano.

Читайте также:  Как в linux монтировать диск

GNU Nano is an editor that has a minimal learning curve and hence is widely used for beginner-level guides.

That doesn’t mean that it is as easy to use for beginners as a graphical text editor. Why? because you still have to rely on the keyboard shortcuts to do the basic things such as save, undo, etc.

How about cut, copy and paste in Nano? Does it require specific keyboard shortcuts too?

Well, yes and no. You can use the mouse to copy-paste. There are also keyboard shortcuts for the same purpose. To cut, you must use shortcuts.

Let me show all this to you in detail.

Copy text in Nano

To copy a piece of text, the text needs to be selected first.

There are two ways to copy the text in Nano:

Using mouse to select, copy and paste

If you want a quick and dirty selection to copy text, there is no better way than using a mouse to do it.

Below are the steps you can follow:

Once the text is copied to clipboard, to paste text using mouse, make sure that you have moved the cursor to the location where you want to paste text.

Then, do a right click, and select «Paste» from the context menu that popped up.

This is what you should do to select, copy and paste text using mouse.

Using keyboard to select and copy (for pros)

Using a mouse to copy text is all good, but when you have to select text that extends the page, it gets frustrating. For that, you will need to use a few keyboard shortcuts.

Below are the steps to select and copy text using keyboard:

  1. To select text, press the Ctrl + 6 key combination. You will see «Mark Set» appear at the bottom of the screen as an acknowledgement.
  2. Now, use the arrow keys (keys like Home, End, Page Up and Page Down can also be used) to select/highlight text.
  3. To copy selected text, press Alt + 6 key combination.

You now have the text copied to your clipboard!

Cut text in Nano

The cut operation is very similar to the copy operation, the only difference is that the data is deleted from one location to be moved to another location.

Unfortunately, cutting text cannot be done with the use of a mouse, so let’s go over the steps to follow if you want to cut text.

  1. Select text using the Ctrl + 6 key combination.
  2. Use the arrow keys to highlight the text you want to cut.
  3. To cut the selected text, press Ctrl + K (think of it as Kutting text).

That is all there is to kutting text 😀

Paste in Nano

Finally, after copying or cutting the text, it won’t be much of a use if you can not paste text.

Thankfully, pasting text in nano is very easy. Follow the steps given below:

  1. Move your cursor to the location where you want to paste text
  2. Press Ctrl + U
Читайте также:  Узнать какие демоны запущены linux

Nano shows the keyboard shortcuts at the bottom of the screen all the time. So, even if you forgot it, you can always look at the bottom of the editor for hints or press Ctrl+G for help. If it says ^K for Cut, use Ctrl+K (^ means Ctrl key). Similarly, if it says M-A for Append, use Alt + M (M means Alt key).

Copy-paste between Nano and the system

If you want to copy text from a web browser or other editor into Nano, use the Ctrl+Shift+V to paste the text into Nano.

Similarly, if you want to copy some text from Nano to an external application, use Ctrl+Shift+C to copy the text and then the usual Ctrl+V to paste it.

When you use Ctrl+Shift+C/V keys, the text is copied into the system buffer and hence it can be used outside the Nano editor as well.

If you use the Nano specific keyboard shortcuts such as Ctrl+K etc, the text stays in Nano buffer. It cannot be accessed at the system level and hence you cannot use it outside Nano.

Conclusion

GNU Nano is a simple text editor that is known best for getting the job done without much fuss. This article covered the basics of copying, cutting and pasting text in Nano.

If you are interested in mastering the basics of Nano, try this free course on Nano.

Источник

Inserting text into a text file

Alright, this maybe the simplest (or the stupidest) question, but I just got to know. Suppose I have a text file containing account no. and balance. I am writing a program to search the file using entered account no. and update the balance field in the same text file with a new balance. I am finding it surprisingly difficult to do so using file streams. The problem is that I am trying to overwrite the balance string in the said text file with a new balance string. So, if the balance is 1000 (4 digits), I can overwrite it with another 4 digit string. But, if the new balance string is more than 4 digits, it is overwriting the data after the balance field (it is a simple text file mind you. ). For example, if the text file contains

Acc. No. balance 123456 100 123567 2500 

The fields are separated by TAB ‘\t’ character, and next record is separated by a newline ‘\n’. If I enter new deposit of 200000 for account 123456, the fwrite() function overwrites the data in the text file as.

Acc. No Balance 123456 2001003567 2500 

You can notice that the ‘\n’ after the balance field, and 2 digits from the next accounts’ acc. no. is overwritten. Of course, no-one wants that to happen 🙂 What I need is a way to insert text in that file, not just overwrite it. There are many results of doing this using Java, python or even SED, but nothing using FILE streams. Please share your thoughts. thanks.

Источник

What’s the quickest way to add text to a file from the command line?

Occasionally I have a thought that I want to write into a file while I am at the terminal. I would want these notes all in the same file, just listed one after the other. I would also like a date / time tag on each one. Is it possible to do this without having to open the file each time? Can I just enter it into the terminal and have it appended to the file each time with a command or script? I am using GNU BASH.

Читайте также:  Linux cat write to file

9 Answers 9

Also, to write multiple lines to a file from the command line, do:

Write yourself a shell script called «n». Put this in it:

#!/bin/sh notefile=/home/me/notefile date >> $notefile emacs $notefile -f end-of-buffer 

I recommend this instead of cat >> notefile because:

  1. One day you’ll be in such a hurry that you’ll fumblefinger the >> and type > instead and blow away your file.
  2. Emacs starts in five one-hundredths of a second on my Mac Mini. It takes a tenth of a second to start on a ten year old Celeron-based system I have sitting around. If you can’t wait that long to start typing, then you’re already a machine and don’t need to take notes. 🙂

If you insist on avoiding a text editor, use a shell function:

n () < date >> /home/me/notefile; cat >> /home/me/notefile; > 

which should work in all shells claiming Bourne shell compatibility.

How does the n function write to the file? Don’t you mean something like a echo «$*» rather than UUOC? How does it incorporate the date/time tag?

Not quite useless. It reads from stdin, which is where the user will be typing their note. I did forget the datestamp, though.

You can use >> to append to a file, or use > to overwrite it.

Lots of neat bells and whistles here!

date >>filename; cat >>filename 

If you have ruby installed on your computer you can use https://github.com/minhajuddin/taskr . This way you get a nice view of your notes with tags and the hours elapsed.

Taskr screenshot

Depending on your needs, syslogd might be another tool to peruse. The command

theuser@tetrad:~$ logger This message is sent to syslog 

will log the message with the facility user.notice. With many Linux systems this will be enough to have a file /var/log/user.log opened and appended to, with others you may need to define a handling for that facility and log level (or, whichever facility you choose — the local0 to local7 facilities are usually free to assign to things like this.

It’s got the benefit of being able (aka configurable) to send notes from client machines to a central logging server, something I like to use for keeping track of administrative action since it preserves timestamp, user and host information automagically, while keeping actions in order.

resulting output in local file:

theuser@tetrad:~$ tail /var/log/user.log Jan 31 07:18:37 tetrad theuser: This message is sent to syslog 

Example for syslog configuration line on Solaris:

local5.notice ifdef(`LOGHOST', /var/log/diary, @loghost) 

Note: The ifdef is preprocessed with m4 , on the machine with the hostname/hostalias «loghost», the messages will be logged to the file /var/log/diary, on all others, they will be sent to the remote syslog service at loghost. To test this kind of configuration, the config file can be sent through m4 for expansion (leave away the -D LOGHOST to see how it would look on a system not called loghost:

theuser@solstice$ /usr/ccs/bin/m4 -D LOGHOST /etc/syslog.conf 

Источник

Оцените статью
Adblock
detector