diff腳本問題及使用IFS解決問題實際案例

說明:本文來自老男孩教育23期第三週課程李想同學的問題及老男孩的解答後學生的總結。

   

在整理diff命令的時候,突然奇想diff是比較兩個文件不同。那我能不能通過diff把兩個文件不同之處比較出來並打印並指明是來自那個文件?

下面是我創建兩個文件file1file2,其內容如下

[root@lx data]# cat file1
I am oldboy,myqq is 31333741
1234
fdsadas
fdafdggfs
kio323423
543rsgrsq
 
[root@lx data]# cat file2
fdsadas
34fdafdggfs
kio323423

 

下面是我寫的一個簡單的腳本

#!/bin/bash
cd /root/data
file1=(`diff -a file1 file2|grep'^<'|sed -r 's/^< (.*)/\1/g'`)
file2=(`diff -a file1 file2|grep'^>'|sed -r 's/^> (.*)/\1/g'`)
for m in ${file1[*]}
do
       echo "the '$m' is from file1"
done
for n in ${file2[*]}
do
       echo "the '$n' is from file2"
done

其打印結果如下

[root@lx data]# sh /shell/2.sh           
the 'I' is from file1
the 'am' is from file1
the 'oldboy,myqq' is from file1
the 'is' is from file1
the '31333741' is from file1
the '1234' is from file1
the 'fdafdggfs' is from file1
the '543rsgrsq' is from file1
the '34fdafdggfs' is from file2

此腳本的有一個bug,我想要的是I am oldboy,myqq is 31333741作爲一個整體來輸出,而不是分個輸出

 

student:於是請教老師怎樣解決這個問題?

oldboy:老男孩老師給出了 IFS=$'\n'這個參數

#!/bin/bash
cd /root/data
IFS=$'\n'   #IFS='\n'
file1=(`diff -a file1 file2|grep '^<'|sed -r 's/^<(.*)/\1/g'`)
file2=(`diff -a file1 file2|grep '^>'|sed -r 's/^>(.*)/\1/g'`)
for m in ${file1[*]}
do
        echo"the '$m' is from file1"
done
for n in ${file2[@]}
do
        echo"the '$n' is from file2"
done

打印輸出結果爲:

[root@lx data]# sh /shell/2.sh 
the 'I am oldboy,myqq is 31333741' is from file1
the '1234' is from file1
the 'fdafdggfs' is from file1
the '543rsgrsq' is from file1
the '34fdafdggfs' is from file2
 
[root@lx data]# sh /shell/2.sh
the 'I am oldboy,myqq is 31333741
1234
fdafdggfs
543rsgrsq' is from file1
the '34fdafdggfs' is from file2

 

總結:

查看了下IFS,瞭解到IFS是內部域分隔符;此題IFS=$'\n'默認以換行作爲分隔符; IFS='\n',是以n字符作爲分隔

1. IFS的默認值爲:空白(包括:空格,tab, 和新行),將其ASSII碼用十六進制打印出來就是:20 09 0a (見下面的shell腳本)。

2. IFS對空格的空白的處理和其他字符不一樣,左右兩半的純空白會被忽略,多個連續的空白被當成一個IFS處理。

3. S*中使用IFS中的第一個字符。

4. awk中的FS(域分隔符)也和IFS有類似的用法和作用

 

老男孩老師點評:此問題略微有些偏門,不過了解下也是有好處的。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章