Redian新闻
>
Need urgent help on a samll shell script problem!
avatar
a*a
2
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
avatar
o*a
3

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

avatar
a*a
4

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

avatar
o*a
5
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

avatar
o*a
6

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
avatar
o*a
7

i know, it's practical but not perfect.
since anything may occur inside the input.
avatar
w*n
8
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

avatar
l*l
9
#!/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

avatar
o*a
10

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

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

avatar
w*n
11
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。