使用 echo 从文件中 awk 并输出到文件
awk from file using echo and output to file
A.txt 包含:
/*333*/
asdfasdfadfg
sadfasdfasgadas
@@@
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
@@@
/*777*/
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
@@@
B.txt 包含:
555
777
我想为在 B.txt 中找到的每个字符串创建循环,然后输出 '/*'[字符串] 直到第一个 '@@@' 遇到每个自己的文件之前(字符串名称也用作文件名)。
所以根据上面的例子,结果应该是:
555.txt,其中包含:
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
和777.txt,其中包含:
/*777*/
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
我试过这个脚本,但它什么也没输出:
for i in `cat B.txt`; do echo $i | awk '/{print "/*"}/{flag=1} /@@@/{flag=0} flag' A.txt > $i.txt; done
提前致谢
对您的代码进行一些改动,即可使用所提供的示例数据获得所需的结果:
while read -r f
do
awk -v var="/[*]$f[*]/" '[=10=] ~ var {flag=1} /@@@/{flag=0} flag' A.txt > "$f".txt
done < B.txt
cat 555.txt
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
cat 777.txt
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
这是否解决了您的问题?
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
中编写和测试应该在任何 awk
.
中工作
awk '
FNR==NR{
if([=10=]~/^\/\*/){
line=[=10=]
gsub(/^\/\*|\*\/$/,"",line)
arr[++count]=[=10=]
arr1[line]=count
next
}
arr[count]=(arr[count]?arr[count] ORS:"") [=10=]
next
}
([=10=] in arr1){
outputFile=[=10=]".txt"
print arr[arr1[[=10=]]] >> (outputFile)
close(outputFile)
}
' file1 file2
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when file1 is being read.
if([=11=]~/^\/\*/){ ##Checking condition if current line starts with /* then do following.
line=[=11=] ##Setting [=11=] to line variable here.
gsub(/^\/\*|\*\/$/,"",line) ##using gsub to globally substitute starting /* and ending */ with NULL in line here.
arr[++count]=[=11=] ##Creating arr with index of ++count and value is [=11=].
arr1[line]=count ##Creating arr1 with index of line and value of count.
next ##next will skip all further statements from here.
}
arr[count]=(arr[count]?arr[count] ORS:"") [=11=] ##Creating arr with index of count and keep appending values of same count values with current line value.
next ##next will skip all further statements from here.
}
([=11=] in arr1){ ##checking if current line is present in arr1 then do following.
outputFile=[=11=]".txt" ##Creating outputFile with current line .txt value here.
print arr[arr1[[=11=]]] >> (outputFile) ##Printing arr value with index of arr1[[=11=]] to outputFile.
close(outputFile) ##Closing outputFile in backend to avoid too many opened files error.
}
' file1 file2 ##Mentioning Input_file names here.
这是另一个 awk
解决方案:
awk '
FNR == NR {
map["/*" [=10=] "*/"] = [=10=]
next
}
[=10=] in map {
fn = map[[=10=]] ".txt"
}
/^@@@$/ {
close(fn)
fn = ""
}
fn {print > fn}' B.txt A.txt
A.txt 包含:
/*333*/
asdfasdfadfg
sadfasdfasgadas
@@@
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
@@@
/*777*/
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
@@@
B.txt 包含:
555
777
我想为在 B.txt 中找到的每个字符串创建循环,然后输出 '/*'[字符串] 直到第一个 '@@@' 遇到每个自己的文件之前(字符串名称也用作文件名)。 所以根据上面的例子,结果应该是:
555.txt,其中包含:
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
和777.txt,其中包含:
/*777*/
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
我试过这个脚本,但它什么也没输出:
for i in `cat B.txt`; do echo $i | awk '/{print "/*"}/{flag=1} /@@@/{flag=0} flag' A.txt > $i.txt; done
提前致谢
对您的代码进行一些改动,即可使用所提供的示例数据获得所需的结果:
while read -r f
do
awk -v var="/[*]$f[*]/" '[=10=] ~ var {flag=1} /@@@/{flag=0} flag' A.txt > "$f".txt
done < B.txt
cat 555.txt
/*555*/
hfawehfihohawe
aweihfiwahif
aiwehfwwh
cat 777.txt
jawejfiawjia
ajwiejfjeiie
eiuehhawefjj
这是否解决了您的问题?
使用您显示的示例,请尝试以下 awk
代码。在 GNU awk
中编写和测试应该在任何 awk
.
awk '
FNR==NR{
if([=10=]~/^\/\*/){
line=[=10=]
gsub(/^\/\*|\*\/$/,"",line)
arr[++count]=[=10=]
arr1[line]=count
next
}
arr[count]=(arr[count]?arr[count] ORS:"") [=10=]
next
}
([=10=] in arr1){
outputFile=[=10=]".txt"
print arr[arr1[[=10=]]] >> (outputFile)
close(outputFile)
}
' file1 file2
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when file1 is being read.
if([=11=]~/^\/\*/){ ##Checking condition if current line starts with /* then do following.
line=[=11=] ##Setting [=11=] to line variable here.
gsub(/^\/\*|\*\/$/,"",line) ##using gsub to globally substitute starting /* and ending */ with NULL in line here.
arr[++count]=[=11=] ##Creating arr with index of ++count and value is [=11=].
arr1[line]=count ##Creating arr1 with index of line and value of count.
next ##next will skip all further statements from here.
}
arr[count]=(arr[count]?arr[count] ORS:"") [=11=] ##Creating arr with index of count and keep appending values of same count values with current line value.
next ##next will skip all further statements from here.
}
([=11=] in arr1){ ##checking if current line is present in arr1 then do following.
outputFile=[=11=]".txt" ##Creating outputFile with current line .txt value here.
print arr[arr1[[=11=]]] >> (outputFile) ##Printing arr value with index of arr1[[=11=]] to outputFile.
close(outputFile) ##Closing outputFile in backend to avoid too many opened files error.
}
' file1 file2 ##Mentioning Input_file names here.
这是另一个 awk
解决方案:
awk '
FNR == NR {
map["/*" [=10=] "*/"] = [=10=]
next
}
[=10=] in map {
fn = map[[=10=]] ".txt"
}
/^@@@$/ {
close(fn)
fn = ""
}
fn {print > fn}' B.txt A.txt