Ⅰ 用shell腳本編寫一個函數,調用兩個數據文件的逐行數據(每一行有兩個數據)作為變數來計算,怎麼編寫
function fname(){
...
}
while read line
do
num1=`echo $line | awk '{print $1}'`
num2=`echo $line | awk '{print $2}'`
fname $num1 $num2
done < $file
Ⅱ 求一個linux的shell腳本,用命令行參數實現任意兩個文件的文件名互換
#!/bin/bash
file1=$1
mv $2 $2.bak
mv $1 $2
mv $2.bak $file1
Ⅲ 用shell腳本實現兩個文件比較
[root@localhostxly]#lla.txtb.txt
-rw-r--r--1rootroot28Jul1315:00a.txt
-rw-r--r--1rootroot30Jul1315:00b.txt
[root@localhostxly]#cata.txt
hello
123
thisisa.txt
456
[root@localhostxly]#catb.txt
hello!
123
thisisb.txt
4567
[root@localhostxly]#diffa.txtb.txt
1c1
<hello
---
>hello!
3,4c3,4
<thisisa.txt
<456
---
>thisisb.txt
>4567
1c1就是指2個文件第一行不一樣
<指的是a.txt
>指的是b.txt
Ⅳ shell腳本,一行一行比較兩個文本文件。 shell腳本,對一行一行讀取文本文件得到的那一行數據進行字元串
沒明白你的意思,就寫了一個判斷兩個文件對應行是不是相同。在線的話就追問我。
line=`cat
file1|wc
-l`
for
((i=1;i<=$line;i++));do
file1=`awk
'NR="'$i'"{print
$0}'`
file1
file2=`awk
'NR="'$i'"{print
$0}'`
file2
if
[
$file1
==
$file2
];then
echo
line
$i
looks
same!
else
echo
line
$i
looks
different!
fi
done
Ⅳ 3、編寫一個Shell腳本,從鍵盤上接收兩個文件名,如果兩個文件都存在則交換兩個文件的內容,否則應給出出
下面是代碼
#!/bin/bash
if [ -z $1 ]; then
echo "Input file 1 and file 2"
exit
fi
if [ -z $2 ]; then
echo "Input file 2"
exit
fi
if [ ! -f $1 ]; then
echo "$1 doesn't exist."
exit
fi
if [ ! -f $2 ]; then
echo "$2 doesn't exist."
exit
fi
mv $1 /tmp/diff.tmp
mv $2 $1
mv /tmp/diff.tmp $2
rm -rf /tmp/diff.tmp
echo "Finished change file"
下面是運行結果
[wyc@wyc-desktop:bash]$ echo 1.c > 1.c
[wyc@wyc-desktop:bash]$ echo 2.c > 2.c
[wyc@wyc-desktop:bash]$ cat 1.c
1.c
[wyc@wyc-desktop:bash]$ cat 2.c
2.c
[wyc@wyc-desktop:bash]$ ./diff.sh 1.c 2.c
Finished change file
[wyc@wyc-desktop:bash]$ cat 1.c
2.c
[wyc@wyc-desktop:bash]$ cat 2.c
1.c
Ⅵ 兩個文件夾 操作的 shell 腳本 需求,100高分在線等。滿意追加50分.
一(直接看結果、累加命名、關鍵在於找到最大值):
bsstest2:/billing/app/user/xufc/test$ls dir_A
a.pl ab.pl abc.pl
bsstest2:/billing/app/user/xufc/test$./rename.sh --第一次執行
a.pl ab.pl abc.pl_1
bsstest2:/billing/app/user/xufc/test$./rename.sh --第二次執行、新的文件沒有到達
ls: 0653-341 The file abc.pl does not exist.
新的 abc.pl 文件還未生成、退出
bsstest2:/billing/app/user/xufc/test$cd dir_A;touch abc.pl --創建新文件、等同於文件從另外一個地方生成了
bsstest2:/billing/app/user/xufc/test/dir_A$ls --創建文新文件後的目錄內容
a.pl ab.pl abc.pl abc.pl_1
bsstest2:/billing/app/user/xufc/test/dir_A$cd .. --退到上級目錄
bsstest2:/billing/app/user/xufc/test$./rename.sh --第2次執行
a.pl ab.pl abc.pl_1 abc.pl_2
bsstest2:/billing/app/user/xufc/test$cat ./rename.sh --具體腳本內容
#!/bin/ksh
dir=/billing/app/user/xufc/test/dir_A
cd $dir
if [ `ls abc.pl|wc -l` -eq 0 ]
then
echo "新的 abc.pl 文件還未生成、退出"
return 1
fi
cnt=`ls abc.*|wc -l|awk '{print $1}'`
mv abc.pl abc.pl_${cnt}
ls
bsstest2:/billing/app/user/xufc/test$
----------------------------------------------------------------
二:最小重命名(關鍵在於找到最小文件、利用awk比較數字)
bsstest2:/billing/app/user/xufc/test$ls dir_A --原始目錄內容
a.pl ab.pl abc.pl abc.pl_1 abc.pl_2 abc.pl_3 abc.pl_4 abc.pl_5 abc.pl_6
bsstest2:/billing/app/user/xufc/test$./recename.sh --第一次
a.pl ab.pl abc.pl abc.pl_2 abc.pl_3 abc.pl_4 abc.pl_5 abc.pl_6
bsstest2:/billing/app/user/xufc/test$./recename.sh --第二次
a.pl ab.pl abc.pl abc.pl_3 abc.pl_4 abc.pl_5 abc.pl_6
bsstest2:/billing/app/user/xufc/test$./recename.sh --第3次
a.pl ab.pl abc.pl abc.pl_4 abc.pl_5 abc.pl_6
bsstest2:/billing/app/user/xufc/test$cat ./recename.sh
#!/bin/ksh
dir=/billing/app/user/xufc/test/dir_A
cd $dir
if [ `ls abc.pl_*|wc -l` -eq 0 ]
then
echo "可以匹配的被修改的文件不存在、退出"
return 1
fi
#利用awk求取最小值
minnum=`ls abc.pl_*|awk -F\_ '{print $2}'|awk 'BEGIN {min = 1999999} {if ($1<min) min=$1 fi} END {print min}'`
mv abc.pl_${minnum} abc.pl
ls
bsstest2:/billing/app/user/xufc/test$
備註:其中dir目錄變數根據需要自行修改
Ⅶ shell腳本怎麼把內按照格式寫入到另一個文件里
1、用最簡單的cat 命令就可實現:cat b >> a (注意之間的空格) 2、還有用SED命令來實現,這有點復雜:sed '$ a\'$(cat b|awk '{print $0 "\\n"}'|tr -d '\n'|sed 's/\\n$//') a