Pasting files in linux

Команда Paste в Linux (Объединение Строк)

Paste Command in Linux (Merge Lines)

В этом руководстве мы объясним, как использовать команду paste.

paste это команда, которая позволяет объединять строки файлов по горизонтали. Он выводит строки, состоящие из последовательно соответствующих строк каждого файла, указанного в качестве аргумента, разделенных табуляцией.

Как использовать paste команду

paste является одной из малоизвестных и используемых утилит командной строки для Linux и Unix.

Общий синтаксис paste команды выглядит следующим образом:

Если входные файлы не предоставлены или когда — задано в качестве аргумента, paste используется стандартный ввод.

Предположим, у нас есть следующие файлы:

При вызове без параметра paste будут считаны все файлы, заданные в качестве аргументов, и объединены по горизонтали соответствующие строки файлов, разделенные пробелом:

Вместо отображения вывода на экран, вы можете перенаправить его в файл , используя > , >> оператор:

Если файл не существует, он будет создан. > Оператор будет перезаписывать существующий файл, в то время как >>
оператор будет добавлять вывод в файл.

Параметр -d , -delimiters позволяет указать список символов, которые будут использоваться в качестве разделителей вместо разделителя по умолчанию TAB .

Каждый разделитель используется последовательно. Когда список исчерпан, paste начинается снова с первого символа разделителя.

Чтобы использовать _ символ (подчеркивание) в качестве разделителя вместо TAB , вы должны набрать:

Вот пример использования двух разделителей:

Строки из первого и второго файла отделяются первым символом из списка разделителей. Вторая и третья строки файла разделены вторым разделителем.

Если было дано больше файлов, paste начинается снова с начала списка.

Опция -s , —serial заставляет paste отображать строки одного файла за раз вместо одной строки из каждого файла.

Команда объединит все строки из данного файла в отдельные строки:

При использовании с -z , —zero-terminated вариантом, paste использует нулевой символ , чтобы разграничить элементы вместо перевода строки по умолчанию символ в. Такое поведение удобно , когда paste используются в сочетании с find -print0 и xargs -0 командами для обработки имен файлов , содержащих специальные символы.

Вывод

Команда paste используется для объединения соответствующих строк заданных файлов.

Источник

Paste command in Linux with examples

Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash (“-“) instead of file name, paste reads from standard input and gives output as it is until a interrupt command [Ctrl-c] is given.

Syntax:

Let us consider three files having name state, capital and number. state and capital file contains 5 names of the Indian states and capitals respectively. number file contains 5 numbers.

Without any option paste merges the files in parallel. The paste command writes corresponding lines from the files with tab as a deliminator on the terminal.

In the above command three files are merges by paste command.

Options:

1. -d (delimiter): Paste command uses the tab delimiter by default for merging the files. The delimiter can be changed to any other character by using the -d option. If more than one character is specified as delimiter then paste uses it in a circular fashion for each file line separation.

2. -s (serial): We can merge the files in sequentially manner using the -s option. It reads all the lines from a single file and merges all these lines into a single line with each line separated by tab. And these single lines are separated by newline.

In the above command, first it reads data from number file and merge them into single line with each line separated by tab. After that newline character is introduced and reading from next file i.e. state starts and process repeats again till all files are read.

Combination of -d and -s: The following example shows how to specify a delimiter for sequential merging of files:

3. –version: This option is used to display the version of paste which is currently running on your system.

Applications of Paste Command

1. Combining N consecutive lines: The paste command can also be used to merge N consecutive lines from a file into a single line. Here N can be specified by specifying number hyphens(-) after paste.

2. Combination with other commands: Even though paste require at least two files for concatenating lines, but data from one file can be given from shell. Like in our example below, cut command is used with -f option for cutting out first field of state file and output is pipelined with paste command having one file name and instead of second file name hyphen is specified.

Note: If hyphen is not specified then input from shell is not pasted.

Ordering of pasting can be changed by altering the location of hyphen:

This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

How to Copy and Paste Text, Files and Folders in Linux Terminal

Copying and pasting is one of the most used actions on a computer. While it is easy to do so with the Ctrl + C and Ctrl + V keyboard shortcuts, on the Linux terminal it is not so straightforward. You have several options to get the job done. Here is how you can copy and paste text, files and directories in Linux terminal.

