【问题】将文本文件的每一行存到一个变量中。

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

【问题】将文本文件的每一行存到一个变量中。

#1

帖子 cnkilior » 2008-09-24 11:46

不能用sed,awk,不进行额外的文件操作(不新建文件包括FIFO,修改文件)

这个文件的每一行都有空格(或tab)

例如:

代码: 全选

abc   12112  89898
hfjdskhjh iuiuiui 75678
hjhkhu 8786876 09090 GHGJKHjk
GghTGJHB ^&*()())_       hjhkj
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 【问题】将文本文件的每一行存到一个变量中。

#2

帖子 HuntXu » 2008-09-24 12:09

xiooli貌似有过相关问题...不知解决没有...
HUNT Unfortunately No Talent...
头像
alinmn
帖子: 185
注册时间: 2006-05-19 21:42
来自: NIT

Re: 【问题】将文本文件的每一行存到一个变量中。

#3

帖子 alinmn » 2008-09-24 12:25

while read a
do
echo $a
done < file
上次由 alinmn 在 2008-09-24 12:33,总共编辑 1 次。
头像
suncanoe
帖子: 476
注册时间: 2005-12-07 0:17
来自: 昆明

Re: 【问题】将文本文件的每一行存到一个变量中。

#4

帖子 suncanoe » 2008-09-24 12:28

cat ttt.txt
abc 12112 89898
hfjdskhjh iuiuiui 75678
hjhkhu 8786876 09090 GHGJKHjk
GghTGJHB ^&*()())_ hjhkj

bash

cat ttt | while read oneline; do echo $oneline; done;
运行结果
abc 12112 89898
hfjdskhjh iuiuiui 75678
hjhkhu 8786876 09090 GHGJKHjk
GghTGJHB ^&*()())_ hjhkj
Two wrongs do not make a right.
blog: http://hi.baidu.com/bookpage
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 【问题】将文本文件的每一行存到一个变量中。

#5

帖子 xiooli » 2008-09-24 13:04

HuntXu 写了:xiooli貌似有过相关问题...不知解决没有...
我是把空格先用特殊字符替换掉,用的时候再删除特殊字符。 :em06
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 【问题】将文本文件的每一行存到一个变量中。

#6

帖子 xiooli » 2008-09-24 13:08

哦,lz的这个还不一样,我的是把每行存到一个数组里面去,有空格的话一行就变成几个元素了。
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

Re: 【问题】将文本文件的每一行存到一个变量中。

#7

帖子 cnkilior » 2008-09-24 15:20

xiooli 写了:
HuntXu 写了:xiooli貌似有过相关问题...不知解决没有...
我是把空格先用特殊字符替换掉,用的时候再删除特殊字符。 :em06
这个方法不错,但要保证文件中没有那些字符。。。
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

Re: 【问题】将文本文件的每一行存到一个变量中。

#8

帖子 cnkilior » 2008-09-24 15:25

xiooli 写了:哦,lz的这个还不一样,我的是把每行存到一个数组里面去,有空格的话一行就变成几个元素了。
我曾想用IFS解决这个问题。

就不知道

代码: 全选

cat xxx|while read sss do :done 
回补会在文件尾巴处停下来,IFS会不会影响他
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

Re: 【问题】将文本文件的每一行存到一个变量中。

#9

帖子 cnkilior » 2008-09-24 16:54

alinmn, suncanoe, 你们的方法是对的,谢谢了,证明我的担心是多余的。。

----------------
手册页: One line is read from the standard input, or from the file
descriptor fd supplied as an argument to the -u option, and the
first word is assigned to the first name, the second word to the
second name, and so on, with leftover words and their interven-
ing separators assigned to the last name. If there are fewer
words read from the input stream than names, the remaining names
are assigned empty values. The characters in IFS are used to
split the line into words. The backslash character (\) may be
used to remove any special meaning for the next character read
and for line continuation. Options, if supplied, have the fol-
lowing meanings:
----
翻译过来就是,read每次读取一行,给跟在后面的变量,如果变量数不够就把剩下的全给最后一个变量。这样就把一行赋值给一个变量了,但read为什么会在EOF处停住手册页貌似没说……
头像
alinmn
帖子: 185
注册时间: 2006-05-19 21:42
来自: NIT

Re: 【问题】将文本文件的每一行存到一个变量中。

#10

帖子 alinmn » 2008-09-24 19:29

我原来写的就是改IFS的,不过后来发现不必要
要改的话可以这样
OLDIFS=$IFS
IFS=$'\n'
...
IFS=$OLDIFS
头像
alinmn
帖子: 185
注册时间: 2006-05-19 21:42
来自: NIT

Re: 【问题】将文本文件的每一行存到一个变量中。

#11

帖子 alinmn » 2008-09-24 19:38

我原来写的就是改IFS的,不过后来发现不必要
要改的话可以这样
OLDIFS=$IFS
IFS=$'\n'
...
IFS=$OLDIFS
头像
c\nc
帖子: 231
注册时间: 2007-12-25 12:51

Re: 【问题】将文本文件的每一行存到一个变量中。

#12

帖子 c\nc » 2008-10-01 14:01

先改 IFS=$'\n'
然后 array=(`cat filename`)。
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

Re: 【问题】将文本文件的每一行存到一个变量中。

#13

帖子 yjcong » 2008-10-02 0:34

也可以用head和tail结合起来做嘛
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
回复