Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basic shell programming

Tags:

bash

shell

awk

Probably this is a very basic question for shell programmers. But suppose I have a text file A and B and B is a subset of A.

I want to create a text file C which contains (A-B) data.

So omit all the common lines .

The line in files are numeric data: like

id , some aspect, other aspec.

Thanks.

like image 760
frazman Avatar asked Apr 26 '12 21:04

frazman


People also ask

Is shell script easy to learn?

Working with command line shell is bit difficult for the beginners because it's hard to memorize so many commands. It is very powerful, it allows user to store commands in a file and execute them together.

What is shell programming in OS?

Your interface to the operating system is called a shell. The shell is the outermost layer of the operating system. Shells incorporate a programming language to control processes and files, as well as to start and control other programs.


1 Answers

Use sort and uniq

sort a b | uniq -u

If you want the lines that are the same between A and B, you can use uniq -d

sort a b | uniq -d

This assumes of course that the data in A and B are exactly the same. There cannot be any lose spaces or tabs in the datasets. If there are, you'll have to clean up the data with sed, tr, or awk first.

Edit

As Peter. O pointed out, this will fail if there happen to be exact duplicates in file a. If that's an issue, you can fix it by doing this:

sort <(sort -u a) b | uniq -u
like image 141
Tim Pote Avatar answered Sep 18 '22 16:09

Tim Pote