From paste(1) man pages:
paste - merge lines of files
Re-discussing some of the usual uses that I posted in my previous post
$ cat f1.txt
a
b
c
$ cat f2.txt
1
2
Now:
$ paste f1.txt f2.txt
a 1
b 2
c
$ paste f2.txt f1.txt
1 a
2 b
c
Using -d (delimiter) and -s (serial) options:
$ paste -s -d : f1.txt f2.txt
a:b:c
1:2
Now some additional examples:
Example 1:
We can specify a delimiters list instead of a single delimiter with -d option.
Suppose you have 3 files fa.txt, fb.txt and fc.txt.
$ cat fa.txt
20
60
90
12
$ cat fb.txt
60
90
12
14
$ cat fc.txt
70
90
80
12
Required:
The requirement is to add the individual lines from fa.txt with fb.txt and then subtracting the corresponding line from fc.txt.
Lets construct the expression first:
$ paste -d"+-" fa.txt fb.txt fc.txt
20+60-70
60+90-90
90+12-80
12+14-12
Then use bc to evaluate each line of expression like this:
$ paste -d"+-" fa.txt fb.txt fc.txt| while read line
do
echo -n "($line)="
echo $line | bc
done
Output:
(20+60-70)=10
(60+90-90)=60
(90+12-80)=22
(12+14-12)=14
Awk will be more simple in this case:
$ paste fa.txt fb.txt fc.txt | awk '{print "("$1"+"$2"-"$3")="$1+$2-$3}'
Output:
(20+60-70)=10
(60+90-90)=60
(90+12-80)=22
(12+14-12)=14
Things to learn from above example:
1) -n option with echo (do not output the trailing newline)
2) Specifying a delimiters list with -d option in paste
Example 2:
Input file: element.txt has the element name followed by the rpm in the following format.
$ cat element.txt
Element E1:
rpm=2300
Element E5:
rpm=8900
Element E3:
rpm=5000
Element E4:
rpm=1200
Required: Only list the elements which have rpm > 3000
$ paste - - < element.txt | awk '$NF>=3000 {print $1}' FS=[:,=]
Element E5
Element E3
Things to learn from above example:
1) Specifying multiple field separator (FS) with awk, my previous post on that
2) paste - - ( see example 3 below for more clarification)
Example 3: Again one more example using paste - -
Input file: details.txt has some records like this.
$ cat details.txt
Jweis
1982
awk
Lalit
1983
sed
Annie
1983
Perl
Required: We need to make a line out of 3 rows from the above file. i.e. merging 3 rows to 1 row
$ < details.txt paste - - -
Jweis 1982 awk
Lalit 1983 sed
Annie 1983 Perl
Awk ?: is also can be used here.
$ awk '{printf("%s",NR%3 ? $0"\t":$0"\n")}' details.txt
Jweis 1982 awk
Lalit 1983 sed
Annie 1983 Perl
Things to learn from above example:
1) awk ?: operator: If the first pattern is true then the pattern used for testing is the second pattern, otherwise it is the third. Only one of the second and third patterns is evaluated.
Few more posts based on Linux paste command
- Replace column with column of another file using awk click
- Merge alternate lines of files using awk click
- Sequence subtraction one liner click
2 comments:
what if this is scenario:
$ cat Delta_Folder/1.properties
account.org.com.email=New-Email
account.value.range=True
account.comment.box=Hello There
$ cat Original_Folder/1.properties
#this is a comment1
account.org.com.email=Old-Email
account.value.range=False
range.list.type=String
currency.country=Sweden
#this is a comment2
account.comment.box=Howdy
Output:
#this is a comment1
account.org.com.email=New-Email
account.value.range=True
range.list.type=String
currency.country=Sweden
#this is a comment2
account.comment.box=Hello There
Thanks for providing the good examples.
For more examples: paste command examples in unix
Post a Comment