一个读取文件内容的shell脚本
2018-01-23 12:13
477 查看
如下展示的是一个shell脚本,该脚本可以2行2行的读取文件中的内容,然后把读取到的文件内容保存到一个名为target.json的文件当中,然后打印出target.json中的内容,打印完毕删除target.json文件,然后再新建target.json文件,然后再读入两行内容,然后再打印,再删除,直到源文件被读完。贴到博客的目的是做个笔记,毕竟本人没有专门学个shell编程,这些脚本中的某些语法可能后续会用到,所以算是做了个储备。
#!/bin/bash
count=0
rm target.json
touch target.json
while read line;do
((count++))
{
echo $line >> target.json
if [ $count -ge 2 ] && [ $((count%2)) -eq 0 ];then
sudo cat target.json
echo "2 line is reached!, current count is:"$count
count=0
sudo rm -f target.json
touch target.json
fi
}
done < $1
echo 'last submit'
假设保存该脚本的文件名是readLine.sh。则用法是: ./readLine.sh + 目标文件名。 该脚本改编自如下脚本,原文链接是:原始脚本来源,原脚本顺便也张贴出来:
#!/bin/bash
count=0
rm target.json
touch target.json
while read line;do
((count++))
{
echo $line >> target.json
if [ $count -gt 100000 ] && [ $((count%2)) -eq 0 ];then
count=0
curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null
rm target.json
touch target.json
fi
}
done < $1
echo 'last submit'
curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null
#!/bin/bash
count=0
rm target.json
touch target.json
while read line;do
((count++))
{
echo $line >> target.json
if [ $count -ge 2 ] && [ $((count%2)) -eq 0 ];then
sudo cat target.json
echo "2 line is reached!, current count is:"$count
count=0
sudo rm -f target.json
touch target.json
fi
}
done < $1
echo 'last submit'
假设保存该脚本的文件名是readLine.sh。则用法是: ./readLine.sh + 目标文件名。 该脚本改编自如下脚本,原文链接是:原始脚本来源,原脚本顺便也张贴出来:
#!/bin/bash
count=0
rm target.json
touch target.json
while read line;do
((count++))
{
echo $line >> target.json
if [ $count -gt 100000 ] && [ $((count%2)) -eq 0 ];then
count=0
curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null
rm target.json
touch target.json
fi
}
done < $1
echo 'last submit'
curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null
相关文章推荐
- shell读取脚本同一个目录下的xml格式的文件
- shell脚本 把一个文件的内容全部转换为大写
- 如何写一个能够读取文件并显示全部内容,计数行数的shell
- Shell脚本基础7-如何在一个文件尾部写入内容
- 遇到一个在脚本中读取文件内容占用文件句柄的小问题
- 读取文件的每一行内容并输出 的shell脚本实现
- shell脚本按行读取文件内容的方法
- 批处理脚本读取文件内容赋值给一个变量
- shell读取一个文件内容到变量并逐一赋值
- shell脚本----读取一个文本文件将内容按行保存成数组变量
- 通过脚本的方式杀tomcat进程(shell脚本),shell中将一个文件的内容读出赋值给一个变量
- shell实践(一)读取一个文件输出文件内容
- 用shell脚本写出检测/tmp/size.log文件,如果存在显示它的内容,不存在则创建一个文件将创建时间写入
- shell脚本如何从json文件读取一个某个值
- Shell脚本基础17-while循环读取文件内容
- shell脚本清空文件内容
- Shell脚本读取ini配置文件的实现代码2例
- Shell读取文件内容
- 如何用shell一次读取多个文件内容
- shell:读取文件的每一行内容并输出