Also read: How to Easily Rename Files in Linux

Copy and Paste Text

If you just want to copy a piece of text in the terminal, all you need to do is highlight it with your mouse, then press Ctrl + Shift + C to copy.

To paste it where the cursor is, use the keyboard shortcut Ctrl + Shift + V .

The Paste shortcut also applies when you copy a section of text from a Word document (or any other application) and want to paste it in the terminal. For example, you can copy a command from a web page in your browser and use the Ctrl + Shift + V shortcut to paste it in the terminal.

Copy and Paste a Single File

Any time you want to copy a file or folder in the Linux command line, the above keyboard shortcut won’t work. You have to use the cp command. cp is shorthand for copy. The syntax is simple, too. Use cp followed by the file you want to copy and the destination where you want it moved.

That, of course, assumes that your file is in the same directory you’re working out of. You can specify both.

You also have the option of renaming your file while copying it. Specify the new name in the destination.

Copy and Paste a Folder and Its Contents

In order to copy a folder and its contents, you’re going to need to tell the cp command to copy recursively. That’s simple enough with the -r flag.

All the rest of your syntax is exactly the same. The -r flag serves to tell cp that it’s working with a directory and should copy its contents.

If you want the paste action to overwrite existing files, you can add the -f flag:

Also read: How to Compress Files with zstd Utility in Linux

Copy and Paste Multiple Files

You can also copy multiple files. The Linux command line lets you target multiple items at once with brackets <> . You can use them to list the names of each file to be copied separated by commas.

All three files of differing file types will be copied to the Documents directory.

Copy and Paste All Files of the Same Type

If you have a ton of files of the same type to copy, you can use the wildcard character * . The asterisk/wildcard tells the Linux command line to accept absolutely anything in that place. So, if you tell Linux to copy *.jpg , it’ll copy all JPG files, regardless of the name or whatever comes before the .jpg part.

If you want to use multiple file types, say JPG and PNG, you can use the brackets from before.

Move a File or Folder

If you came here looking to move a file from one place to another without making a duplicate, you can do that easily too, but moving a file requires the mv command. The syntax is very similar to cp.

Similarly, you can also rename it.

There is one major difference, though. You don’t need the -r flag to move a whole folder.

That’s all there is to it. You’re ready to start copying and moving your files from the command line. You can see that the command line way can be very efficient in some situations.

Want more pointers on the Linux command line? Here’s how to check sudo history or find out what the chmod 777 command does to your file permission.

Image credit: Copy – Paste by DepositPhotos

Источник

Pasting files in linux

In this article, we will see how to use the paste command with some examples. paste command, by definition of man page, is used to merge lines of files. It is very useful for merging a single file and also for merging set of files as well. This article is divided into 2 parts:

  • paste command examples for single file handling
  • paste command examples for multiple files handling

Let us consider a file with the sample contents as below:
paste command with a single fil e:

1. paste command without any options is as good as the cat command when operated on a single file.
2. Join all lines in a file:
-s option of paste joins all the lines in a file. Since no delimiter is specified, default delimiter tab is used to separate the columns.

3. Join all lines using the comma delimiter:
-d option is used to specify the delimiter. Using this -d and -s combination, all the lines in the file get merged into a single line.

4. Merge a file by pasting the data into 2 columns:
The ‘-‘ reads a line from the standard input. Two ‘-‘ reads 2 lines and pastes them side by side.

5.Merge a file by pasting the data into 2 columns using a colon separator:
This is same as joining every 2 lines in a file.

6. Merge a file by pasting the file contents into 3 columns:
7. Merge a file into 3 columns using 2 different delimiters:
The -d option can take multiple de-limiters. The 1st and 2nd columns is separated by ‘:’, whereas the 2nd and 3rd are separated by a ‘,’.

paste command with multiple files:

Let us consider a file, file2, with the following contents:
8. paste contents of 2 files side by side.
paste command is used in scenarios to merge multiple files side by side. As shown above, the file contents are pasted side by side.

