Linux
paste
command- This article is about paste
command in Linux which is used to merge lines in a file. It is also useful in merging lines from multiple files as we will see shortly. The general syntax of this command is -Syntax :
paste <options> <file1> <file2> ...
To demonstrate this, we create a test file as below -
[root@LinuxBox paste_test]$ cat country.txt India America United Kingdom Australia Ukraine Brazil
1. Basic usage - No option
Basic usage of any command is when it is not used with any option. Here, we do not specify any option, but provide a file name as an argument to
paste
command and see what happens.[root@LinuxBox paste_test]$ paste country.txt India America United Kingdom Australia Ukraine Brazil
It just displays the file contents, very much same as the
cat
command.2. Read input from stdin with -
When we specify a
-
with paste
command, it reads lines from stdin and arranges them in one column. When we specify two dashes, it would read the lines arranging them in two columns and so on. So, if we specify only one -
, it would display the file contents as in previous case, similar to cat
command.# With piping [root@LinuxBox paste_test]$ cat country.txt | paste - India America United Kingdom Australia Ukraine Brazil # With I/O redirection [root@LinuxBox paste_test]$ paste - < country.txt India America United Kingdom Australia Ukraine Brazil
3. Arranging file contents in multiple columns - with multiple -
s
As mentioned earlier,
- -
will arrange the file contents in two columns, while - - -
will merge them in three columns. Lets check this.# Two columns [root@LinuxBox paste_test]$ cat country.txt | paste - - India America United Kingdom Australia Ukraine Brazil [root@LinuxBox paste_test]$ paste - - < country.txt India America United Kingdom Australia Ukraine Brazil # Three columns [root@LinuxBox paste_test]$ cat country.txt | paste - - - India America United Kingdom Australia Ukraine Brazil [root@LinuxBox paste_test]$ paste - - -< country.txt India America United Kingdom Australia Ukraine Brazil
4. Changing the delimiter - using -d
option
If you have observed, the default delimiter while arranging data in columns is whitespace. We can change this delimited using option
-d
and providing the character we wish to have as a delimiter as - paste -d <character> <file>
. Lets verify this on terminal.# Comma as a delimiter - 2 columns [root@LinuxBox paste_test]$ paste -d ',' - - < country.txt India,America United Kingdom,Australia Ukraine,Brazil # '|' as a delimiter - 3 columns [root@LinuxBox paste_test]$ paste -d '|' - - - < country.txt India|America|United Kingdom Australia|Ukraine|Brazil
As you can observe in second example, we have a common delimiter
|
that divides data into three columns. We can change the other delimiter by specifying two delimiters with option -d
as shown below:# First delimiter = '1', Second delimiter = '2' [root@LinuxBox paste_test]$ paste -d '12' - - - < country.txt India1America2United Kingdom Australia1Ukraine2Brazil # First delimiter = '|', Second delimiter = ':' [root@LinuxBox paste_test]$ paste -d '|:' - - - < country.txt India|America:United Kingdom Australia|Ukraine:Brazil
5. Merging files sequentially, one at a time - with -s
option
With option
-s
we can join all the lines in a file sequentially, one file at a time.[root@LinuxBox paste_test]$ paste -s country.txt India America United Kingdom Australia Ukraine Brazil
Lets try inserting delimiters into them.
[root@LinuxBox paste_test]$ paste -s -d ',' country.txt India,America,United Kingdom,Australia,Ukraine,Brazil
Now, we create another file
fruits.txt
as below:[root@LinuxBox paste_test]$ cat fruits.txt Apple Banana Orange Watermelon Guava Pineapple
And, lets put all things together and try joining two files with option
-s
and :
as a delimiter.[root@LinuxBox paste_test]$ paste -s -d ':' country.txt fruits.txt India:America:United Kingdom:Australia:Ukraine:Brazil Apple:Banana:Orange:Watermelon:Guava:Pineapple
6. Merge two files horizontally
As you might know, when we provide two files as arguments to
cat
command, it concatenates two files i.e. joins them vertically. But, when we provide same two files as arguments to paste
command, they are merged together horizontally. Lets compare both of these operations.# 'cat' command [root@LinuxBox paste_test]$ cat country.txt fruits.txt India America United Kingdom Australia Ukraine Brazil Apple Banana Orange Watermelon Guava Pineapple # 'paste' command [root@LinuxBox paste_test]$ paste country.txt fruits.txt India Apple America Banana United Kingdom Orange Australia Watermelon Ukraine Guava Brazil Pineapple
7. Reading lines from stdin, again - with - -
To again remind you of
- -
option, it takes input from stdin and merges the file in two columns.[root@LinuxBox paste_test]$ paste - fruits.txt < country.txt India Apple America Banana United Kingdom Orange Australia Watermelon Ukraine Guava Brazil Pineapple [root@LinuxBox paste_test]$ paste country.txt - < fruits.txt India Apple America Banana United Kingdom Orange Australia Watermelon Ukraine Guava Brazil Pineapple [root@LinuxBox paste_test]$ cat country.txt fruits.txt | paste - - India America United Kingdom Australia Ukraine Brazil Apple Banana Orange Watermelon Guava Pineapple # With delimiter [root@LinuxBox paste_test]$ cat country.txt fruits.txt| paste -d ':' - - India:America United Kingdom:Australia Ukraine:Brazil Apple:Banana Orange:Watermelon Guava:Pineapple
That's all. Thank you.
0 comments:
Post a Comment