<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]对文本里的内容组装SQL之用Shell的for循环读取文件里有空格时出现一行变两行输出的问题解决方法。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Sat, 29 Sep 2018 06:35:31 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	cat a.txt <br/>jack 1<br/>tianjin 2<br/>old5 3<br/><br/><br/><textarea name="code" class="php" rows="15" cols="100">
#!/bin/bash
IFS_old=$IFS
IFS=$&#039;&#92;n&#039;
for line in $(cat a.txt);do
echo $line
done
IFS=$IFS_old
</textarea><br/><br/>sh read.sh <br/>jack 1<br/>tianjin 2<br/>old5 3<br/><br/><br/>加上模拟输出：<br/>insert into t set name=&#039;xxx&#039;, id=N;<br/><textarea name="code" class="php" rows="15" cols="100">
#!/bin/bash
IFS_old=$IFS
IFS=$&#039;&#92;n&#039;
for line in $(cat a.txt);do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #echo $line;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name=$(echo $line&#124;cut -d&quot; &quot; -f 1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; age=$(echo $line&#124;cut -d&quot; &quot; -f 2)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;insert into t set name=&#039;$&#123;name&#125;&#039;,id=$&#123;age&#125;;&quot;
done
IFS=$IFS_old
</textarea><br/>sh read.sh&nbsp;&nbsp;<br/>insert into t set name=&#039;jack&#039;,id=1;<br/>insert into t set name=&#039;tianjin&#039;,id=2;<br/>insert into t set name=&#039;old5&#039;,id=3;<br/><br/>行内有空格换行的原因：<br/>如果输入文本每行中没有空格，则line在输入文本中按换行符分隔符循环取值．<br/>如果输入文本中包括空格或制表符，则不是换行读取，line在输入文本中按空格分隔符或制表符或换行符特环取值．<br/>可以通过把IFS设置为换行符来达到逐行读取的功能．<br/>demo：<br/>假设现需要读取如下的文件rollback_config.txt：<br/>ROLLBACK_SERVICES:upserv  checkserv<br/>ROLLBACK_VERSION:v1.1<br/>使用   for line in `cat rollback_config.txt`; do echo &quot;$&#123;line&#125;&quot;; done  读取的结果会是：<br/>ROLLBACK_SERVICES:upserv <br/>checkserv<br/>ROLLBACK_VERSION:v1.1<br/>显然不是我们想要的。<br/><br/>解决方法：<br/>IFS_old=$IFS<br/>IFS=$&#039;&#92;n&#039;<br/>for line in  `cat  rollback_config`;do<br/>echo &quot;$line&quot;<br/>done;<br/>IFS=$IFS_old<br/>这样一来就可以了！<br/><br/><br/><br/>IFS的默认值为：空白(包括：空格，制表符，换行符)．<br/><br/>---------------------<br/><br/>本文来自 潼潼水势向江东 的CSDN 博客 ，全文地址请点击：https://blog.csdn.net/zhongjling/article/details/52859055?utm_source=copy <br/><br/>二）用AWK就很方便处理，如下，注意下单引号是三个单引号，中间那个被转义，才输出一个单引号&nbsp;&nbsp;&nbsp;&nbsp; &#039;&#92;&#039;&#039;&nbsp;&nbsp;：<br/><textarea name="code" class="php" rows="15" cols="100">
cat readawk.sh
#!/bin/bash
cat a.txt &#124;awk &#039;&#123;print &quot;insert into t set name=&#039;&#92;&#039;&#039;&quot;$1&quot;&#039;&#92;&#039;&#039;,id=&quot;$2.&quot;;&quot;&#125;&#039;
</textarea><br/>insert into t set name=&#039;jack&#039;,id=1;<br/>insert into t set name=&#039;tianjin&#039;,id=2;<br/>insert into t set name=&#039;old5&#039;,id=3;<br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]对文本里的内容组装SQL之用Shell的for循环读取文件里有空格时出现一行变两行输出的问题解决方法。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>