由于此线程的主要目标是如何在NON GNU中进行SAVE的存储,awk
因此我首先发布其模板,该模板将对任何要求的人有所帮助,因此他们需要在代码中添加/附加BEGIN
和END
分段,以保持其主BLOCK不变。要求,然后应进行就地编辑:
注意:以下命令会将其所有输出写入output_file,因此,如果要将任何内容打印到标准输出,请仅添加print...
语句,而不要> (out)
在后面添加。
通用模板:
awk -v out_file="out" '
FNR==1{
close(out)
out=out_file count++
rename=(rename?rename ORS:"") "mv \047" out "\047 \047" FILENAME "\047"
}
{
.....your main block code.....
}
END{
if(rename){
system(rename)
}
}
' *.txt
具体提供的样品解决方案:
我自己想出了以下方法awk
(对于添加的示例,以下是我解决此问题并将输出保存到Input_file本身的方法)
awk -v out_file="out" '
FNR==1{
close(out)
out=out_file count++
rename=(rename?rename ORS:"") "mv \047" out "\047 \047" FILENAME "\047"
}
{
print FNR > (out)
}
END{
if(rename){
system(rename)
}
}
' *.txt
注意:这仅是一种用于将编辑后的输出保存到Input_file本身的测试,可以在程序中使用其BEGIN部分以及END部分,主要部分应根据特定问题本身的要求。
合理的警告:此外,由于此方法会在路径中创建一个新的临时out文件,因此最好确保我们在系统上有足够的空间,尽管最终结果将仅保留主要的Input_file,但在操作过程中它需要系统/目录上的空间
以下是上面代码的测试。
举例说明程序的执行:假设以下是.txt
Input_file:
cat << EOF > test1.txt
onetwo three
tets testtest
EOF
cat << EOF > test2.txt
onetwo three
tets testtest
EOF
cat << EOF > test3.txt
onetwo three
tets testtest
EOF
现在,当我们运行以下代码时:
awk -v out_file="out" '
FNR==1{
close(out)
out=out_file count++
rename=(rename?rename ORS:"") "mv \047" out "\047 \047" FILENAME "\047"
}
{
print "new_lines_here...." > (out)
}
END{
if(rename){
system("ls -lhtr;" rename)
}
}
' *.txt
注意:我ls -lhtr
在此system
部分中有意查看它正在创建的输出文件(临时基础),因为稍后它将把它们重命名为实际名称。
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test2.txt
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test1.txt
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test3.txt
-rw-r--r-- 1 runner runner 38 Dec 9 05:33 out2
-rw-r--r-- 1 runner runner 38 Dec 9 05:33 out1
-rw-r--r-- 1 runner runner 38 Dec 9 05:33 out0
当我们 完成运行ls -lhtr
后awk
脚本后,我们只能.txt
在其中看到文件。
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test2.txt
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test1.txt
-rw-r--r-- 1 runner runner 27 Dec 9 05:33 test3.txt
说明:在此处添加上述命令的详细说明:
awk -v out_file="out" ' ##Starting awk program from here, creating a variable named out_file whose value SHOULD BE a name of files which are NOT present in our current directory. Basically by this name temporary files will be created which will be later renamed to actual files.
FNR==1{ ##Checking condition if this is very first line of current Input_file then do following.
close(out) ##Using close function of awk here, because we are putting output to temp files and then renaming them so making sure that we shouldn't get too many files opened error by CLOSING it.
out=out_file count++ ##Creating out variable here, whose value is value of variable out_file(defined in awk -v section) then variable count whose value will be keep increment with 1 whenever cursor comes here.
rename=(rename?rename ORS:"") "mv \047" out "\047 \047" FILENAME "\047" ##Creating a variable named rename, whose work is to execute commands(rename ones) once we are done with processing all the Input_file(s), this will be executed in END section.
} ##Closing BLOCK for FNR==1 condition here.
{ ##Starting main BLOCK from here.
print "new_lines_here...." > (out) ##Doing printing in this example to out file.
} ##Closing main BLOCK here.
END{ ##Starting END block for this specific program here.
if(rename){ ##Checking condition if rename variable is NOT NULL then do following.
system(rename) ##Using system command and placing renme variable inside which will actually execute mv commands to rename files from out01 etc to Input_file etc.
}
} ##Closing END block of this program here.
' *.txt ##Mentioning Input_file(s) with their extensions here.