I am a new to Linux and try to write a script to join three multiple files.
For example, there are three files
file1
# comment
a Kevin
b Vin
c Sam
file 2
# comment
a 10
b 20
c 40
file 3
# comment
a blue
b yellow
c red
I am trying to ignore the line starting with #, and join only three lines. but because of the comment I cannot use join function.
Can anybody help me??
The "cat" command concatenates multiple files.
The comment can be removed in a variety of ways, one of which is by using sed.
$
$ cat file1
# comment
a Kevin
b Vin
c Sam
$
$ cat file2
# comment
a 10
b 20
c 40
$
$ cat file3
# comment
a blue
b yellow
c red
$
$ cat file1 file2 file3
# comment
a Kevin
b Vin
c Sam
# comment
a 10
b 20
c 40
# comment
a blue
b yellow
c red
$
$ cat file1 file2 file3 | sed '/^#/d'
a Kevin
b Vin
c Sam
a 10
b 20
c 40
a blue
b yellow
c red
$
$
If you were going to use the join command it would be a two step operation. The awk solution that Devaraj provided is much better.
echo "
# comment
a Kevin
b Vin
c Sam
" > test1
echo "
# comment
a 10
b 20
c 40
" > test2
echo "
# comment
a blue
b yellow
c red
" > test3
join -1 1 -2 1 -t" " -o "1.1,1.2,2.2" test1 test2 > test4
join -1 1 -2 1 -t" " -o "1.1,1.2,1.3,2.2" test4 test3 | sed '/^#/d'