在 shell 脚本中读取很长的一行而不分成多行

发布于 2024-11-05 23:11:49 字数 545 浏览 0 评论 0原文

我有一个包含很长行数据的文件。当我尝试使用 shell 脚本读取时,数据会分成多行,即在某些点中断。

示例行:

B_18453583||Active|917396140129|405819121107402|Active|7396140129||7396140129|||||||||18-MAY-10|||||18-MAY-10|405819121107402|Outgoing International Calls,Outgoing Calls,WAP,Call Waiting,MMS,Data Service,National Roaming-Voice,Outgoing International Calls except home country,Conference Call,STD,Call Forwarding-Barr,CLIP,Incoming Calls,INTSNS,WAPSNS,International Roaming-Voice,ISD,Incoming Calls When Roaming Internationally,INTERNET||For You Plan

I have a file which has very long rows of data. When i try to read using shell script, the data comes into multiple lines,ie, breaks at certain points.

Example row:

B_18453583||Active|917396140129|405819121107402|Active|7396140129||7396140129|||||||||18-MAY-10|||||18-MAY-10|405819121107402|Outgoing International Calls,Outgoing Calls,WAP,Call Waiting,MMS,Data Service,National Roaming-Voice,Outgoing International Calls except home country,Conference Call,STD,Call Forwarding-Barr,CLIP,Incoming Calls,INTSNS,WAPSNS,International Roaming-Voice,ISD,Incoming Calls When Roaming Internationally,INTERNET||For You Plan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦情居士 2024-11-12 23:11:49

||||||||

所有这些都是一行的内容。
我使用这样的正常读取:

var=`cat pranay.psv`
for i in $var; do
    echo $i
done

输出如下:

B_18453583||Active|917396140129|405819121107402|Active|7396140129||7396140129|||||||||18-  MAY-10|||||18-MAY-10|405819121107402|Outgoing  
International  
Calls,Outgoing  
Calls,WAP,Call  
Waiting,MMS,Data  
Service,National  
Roaming-Voice,Outgoing  
International  
Calls  
except  
home  
country,Conference  
Call,STD,Call  
Forwarding-Barr,CLIP,Incoming  
Calls,INTSNS,WAPSNS,International  
Roaming-Voice,ISD,Incoming  
Calls  
When  
Roaming  
Internationally,INTERNET||For  
You  
Plan

||||||||

All this is the content of a single line.
I use a normal read like this :

var=`cat pranay.psv`
for i in $var; do
    echo $i
done

The output comes as:

B_18453583||Active|917396140129|405819121107402|Active|7396140129||7396140129|||||||||18-  MAY-10|||||18-MAY-10|405819121107402|Outgoing
International
Calls,Outgoing
Calls,WAP,Call
Waiting,MMS,Data
Service,National
Roaming-Voice,Outgoing
International
Calls
except
home
country,Conference
Call,STD,Call
Forwarding-Barr,CLIP,Incoming
Calls,INTSNS,WAPSNS,International
Roaming-Voice,ISD,Incoming
Calls
When
Roaming
Internationally,INTERNET||For
You
Plan

若水般的淡然安静女子 2024-11-12 23:11:49

||||||||

我如何在单行中打印所有内容?
请帮忙。
谢谢

||||||||

How do i print all in single line??
Please help.
Thanks

活雷疯 2024-11-12 23:11:49

这是因为分词。一种更简单的方法来做到这一点(也可以通过 无用的使用 cat 来解散)是这样的:

while IFS= read -r -d 

详细解释一下:

  • $'...' 可用于创建带有转义序列的人类可读字符串。请参阅man bash
  • IFS= 是必要的,以避免 IFS 中的任何字符从 $REPLY 的开头和结尾被删除。
  • -r 避免专门解释文本中的反斜杠。
  • -d $'\n' 按换行符分割行。
  • 使用文件描述符 9 代替标准输入来存储数据,以避免像 cat 这样的贪婪命令吃掉所有数据。
\n' -u 9 do echo "$REPLY" done 9< pranay.psv

详细解释一下:

  • $'...' 可用于创建带有转义序列的人类可读字符串。请参阅man bash
  • IFS= 是必要的,以避免 IFS 中的任何字符从 $REPLY 的开头和结尾被删除。
  • -r 避免专门解释文本中的反斜杠。
  • -d $'\n' 按换行符分割行。
  • 使用文件描述符 9 代替标准输入来存储数据,以避免像 cat 这样的贪婪命令吃掉所有数据。

This is because of word splitting. An easier way to do this (which also disbands with the useless use of cat) is this:

while IFS= read -r -d 

To explain in detail:

  • $'...' can be used to create human readable strings with escape sequences. See man bash.
  • IFS= is necessary to avoid that any characters in IFS are stripped from the start and end of $REPLY.
  • -r avoids interpreting backslash in text specially.
  • -d $'\n' splits lines by the newline character.
  • Use file descriptor 9 for data storage instead of standard input to avoid greedy commands like cat eating all of it.
\n' -u 9 do echo "$REPLY" done 9< pranay.psv

To explain in detail:

  • $'...' can be used to create human readable strings with escape sequences. See man bash.
  • IFS= is necessary to avoid that any characters in IFS are stripped from the start and end of $REPLY.
  • -r avoids interpreting backslash in text specially.
  • -d $'\n' splits lines by the newline character.
  • Use file descriptor 9 for data storage instead of standard input to avoid greedy commands like cat eating all of it.
夏有森光若流苏 2024-11-12 23:11:49

您需要正确的引用。在您的情况下,您应该使用命令read

while read line ; do
    echo "$line"
done < pranay.psv

You need proper quoting. In your case, you should use the command read:

while read line ; do
    echo "$line"
done < pranay.psv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文