Shell脚本来处理文件

Shell脚本来处理文件

本文介绍了Shell脚本来处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个Shell脚本来处理一个包含近20个关卡的巨大文件夹。我必须处理每一个文件,并检查哪些文件包含像
这样的行。select
insert
update

当我的意思是行应该采取行,直到我在该文件中找到一个分号。
我应该得到像这样的结果

pre code $ C $ / :/test.java select * from test $ b $ C C:/test1.java select * from tester
C:/test1.java select * from dual

等等。现在我有一个脚本来读取所有文件

 #!bin / ksh 

FILE =<要遍历的FILEPATH>
TEMPFILE =<临时文件的位置>
cd $ FILE
for find in。 !型d';
do
cat $ FILE / addedText.txt>> $ TEMPFILE / newFile.txt
cat $ f>> $ TEMPFILE / newFile.txt
rm $ f
cat $ TEMPFILE / newFile.txt>> $ f
rm $ TEMPFILE / newFile.txt
完成

我对awk和sed知之甚少,要进一步阅读每个文件并达到我想要的目标。任何人都可以在这里帮助我

解决方案

如果您有GNU find / gawk

  find / path -type f -name* .java| while read -r FILE 
do
awk -vfile =$ FILEBEGIN {RS =;}
/ select | update | insert / {
b = gensub( /(.*)(select |update|insert)(.*)/,\"\\2\\3\",\"g\"$$0)
gsub(/ \\\
+ /, ,b)
打印文件,b
}
'$ FILE

完成

如果你在Solaris上,使用nawk
$ b $ pre $ find $ /键入f -nametest * file| while read -r FILE
do
nawk -v file =$ FILE'BEGIN {RS =;}
/ select / {gsub(/.* select /,select ); gsub(/ \ n + /,);打印文件,$ 0; }
/ update / {gsub(/.* update /,update); gsub(/ \ n + /,);打印文件$ 0; }
/ insert / {gsub(/.* insert /,insert); gsub(/ \ n + /,); print file,$ 0; }
'$ FILE
完成

注意这是简单的情况。你的SQL语句可能更复杂。

I need to write a Shell Script to process a huge folder of nearly 20 levels.I have to process each and every file and check which files contain lines likeselectinsertupdate

When I mean line it should take the line till I find a semicolon in that file.I should get a result like this

C:/test.java   select * from dual
C:/test.java   select * from test
C:/test1.java  select * from tester
C:/test1.java  select * from dual

and so on.Right now I have a script to read all the files

#!bin/ksh

FILE=<FILEPATH to be traversed>
TEMPFILE=<Location of Temp file>
cd $FILE
for f in `find . ! -type d`;
do
cat $FILE/addedText.txt>>$TEMPFILE/newFile.txt
cat $f>>$TEMPFILE/newFile.txt
rm $f
cat $TEMPFILE/newFile.txt>>$f
rm $TEMPFILE/newFile.txt
done

I have very little knowledge of awk and sed to proceed further in reading each file and achieve what I want to.Can anyone help me in this

解决方案

if you have GNU find/gawk

find /path -type f -name "*.java" | while read -r FILE
do
  awk -vfile="$FILE" 'BEGIN{RS=";"}
  /select|update|insert/{
     b=gensub(/(.*)(select|update|insert)(.*)/,"\\2\\3","g",$0)
     gsub(/\n+/,"",b)
     print file,b
  }
  ' "$FILE"

done

if you are on Solaris, use nawk

find /path -type f -name "test*file" | while read -r FILE
do
  nawk -v file="$FILE" 'BEGIN{RS=";"}
  /select/{ gsub(/.*select/,"select");gsub(/\n+/,"");print file,$0; }
  /update/{ gsub(/.*update/,"update");gsub(/\n+/,"");print file,$0; }
  /insert/{ gsub(/.*insert/,"insert");gsub(/\n+/,"");print file,$0; }
  ' "$FILE"
done

Note this is simplistic case. your SQL statement might be more complicated.

这篇关于Shell脚本来处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:13