9. paste contents of 2 files side by side with a comma separator:
10. paste command can take standard input in case of multiple files too:
Like this as well:
One more:
11. Read lines in both the files alternatively:
Using the newline character as the delimiter, we can read 2 files line by line alternatively.

Источник

7 Essential and Practical Usage of Paste Command in Linux

In a previous article, we talked about the cut command which can be used to extract columns from a CSV or tabular text data file.

The paste command does the exact opposite: it merges several input files to produce a new delimited text file from them. We are going to see how to effectively use Paste command in Linux and Unix.

7 Practical examples of Paste command in Linux

If you prefer videos, you can watch this video explaining the same Paste command examples discussed in this article.

1. Pasting columns

In its most basic use case, the paste command takes N input files and join them line by line on the output:

But let’s leave now the theoretical explanations to work on a practical example. If you’ve downloaded the sample files used in the video above, you can see I have several data files corresponding to the various columns of a table:

It is quite easy to produce a tab-delimited text file from those data:

As you may see, when displayed on the console, the content of that tab-separated values file does not produce a perfectly formatted table. But this is by design: the paste command is not used to create fixed-width text files, but only delimited text files where one given character is assigned the role of being the field separator.

So, even if it is not obvious in the output above, there is actually one and only one tab character between each field. Let’s make that apparent by using the sed command:

Now, invisible characters are displayed unambiguously in the output. And you can see the tab characters displayed as \t . You may count them: there is always three tabs on every output line— one between each field. And when you see two of them in a row, that only means there was an empty field there. This is often the case in my particular example files since on each line, either the CREDIT or DEBIT field is set, but never both of them at the same time.

2. Changing the field delimiter

As we’ve seen it, the paste command uses the tab character as the default field separator (“delimiter”). Something we can change using the -d option. Let’s say I would like to use a semi-colon instead:

No need to append the sed command at the end of the pipeline here since the separator we used is a printable character. Anyway, the result is the same: on a given row, each field is separated from its neighbor by using a one-character delimiter.

3. Transposing data using the serial mode

The examples above have one thing in common: the paste command reads all its input files in parallel, something that is required so it can merge them on a line-by-line basis in the output.

But the paste command can also operate in the so-called serial mode, enabled using the -s flag. As its name implies it, in the serial mode, the paste command will read the input files one after the other. The content of the first input file will be used to produce the first output line. Then the content of the second input file will be used to produce the second output line, and so on. That also means the output will have as many lines as there were files in the input.

More formally, the data taken from file N will appear as the Nth line in the output in serial mode, whereas it would appear as the Nth column in the default “parallel” mode. In mathematical terms, the table obtained in serial mode is the transpose of the table produced in the default mode (and vice versa).

To illustrate that, let’s consider a small subsample of our data:

In the default (“parallel”) mode, the input file’s data will serve as columns in the output, producing a two columns by five rows table:

But in serial mode, the input file’s data will appear as rows, producing now a five columns by two rows table:

4. Working with the standard input

Like many standard utilities, the paste command can use the standard input to read data. Either implicitly when there is no filename given as an argument, or explicitly by using the special — filename. Apparently, this isn’t that useful though:

I encourage you to test it by yourself, but the following syntax should produce the same result— making once again the paste command useless in that case:

So, what could be the point of reading data from the standard input? Well, with the -s flag, things become a lot more interesting as we will see it now.

4.1. Joining lines of a file

As we’ve seen it a couple of paragraphs earlier, in the serial mode the paste command will write all lines of an input file on the same output line. This gives us a simple way to join all the lines read from the standard input into only one (potentially very long) output line:

This is mostly the same thing you could do using the tr command, but with one difference though. Let’s use the diff utility to spot that:

As reported by the diff utility, we can see the tr command has replaced every instance of the newline character by the given delimiter, including the very last one. On the other hand, the paste command kept the last newline character untouched. So depending if you need the delimiter after the very last field or not, you will use one command or the other.

4.2. Multi-column formatting of one input file

According to the Open Group specifications, “the standard input shall be read one line at a time” by the paste command. So, passing several occurrences of the — special file name as arguments to the paste command will result with as many consecutive lines of the input being written into the same output line:

