int main(int argc, char **argv)
{
...
/*system中不支持使用格式符%s,%d等,因此使用sprintf()先使用格式符转换,再将转换后的字符串存放在buffer中*/
snprintf(buffer, sizeof(buffer), "cat '%s' | tr -d '\r''\n' > temp_logo.txt",argv[1]);
system(buffer);/*复制输入源文件到trmp_logo.txt*/
system("sed -i 's/0x//g' temp_logo.txt "); /*删除文件中的0x字符*/
system("sed -i 's/\ //g' temp_logo.txt ");/*删除文件中的空格*/
system("sed -i 's/\,//g' temp_logo.txt ");/*删除文件中的","*/
system("sed -i 's/\n//g' temp_logo.txt ");/*删除文件中的换行符*/
system("sed -i 's/\t//g' temp_logo.txt ");/*删除文件中的Tab键*/
...
}
本文介绍了一种通过shell脚本结合sed命令和system函数,从输入源文件中去除特定字符(如0x、空格、逗号、换行符、Tab键)的方法,最终将净化后的数据保存到temp_logo.txt文件中。此过程适用于需要预处理文本数据的场景,例如,当源文件包含不必要的格式字符或需要统一格式时。
2308

被折叠的 条评论
为什么被折叠?



