Merge files into columns - text

Let's say I have three files with text data:
file1
A
B
C
file2
1
2
3
file3
alpha
beta
gamma
Now I want to get one FILE with following format:
A 1 alpha
B 2 beta
C 3 gamma
How can I do it with bash commands?

Very easy command to look into on unix/linux machines is the paste command.
paste file1 file2 file3

Related

comm command not comparing words

I am trying to learn shell programming and for that I am using ubuntu app in windows 10, I read about the comm command and as I have understood it it should be working as below
file1.txt file2.txt
abc abc
cde efg
a b
b c
the result should be
a
cde
abc
b
c
efg
but what I am getting is
abc
a
cde
b
efg
abc
c
this is how I used the command
comm file1.txt file2.txt
I suspect its because I am using it on a windows app but other commands such as grep uniq ps pwd ... all are working fine
Any help would be appreciated
Windows is not the problem here. You used comm in the wrong way. man comm states
comm - compare two sorted files line by line
Therefore, you have to sort both files first.
Use
sort file1.txt > file1sorted
sort file2.txt > file2sorted
comm file1sorted file2sorted
Or if you are using bash (not plain sh or some other shell)
comm <(sort file1.txt) <(sort file2.txt)

Merging files in reverse

I am working on the logs, they are in multiple number.
lets assume the following files has the content
file1
1
file2
2
file3
3
by using the command cat file* the result would be
1
2
3
but i am looking for some thing , while i use the regex/command using file* i want the output to be some thing like this.
3
2
1
could some one help me please.
Pass the output of cat to tac :
$ cat file*
1
2
3
$ cat file* | tac
3
2
1
You may call
ls -1r file* | xargs cat
in order to specify the order of the files. Its output is different from the tac solution, since each single logfile is in the correct order. (Perhaps this is not even the desired output).

How to select uncommon rows from two large files using linux (in terminal)?

Both have two columns: names and IDs.(files are in xls or txt format)
File 1:
AAA K0125
ccc K0234
BMN_a K0567
BMN_c K0567
File 2:
AKP K0897
BMN_a K0567
ccc K0234
I want to print uncommon rows using these two files.
how can it be done using linux terminal.
Try something like this:-
join "-t " -j 1 -v 1 file1 file2
Considering the two files are sorted.
First sort both the files and then use comm utility with -3 option
sort file1 > file1_sorted
sort file2 > file2_sorted
comm -3 file1_sorted file2_sorted
A portion from man comm
-3 suppress column 3 (lines that appear in both files)
Output:
AAA K0125
AKP K0897
BMN_c K0567

How to display only different rows using diff (bash)

How can I display only different rows using diff in a separate file?
For example, the file number 1 contains the line:
1;john;125;3
1;tom;56;2
2;jack;10;5
A file number 2 contains the following lines:
1;john;125;3
1;tom;58;2
2;jack;10;5
How to make in the following happen?
1;tom;58;2
a.txt:
1;john;125;3
1;tom;56;2
2;jack;10;5
b.txt:
1;john;125;3
1;tom;58;2
2;jack;10;5
Use comm:
comm -13 a.txt b.txt
1;tom;58;2
The command line options to comm are pretty straight-forward:
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
Here's a simple solution that I think is better than diff:
sort file1 file2 | uniq -u
sort file1 file2 concatenates the two files and sorts it
uniq -u prints the unique lines (that do not repeat). It requires the input to be pre-sorted.
Assuming you want to retain only the lines unique to file 2 you can do:
comm -13 file1 file2
Note that the comm command expects the two files to be in sorted order.
Using group format specifiers you can suppress printing of unchanged lines and print only changed lines for changed
diff --changed-group-format="%>" --unchanged-group-format="" file1 file2

How to merge two single column csv files with linux commands

I was wondering how to merge two single column csv files into one file where the resulting file will contain two columns.
file1.csv
first_name
chris
ben
jerry
file2.csv
last_name
smith
white
perry
result.csv
first_name,last_name
chris,smith
ben,white
jerry,perry
Thanks
$ cat file1
John
Mary
$ cat file2
Smith
Jones
$ paste -d, file1 file2
John,Smith
Mary,Jones
The -d, argument is used to designate commas as the delimiter between columns
You're looking for paste.

Resources