To make things more clear, I encourage you to study the difference between the two commands below. In the first case, the paste command opens three times the same file, resulting in data duplication in the output. On the other hand, in the second case the ACCOUNTLIB file is opened only once (by the shell), but read three times for each line (by the paste command), resulting in the file content being displayed as three columns:

Given the behavior of the paste command when reading from the standard input, it is usually not advisable to use several — special file names in serial mode. In that case, the first occurrence would read the standard input until its end, and the subsequent occurrences of — would read from an already exhausted input stream— resulting in no more data being available:

5. Working with files of different length

If an end-of-file condition is detected on one or more input files, but not all input files, paste shall behave as though empty lines were read from the files on which end-of-file was detected, unless the -s option is specified.

So, the behavior is what you may expect: missing data are replaced by “empty” content. To illustrate that behavior, let’s record a couple more transactions into our “database”. In order to keep the original files intact, we will work on a copy of our data though:

With those updates, we have now registered a new capital movement from account #1080 to account #4356. However, as you may have noticed it, I didn’t bother to update the ACCOUNTLIB file. This does not seem such a big issue because the paste command will replace the missing rows with empty data:

But beware, the paste command can only match lines by their physical position: all it can tell is a file is “shorter” than another one. Not where the data are missing. So it always adds the blanks fields at the end of the output, something that can cause unexpected offsets in your data. Let’s make that obvious by adding yet another transaction:

This time, I was more rigorous since I properly updated both the account number (ACCOUNTNUM), and it’s corresponding label (ACCOUNTLIB) as well as the CREDIT and DEBIT data files. But since there were missing data in the previous record, the paste command is no longer able to keep the related fields on the same line:

As you may see it, the account #4356 is reported with the label “WEB HOSTING” whereas, in reality, that latter should appear on the row corresponding to the account #3465.

As a conclusion, if you have to deal with missing data, instead of the paste command you should consider using the join utility since that latter will match rows based on their content, and not based on there position in the input file. That makes it much more suitable for “database” style applications. I’ve already published a video about the join command, but that should probably deserve an article of its own, so let us know if you are interested in that topic!

6. Cycling over delimiters

In the overwhelming majority of the use cases, you will provide only one character as the delimiter. This is what we have done until now. However, if you give several characters after the -d option, the paste command will cycle over them: the first character will be used as the first field delimiter on the row, the second character as the second field delimiter, and so on.

Field delimiters can only appear between fields. Not at the end of a line. And you can’t insert more than one delimiters between two given fields. As a trick to overcome these limitations, you may use the /dev/null special file as an extra input where you need an additional separator:

Something you may even abuse:

However, no need to say, if you reach that level of complexity, it might be a clue the paste utility was not necessarily the best tool for the job. Maybe worth considering, in that case, something else like sed or awk command.

But what if the list contains fewer delimiters than needed to display a row in the output? Interestingly, the paste command will “cycle” over them. So, once the list is exhausted, the paste command will jump back to the first delimiter, something that probably opens the door to some creative usage. As of myself, I was not able to make anything really useful with that feature given my data. So you will have to be satisfied with the following a bit far-fetched example. But it will not be a complete waste your time since that was a good occasion to mention you have to double the backslash ( \\ ) when you want to use it as a delimiter:

7. Multibyte character delimiters

Like most of the standard Unix utilities, the paste command is born at a time one character was equivalent to one byte. But this is no longer the case: today, many systems are using the UTF-8 variable length encoding by default. In UTF-8, a character can be represented by 1, 2, 3 or 4 bytes. That allows us to mix in the same text file the whole variety of human writing— as well as tons of symbols and emojis— while maintaining ascending compatibility with the legacy one-byte US-ASCII character encoding.

Let’s say for example I would like to use the WHITE DIAMOND (◇ U+25C7) as my field separator. In UTF-8, this character is encoded using the three bytes e2 97 87 . This character might be hard to obtain from the keyboard, so if you want to try that by yourself, I suggest you copy-paste it from the code block below:

Quite deceptive, isn’t it? Instead of the expected white diamond, I have that “question mark” symbol (at least, this is how it is displayed on my system). It is not a “random” character though. It is the Unicode replacement character used “to indicate problems when a system is unable to render a stream of data to a correct symbol”. So, what has gone wrong?

