From: Hongyi Zhao on
Hi all,

I use the following code to obtain the lines existing file2 but not in
file1, and then store the results into file2 as follows:

awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 > file2

I've a question about the above operation: does the file2 will be
exposed to read/write conflict issue in this case? In detail, when we
redirect the result into file2, it also as the input file for the
awk's manipulation.

Any hints on this issue?
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Hermann Peifer on
Hongyi Zhao wrote:
> Hi all,
>
> I use the following code to obtain the lines existing file2 but not in
> file1, and then store the results into file2 as follows:
>
> awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 > file2
>
> I've a question about the above operation: does the file2 will be
> exposed to read/write conflict issue in this case?

Trying this out with some test files would have taken you less time than posting the question to a newsgroup.

Hermann
From: Chris F.A. Johnson on
On 2010-04-02, Hongyi Zhao wrote:
> Hi all,
>
> I use the following code to obtain the lines existing file2 but not in
> file1, and then store the results into file2 as follows:
>
> awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 > file2
>
> I've a question about the above operation: does the file2 will be
> exposed to read/write conflict issue in this case? In detail, when we
> redirect the result into file2, it also as the input file for the
> awk's manipulation.

'> file2' erases file2 before awk can read it.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Hongyi Zhao on
On 2 Apr 2010 08:36:22 GMT, "Chris F.A. Johnson"
<cfajohnson(a)gmail.com> wrote:

> '> file2' erases file2 before awk can read it.

Thanks a lot, but I find the following strange things:

1- If do the the following things:

$ echo aa > file1
$ echo bb > file2
$ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2
bb

$ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 > file2
$ cat file2

In this case, file2 will be empty in the end, i.e., '> file2' erases
file2 before awk can read it.

2- If do the the following things:

$ echo aa > file1
$ echo bb > file2
$ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2
bb

$ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 | sort -u > file2
$ cat file2
bb

This time, the operation will be finished successfully.

Any hints on this issue?
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: pk on
Hongyi Zhao wrote:

> 2- If do the the following things:
>
> $ echo aa > file1
> $ echo bb > file2
> $ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2
> bb
>
> $ awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 | sort -u > file2
> $ cat file2
> bb
>
> This time, the operation will be finished successfully.
>
> Any hints on this issue?

Luck.