查看完整版本: 问Linux高手一个shell command问题

dhdxx 2007-6-4 08:33 PM

问Linux高手一个shell command问题

现在我在Linux下有个文本文件,比如说text.txt,记录了一些数据,格式如下
a  b  c
d  e  f
......
......
我想每次顺序读入一行数据,把他们付给三个变量,比如说x1,x2,x3,然后干点别的事,在读下面一列
比如说第一次读入后
echo $x1 $x2 $x3
a b c
第二次读入后
echo $x1 $x2 $x3
d e f
谁知道这个在c shell底下该如何作
谢了

墨者革离 2007-6-4 10:06 PM

用awk吧

dhdxx 2007-6-4 10:10 PM

awk just reads the whole file. It seems that it can't read certain line.
[quote]原帖由 [i]墨者革离[/i] 于 2007-6-4 09:06 PM 发表
用awk吧 [/quote]

eruisi 2007-6-4 10:55 PM

perl

dhdxx 2007-6-5 11:18 AM

some guys suggest this software to me. It seems to be very useful.
Thanks
[quote]原帖由 [i]eruisi[/i] 于 2007-6-4 09:55 PM 发表
perl [/quote]

墨者革离 2007-6-5 11:35 AM

[quote]原帖由 [i]dhdxx[/i] 于 2007-6-4 09:10 PM 发表
awk just reads the whole file. It seems that it can't read certain line.
[/quote]

awk is the best tool to do line-by-line processing. For your application, just do

awk '{print $1, $2, $3}' text.txt

Perl is fine too, but I do not think it is necessary.

dhdxx 2007-6-5 11:45 AM

However, for a text file
a b c
d e f
if you use "awk '{print $1, $2, $3}' text.txt", the result is that the whole file will be outputted on the screen. My request is that for each time, you can output the certain line, not the whole file.

Anyway, My origin idea is that shell is a low-level program language and maybe save me some computing time. However, I just found that it is also wasting much time. I discard it.  

Thanks very much for your help
[quote]原帖由 [i]墨者革离[/i] 于 2007-6-5 10:35 AM 发表


awk is the best tool to do line-by-line processing. For your application, just do

awk '{print $1, $2, $3}' text.txt

Perl is fine too, but I do not think it is necessary. [/quote]

墨者革离 2007-6-5 11:52 AM

[quote]原帖由 [i]dhdxx[/i] 于 2007-6-5 10:45 AM 发表
However, for a text file
a b c
d e f
if you use "awk '{print $1, $2, $3}' text.txt", the result is that the whole file will be outputted on the screen. My request is that for each time ... [/quote]

Do not underestimate awk. If you need some other processing, just do

awk 'BEGIN {...} {x1=$1; x2=$2; x3=$3; ...; if (...) {print}}' text.txt

You can do whatever you like in the ... part

andrew 2007-6-5 12:31 PM

matlab is good for large databases

[quote]原帖由 [i]dhdxx[/i] 于 2007-6-5 11:45 AM 发表
However, for a text file
a b c
d e f
if you use "awk '{print $1, $2, $3}' text.txt", the result is that the whole file will be outputted on the screen. My request is that for each time ... [/quote]

dhdxx 2007-6-5 01:05 PM

I finally solve this question by using google to get some menu.
setenv x1 `awk '{if ( NR == 1 ) print $1}' test.txt`
setenv x2 `awk '{if ( NR == 1 ) print $2}' test.txt`
setenv x3 `awk '{if ( NR == 1 ) print $3}' test.txt`

"NR" means the row number
Thank 墨者革离 for your hint

[quote]原帖由 [i]墨者革离[/i] 于 2007-6-5 10:52 AM 发表


Do not underestimate awk. If you need some other processing, just do

awk 'BEGIN {...} {x1=$1; x2=$2; x3=$3; ...; if (...) {print}}' text.txt

You can do whatever you like in the ... part [/quote]

imac 2007-6-5 07:45 PM

大哥,他还要对$1 $2 $3作操作呢...

你要显示,就用more text.txt就行,或者cat也行...

[quote]原帖由 [i]墨者革离[/i] 于 2007-6-5 10:35 AM 发表


awk is the best tool to do line-by-line processing. For your application, just do

awk '{print $1, $2, $3}' text.txt

Perl is fine too, but I do not think it is necessary. [/quote]
页: [1]
查看完整版本: 问Linux高手一个shell command问题