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
|