检查多行并输出缺少的行。 (击)

时间:2013-03-27 18:49:03

标签: linux bash shell scripting

所以基本上我有一个文件,为了理解如何写这个,看起来像这样。

Start:
       First line of text
       Second line of text
Bin:
       Third line of text
       Four  line of text 

我需要完成的是编写一个检查这些字符串的脚本,并输出任何缺少的字符串。

基于我的假设,我认为这将涉及一个awk或grep,它将检查每个字符串和一组if语句是否会说如果不存在则输出不存在的字符串。

关于如何开始这个的任何指示? 这是我迄今为止尝试过的,那就是很少的伪代码。 `

 str1=$(awk '/Start:/' /some/file)
 str2=$(awk '/First line of text/' /some/file) 
 str3=$(awk '/Second line of text/' /some/file)

if $str1 == '' then
   print $str1 'does not exist'
elif $str2 == '' then
   print $str2 'does not exist'
else $str3 == '' then 
   print $str3 'does not exist'
fi`    `

1 个答案:

答案 0 :(得分:1)

这样的东西应该用AWK打印缺少的字符串:

BEGIN {
 a[i++]="Start:"
 a[i++]="First line of text"
 a[i++]="Second line of text"
}

// {
 for (s in a) {
  if (match($0,a[s]) ) { a[s]="" }
 }
}

END {
 for (s in a) {
  if (a[s] != "") { print a[s] }
 }
}
相关问题