r*******y 发帖数: 1081 | 1 what is the difference between parsing the parameter here?
in ./test input, I can use $1 in the test script to denote the input
How to parse input in ./test < input ?
thanks | l******n 发帖数: 1683 | 2 ./test < input的话, 你的程序不会知道input的存在
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : what is the difference between parsing the parameter here? : in ./test input, I can use $1 in the test script to denote the input : How to parse input in ./test < input ? : thanks
| r*******y 发帖数: 1081 | 3 what is the way to do at this time?
for example:
//test
cat $1
cat $1
then ./test input will display input twice
but ./test < input only display input once
If I want to use ./test < input and display input twice,
when what should I do ?
Thanks.
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : ./test < input的话, 你的程序不会知道input的存在
| l******n 发帖数: 1683 | 4 这样可以:
cat $1 > tmp
cat tmp
cat tmp
rm -f tmp
不过这是一种很不robust的用法.
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : what is the way to do at this time? : for example: : //test : cat $1 : cat $1 : then ./test input will display input twice : but ./test < input only display input once : If I want to use ./test < input and display input twice, : when what should I do ? : Thanks.
| K5 发帖数: 93 | 5 you have two cats which need two inputs but you only provides one input.
That is the problem. if you want two outputs from cats you need to provides
two inputs.
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : what is the way to do at this time? : for example: : //test : cat $1 : cat $1 : then ./test input will display input twice : but ./test < input only display input once : If I want to use ./test < input and display input twice, : when what should I do ? : Thanks.
| K5 发帖数: 93 | 6 that is not the correct way to do it. In his bash script, $1 is empty. you
can echo it to see if that is the case. and the first cat get the inpurt
from the re-directed stdin.
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 这样可以: : cat $1 > tmp : cat tmp : cat tmp : rm -f tmp : 不过这是一种很不robust的用法.
| l******n 发帖数: 1683 | 7 $1是empty, 但是这个时候cat会接管"< input", 所以这是个能work的script, 但是
不是一个好的script.
【在 K5 的大作中提到】![](/moin_static193/solenoid/img/up.png) : that is not the correct way to do it. In his bash script, $1 is empty. you : can echo it to see if that is the case. and the first cat get the inpurt : from the re-directed stdin.
| r*******y 发帖数: 1081 | 8 my question is this:
//input
a A 10
b B 20
I need to write a script called test such that
./test < input > out
such that out is here
A,B
10,20
How can I do this job? Thanks
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : $1是empty, 但是这个时候cat会接管"< input", 所以这是个能work的script, 但是 : 不是一个好的script.
| l******n 发帖数: 1683 | 9 你这个跟最初问的完全没啥关系呀, 你是要输出的时候第一行是输入的文件所有行的
第二列(逗号分隔)?
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : my question is this: : //input : a A 10 : b B 20 : I need to write a script called test such that : ./test < input > out : such that out is here : A,B : 10,20 : How can I do this job? Thanks
| r*******y 发帖数: 1081 | 10 I write this code
//test
cat $1 | awk '{print $2","}' | xargs
cat $1 | awk '{print $3","}' | xargs
then ./test input > output will give me the result
but ./test < input > output will not
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 你这个跟最初问的完全没啥关系呀, 你是要输出的时候第一行是输入的文件所有行的 : 第二列(逗号分隔)?
| | | r*******y 发帖数: 1081 | 11 yes
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 你这个跟最初问的完全没啥关系呀, 你是要输出的时候第一行是输入的文件所有行的 : 第二列(逗号分隔)?
| l******n 发帖数: 1683 | 12 你如果一定要./test input > output和./test < input > output
都work的话, 那就是我之前的那种写法. 不过那不是个好的脚本.
//test
cat $1 > tmp
cat tmp | awk '{print $2","}' | xargs
cat tmp | awk '{print $3","}' | xargs
rm -f tmp
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I write this code : //test : cat $1 | awk '{print $2","}' | xargs : cat $1 | awk '{print $3","}' | xargs : then ./test input > output will give me the result : but ./test < input > output will not
| r*******y 发帖数: 1081 | 13 Also this way will give
A, 10,
B, 20,
but not
A, 10
B, 20
The last item should not be followed by an ","
Any idea to acheive this? thanks
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 你如果一定要./test input > output和./test < input > output : 都work的话, 那就是我之前的那种写法. 不过那不是个好的脚本. : //test : cat $1 > tmp : cat tmp | awk '{print $2","}' | xargs : cat tmp | awk '{print $3","}' | xargs : rm -f tmp
| l******n 发帖数: 1683 | 14 可以这样,
//test
cat $1 > tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri
ntf "\n"}'
cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri
ntf "\n"}'
rm -f tmp
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Also this way will give : A, 10, : B, 20, : but not : A, 10 : B, 20 : The last item should not be followed by an "," : Any idea to acheive this? thanks
| r*******y 发帖数: 1081 | 15 thanks. but it seems not to creat a new row ?
the output is only one row ?
pri
pri
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 可以这样, : //test : cat $1 > tmp : cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri : ntf "\n"}' : cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri : ntf "\n"}' : rm -f tmp
| l******n 发帖数: 1683 | 16 这不是你的要求么, input文件所有行的第二列构成输出的第一行,
所有行的第三列构成输出的第二行.
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : thanks. but it seems not to creat a new row ? : the output is only one row ? : : pri : pri
| r*******y 发帖数: 1081 | 17 I tried this:
//input
a A 10
b B 20
//test
cat $1 > tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri
ntf "\n"}'
cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri
ntf "\n"}'
rm -f tmp
then ./test < input just give
A, B10, 20
not
A, B
10, 20
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 这不是你的要求么, input文件所有行的第二列构成输出的第一行, : 所有行的第三列构成输出的第二行.
| l******n 发帖数: 1683 | 18 呵呵 你这是dos和unix文本格式转换的问题吧. unix下面换行就是"\n", dos 下面是"\
r\n".
pri
pri
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I tried this: : //input : a A 10 : b B 20 : //test : cat $1 > tmp : cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri : ntf "\n"}' : cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri : ntf "\n"}'
| r*******y 发帖数: 1081 | 19 but I am doing the job on linux
"\
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 呵呵 你这是dos和unix文本格式转换的问题吧. unix下面换行就是"\n", dos 下面是"\ : r\n". : : pri : pri
| r*******y 发帖数: 1081 | 20 I find a way from your code:
cat $1 >tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}'
cat tmp | awk '{if (NR==1) {printf "\n%s",$3} else {printf ", %s",$3}}'
rm -f mytmp
but here I have another issue for this input:
a "A" 10
b "B" 20
The output is
"A", "B"
10, 20
I want the output to be
A, B
10, 20
That is, I want to remove the double quote around A and B. How can I do this
in awk?
Thanks a lot
~
"\
【在 l******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 呵呵 你这是dos和unix文本格式转换的问题吧. unix下面换行就是"\n", dos 下面是"\ : r\n". : : pri : pri
| r*******y 发帖数: 1081 | 21 I got it
cat $1 >tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}'
cat tmp | awk '{if (NR==1) {gsub("\"", "", $2);printf "\n%s",$3} else {gsub
("\"", "", $2);printf ", %s",$3}}'
rm -f tmp
Thanks a lot
【在 r*******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I find a way from your code: : cat $1 >tmp : cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}' : cat tmp | awk '{if (NR==1) {printf "\n%s",$3} else {printf ", %s",$3}}' : rm -f mytmp : but here I have another issue for this input: : a "A" 10 : b "B" 20 : The output is : "A", "B"
|
|