当前位置:首页 » 网页前端 » shell脚本输入两个文件
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

shell脚本输入两个文件

发布时间: 2022-04-14 12:05:13

Ⅰ 用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