简体   繁体   English

Unix Shell脚本

[英]Unix Shell Scripting

I've a situation, where I've a source file and a target file, supposed to be similar. 我遇到的情况是,我有一个源文件和一个目标文件,应该是相似的。 The source file has sets of information like below. 源文件具有如下信息集。 I need to compare each set of information in Source and Target files. 我需要比较源文件和目标文件中的每组信息。 If anything's missing in Target file, I need to write the line into the target file. 如果目标文件中缺少任何内容,则需要将该行写入目标文件中。 I could have matched line by line, but the sets of Source and target files are not in order. 我本来可以逐行匹配的,但是源文件和目标文件的集合却没有顺序。 Please help me how to achieve using a shell script. 请帮助我如何使用shell脚本实现。

Source file: 源文件:

[Set.1]
value=1
date=today

[Set.2]
value=2
date=today

Target File: 目标文件:

[Set.2]
value=2

[Set.1]
value=1
date=today

If you can see, date is missing in [Set.2] of target file, so, I need to copy it from Source File. 如果可以看到,目标文件的[Set.2]中缺少日期,因此,我需要从源文件中复制它。

如果file_set1中存在file_set2中缺少的任何内容,将通过file_set2和file_set1下面的命令进行比较,然后将其复制回file_set2。

awk 'FNR==NR{a[$0];next}!($0 in a)' file_set2 file_set1 >>file_set2

你可以尝试使用rsync

rsync /target/file/path /source/file/path 

This is either a very easy problem or a hard one. 这要么是一个非常容易的问题,要么是一个难题。 If you just need to make the files identical then 如果您只需要使文件相同,则

cp source_file target_file

would be enough. 足够了。 But it seems like you want to keep the ordering of sections in target_file unchanged. 但是似乎您想保持target_file中节的顺序不变。 This will require you to parse the files first and remember the order of the sections. 这将需要您首先解析文件记住各节的顺序。

Since this is Windows INI file format, the ordering of sections should not be significant to the program reading it. 由于这是Windows INI文件格式,因此各节的顺序对于读取该文件的程序而言并不重要。 Check to make sure whether the reader actually cares. 检查以确保读者是否真正在乎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM