使用Linux Bash脚本处理后,如何在CSV文件中删除一行
我使用Linux bash脚本在我的WordPress网站上使用WP-CLI创建帖子。 这是我使用的脚本的一个示例:
#! /bin/bash
while IFS="|" read -r rec_column1 rec_column2 rec_column3 rec_column4
do
cd /posts/ &&
wp post create \
--post_type=post \
--post_title="$rec_column1" \
--post_status=publish \
--post_category=2 \
--url=http://127.0.0.1/ \
--meta_input="{\"word\":\"$rec_column2\"}" \
--tags_input="$rec_column3" \
--post_content="$rec_column4"
done < <(tail -n +2 ./files/$1) > ./logs/$1.log
这是我所面临的数据的CSV文件的一个示例:
title|word|tags|content
Lorem|ipsum|dolor|sit
amet|consectetur|adipiscing|elit
Maecenas|sed|condimentum|est
in|fermentum|justo|Aenean
如果我的问题是,如果出于某种原因,我会遇到的问题,我会比拥有要查看日志文件以查看最后一个帖子创建的帖子ID是什么,然后我转到WordPress站点,请参阅最后一篇文章,在CSV文件中找到该行,请删除该行上方的所有行重新开始。
我想改进该过程,因此,在创建帖子后,删除了CSV文件中创建该帖子的行。 我将能够从停止的地方继续进行。
例如,如果在第一行之后停止了该过程
title|word|tags|content
amet|consectetur|adipiscing|elit
Maecenas|sed|condimentum|est
in|fermentum|justo|Aenean
因此,下次创建后过程中断时, 建议或代码修改如何像我想要的那样使此工作。
I use a Linux bash script to create posts on my WordPress website with wp-cli.
this is the example of the script that I use:
#! /bin/bash
while IFS="|" read -r rec_column1 rec_column2 rec_column3 rec_column4
do
cd /posts/ &&
wp post create \
--post_type=post \
--post_title="$rec_column1" \
--post_status=publish \
--post_category=2 \
--url=http://127.0.0.1/ \
--meta_input="{\"word\":\"$rec_column2\"}" \
--tags_input="$rec_column3" \
--post_content="$rec_column4"
done < <(tail -n +2 ./files/$1) > ./logs/$1.log
and this is an example of the csv file that I that the data from:
title|word|tags|content
Lorem|ipsum|dolor|sit
amet|consectetur|adipiscing|elit
Maecenas|sed|condimentum|est
in|fermentum|justo|Aenean
The issue that I'm facing is that if the post creation gets interrupted for some reason, I would than have to take a look at the log file to see what was the post id of the last post created, afterwards I go to the WordPress site, see the that last post, find the line in the csv file, delete all the line above that line and start over.
I would like to improve that process so, after the post is created, the line from which that post was created in the CSV file is deleted. so next time that the post creation process gets interrupted, I will be able to continue it from the place it was stopped in.
for example if the process was stopped after the first line the new csv file will look like:
title|word|tags|content
amet|consectetur|adipiscing|elit
Maecenas|sed|condimentum|est
in|fermentum|justo|Aenean
I'll appreciate any suggestions or code modification on how to make this work like I want it to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
时,
循环中,将当前行号存储在变量$ line_number
中。创建一个函数
delete_lines()
从输入文件中删除已处理的行:最后,设置一个陷阱,该陷阱在错误,退出或脚本中断中调用此函数:
整个解决方案:整个解决方案:
In the
while
loop, store current line number in a variable$LINE_NUMBER
.Create a function
delete_lines()
which deletes processed lines from the input file:Finally, set up a trap which calls this function on error, exit or script interruption:
Whole solution:
delete_lines
delete_lines