由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - Need urgent help on a samll shell script problem!
相关主题
IFS shell script新手unix shell script debugging help!
有关Unix shell script的问题paste command 里面的 -
[转载] Re: shell script questionhow to print the first paragraph of a text file?
One shell questionabout strtok()
how to get the backgroup run state?how to find a file in unix
提示我 too many ('s 怎么办?how to count string times using perl
Shell script求教Re: 为什么emacs自动给我加^M.
新手求助shell script一个关于命令行的小问题
相关话题的讨论汇总
话题: need话题: script话题: boy话题: girl话题: shell
进入Unix版参与讨论
1 (共1页)
a*a
发帖数: 23
1
I got stuck for the following problem. My shell script is as follows:
for i in `sort -u file1.txt` #the small dot here is backquote
do
echo $i
done
the file1.txt consists of two sentences :
I am a boy
you are a girl
after I run the script, the output is
$ ./li.sh
I
am
a
boy
you
are
a
girl
However, I need the output like
I am a boy
you are a girl
That is , I need variable i to be assigned the value of the whole line not a
single word delimited by the space. How should I change my scri
o**a
发帖数: 86
2

sth like this:
for i in `sort -u file1.txt` #the small dot here is backquote
do
j=`echo $j $i`
done
echo $j

【在 a*a 的大作中提到】
: I got stuck for the following problem. My shell script is as follows:
: for i in `sort -u file1.txt` #the small dot here is backquote
: do
: echo $i
: done
: the file1.txt consists of two sentences :
: I am a boy
: you are a girl
: after I run the script, the output is
: $ ./li.sh

a*a
发帖数: 23
3

Dear ouba, firstly thank you so much! I tried your script and the output was
]$./lii.sh
I am a boy you are a girl
so I can only bother you again and need your further help. What I want is the
variable to be assigned with the value of *each line*, say $j=I am a boy
firstly then $j =you are a girl. However, because the " echo $j" statement was
outside the loop, $j finally get the value of both lines together. Do you
have any idea? I'm pretty greenie in Unix and thanks again for your help!
ly
a

【在 o**a 的大作中提到】
:
: sth like this:
: for i in `sort -u file1.txt` #the small dot here is backquote
: do
: j=`echo $j $i`
: done
: echo $j

o**a
发帖数: 86
4
sorry, i thought "\n" could be passed to $i.
let me think think.

the
was

【在 a*a 的大作中提到】
:
: Dear ouba, firstly thank you so much! I tried your script and the output was
: ]$./lii.sh
: I am a boy you are a girl
: so I can only bother you again and need your further help. What I want is the
: variable to be assigned with the value of *each line*, say $j=I am a boy
: firstly then $j =you are a girl. However, because the " echo $j" statement was
: outside the loop, $j finally get the value of both lines together. Do you
: have any idea? I'm pretty greenie in Unix and thanks again for your help!
: ly

o**a
发帖数: 86
5

but if the original file has # in it, it's replaced by blank afterwards.
dunno if there is any line-read like <> in perl.
a
o**a
发帖数: 86
6

i know, it's practical but not perfect.
since anything may occur inside the input.
w**n
发帖数: 88
7
add following before sort
IFS=\n
works for me
br />

【在 a*a 的大作中提到】
: I got stuck for the following problem. My shell script is as follows:
: for i in `sort -u file1.txt` #the small dot here is backquote
: do
: echo $i
: done
: the file1.txt consists of two sentences :
: I am a boy
: you are a girl
: after I run the script, the output is
: $ ./li.sh

l*l
发帖数: 225
8
#!/bin/sh
NUM=`cat file1.txt| wc -l`
COUNT=1
while [ $COUNT -le $NUM ]
do
sort -u file1.txt | sed -n $COUNT\p
COUNT=`expr $COUNT + 1`
done

【在 a*a 的大作中提到】
: I got stuck for the following problem. My shell script is as follows:
: for i in `sort -u file1.txt` #the small dot here is backquote
: do
: echo $i
: done
: the file1.txt consists of two sentences :
: I am a boy
: you are a girl
: after I run the script, the output is
: $ ./li.sh

o**a
发帖数: 86
9

this is the kind of cool solution i think:)
a

【在 w**n 的大作中提到】
: add following before sort
: IFS=\n
: works for me
: br />

w**n
发帖数: 88
10
yeah, you're right.
it is my typo, actually no export needed

【在 l*l 的大作中提到】
: #!/bin/sh
: NUM=`cat file1.txt| wc -l`
: COUNT=1
: while [ $COUNT -le $NUM ]
: do
: sort -u file1.txt | sed -n $COUNT\p
: COUNT=`expr $COUNT + 1`
: done

1 (共1页)
进入Unix版参与讨论
相关主题
一个关于命令行的小问题how to get the backgroup run state?
[转载] a simple but weird question about the usage of "test" in RH6.2 !提示我 too many ('s 怎么办?
为什么有时用windows的telnet,每打一个字母会出现两个相同的字母呀?Shell script求教
如何显示出“*”新手求助shell script
IFS shell script新手unix shell script debugging help!
有关Unix shell script的问题paste command 里面的 -
[转载] Re: shell script questionhow to print the first paragraph of a text file?
One shell questionabout strtok()
相关话题的讨论汇总
话题: need话题: script话题: boy话题: girl话题: shell