Once again, examining the raw binary content of the output will give us some clues:

We already had the opportunity of practicing with hex dumps above, so your eyes should now be sharpened enough to spot the field delimiters in the byte stream. By looking closely, you will see the field separator after the line number is the byte e2 . But if you continue your investigations, you will notice the second field separator is 97 . Not only the paste command didn’t output the character I wanted, but it also didn’t use everywhere the same byte as the separator.

Wait a minute: doesn’t that remind you something we already talk about? And those two bytes e2 97 , aren’t they somewhat familiar to you? Well, familiar is probably a little bit too much, but if you jump back a few paragraphs you might find them mentioned somewhere…

So did you find where it was? Previously, I said in UTF-8, the white diamond is encoded as the three bytes e2 97 87 . And indeed, the paste command has considered that sequence not as a whole three-byte character, but as three independent bytes and so, it used the first byte as the first field separator, then the second byte as the second field separator.

I let you re-run that experiment by adding one more column in the input data; you should see the third field separator to be 87 — the third byte of the UTF-8 representation for the white diamond.

Ok, that’s the explanation: the paste command only accepts one-byte “characters” as the separator. And that’s particularly annoying, since, once again, I don’t know any way to overcome that limitation except by using the /dev/null trick I already gave to you:

If you read my previous article about the cut command, you may remember I had similar issues with the GNU implementation of that tool. But I noticed at that time the OpenBSD implementation was correctly taking into account the LC_CTYPE locale setting to identify multibyte characters. Out of curiosity, I’ve tested the paste command on OpenBSD too. Alas, with the same result as on my Debian box this time, despite the specifications for the paste utility mentioning the LC_CTYPE environment variable as determining ” the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files)”. From my experience, all the major implementations of the paste utility currently ignore multi-byte characters in the delimiter list and assume one-byte separators. But I will not claim having tested that for the whole variety of the *nix platforms. So if I missed something here, don’t hesitate to use the comment section to correct me!

Bonus Tip: Avoiding the \0 pitfall

For historical reasons:

are not necessarily equivalent; the latter is not specified by this volume of IEEE Std 1003.1-2001 and may result in an error. The construct ‘\0’ is used to mean “no separator” because historical versions of paste did not follow the syntax guidelines, and the command:

could not be handled properly by getopt().

So, the portable way of pasting files without using a delimiter is by specifying the \0 delimiter. This is somewhat counterintuitive since, for many commands, \0 means the NUL character–a character encoded as a byte made only of zeros that should not clash with any text content.

You might find the NUL character an useful separator especially when your data may contain arbitrary characters (like when working with file names or user-provided data). Unfortunately, I’m not aware of any way to use the NUL character as the field delimiter with the paste command. But maybe do you know how to do that? If that’s the case, I would be more than happy to read your solution in the command section.

On the other hand, the paste implementation part of the GNU Coreutils has the non-standard -z option to switch from the newline to the NUL character for the line separator. But in that case, the NUL character will be used as line separator both for the input and output. So, to test that feature, we need first a zero-terminated version of our input files:

To see what has changed in the process, we can use the hexdump utility to examine the raw binary content of the files:

I will let you compare by yourself the two hex dumps above to identify the difference between “.zero” files and the original text files. As a hint, I can tell you a newline is encoded as the 0a byte.

Hopefully, you took the time needed to locate the NUL character in the “.zero” input files. Anyway, we have now a zero-terminated version of the input files, so we can use the -z option of the paste command to handle those data, producing in the output as well a zero-terminated result:

Since my input files do not contain embedded newlines in the data, the -z option is of limited usefulness here. But based on the explanations above, I let you try to understand why the following example is working “as expected”. To fully understand that you probably need to download the sample files and examine them at byte level using the hexdump utility as we did above:

What’s more?

The paste command produces only delimited text output. But as illustrated at the end of the introductory video, if your system does support the BSD column utility, you can use it to obtain nicely formatted tables by converting the paste command output to a fixed-width text format. But that will be the subject of an upcoming article. So stay tuned, and as always, don’t forget to share that article on your favorite websites and social media!

Источник

Читайте также:  Отказ доступа windows 10
Оцените статью