求脚本!!!!

sh/bash/dash/ksh/zsh等Shell脚本
killer2001
帖子: 16
注册时间: 2010-07-09 20:28

Re: 求脚本!!!!

#31

帖子 killer2001 » 2011-08-05 8:50

我就是我2 写了:我不知道是什么导致这种差异
++ tr -d '\n'
位置乱了。跑到最前面去了。 :em20
直接shell中分开输入,以管道为间隔,若前一个输出正确则继续一个。

代码: 全选

grep "compt" a.dat 
grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }'
grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z 
grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z | tr -d '\n'
grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z | tr -d '\n' | sed 's/,$//'
或者新建一个用户试试。我实在搞不懂了。
前面神说了,调试。。。 :em20
可能是系统的问题,我的是redhat, 分开输入 tr -d '\n'还是跑到前面去了,呵呵

[uidt5100@home]$ grep "compt" a.dat
y1=compt_a
y2=compt_b
y3=compt_c
[uidt5100@home]$ grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }'
",ompt_a
",ompt_b
"compt_c",
[uidt5100@home]$ grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z
",OMPT_A
",OMPT_B
"COMPT_C",
[uidt5100@home]$ grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z | tr -d '\n'
","COMPT_C",[uidt5100@home]$ grep "compt" a.dat | awk -F "=" $2 "\"," }' | tr a-z A-Z | tr -d '\n' | sed 's/,$//'
","COMPT_C"[uidt5100@home]$
killer2001
帖子: 16
注册时间: 2010-07-09 20:28

Re: 求脚本!!!!

#32

帖子 killer2001 » 2011-08-05 8:58

fnan 写了:十二楼的可以用啊:
kose5@kose5-Aspire-4552:~$ data=$(grep "compt" a.dat | awk -F "=" '{print "\"" $2 "\"," }' | tr a-z A-Z | tr -d '\n' | sed 's/,$//')
kose5@kose5-Aspire-4552:~$ echo "ss=$data"
ss="COMPT_A","COMPT_B","COMPT_C","COMPT_D","COMPT_E"
kose5@kose5-Aspire-4552:~$
虽然awk不用另开那么多进程:
awk -F= '{if (f == "") printf "ss=\"";else printf f "\",\"";f=toupper($2)} END{print toupper($2) "\""}' a.dat
(lz的shell是什么shell?)

我不知道是什么shell,但前面运行的脚本里加了#!/bin/bash的啊

[uidt5100@home]$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[uidt5100@home]$ awk --version
GNU Awk 3.1.7
Copyright (C) 1989, 1991-2009 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
[uidt5100@home]$ tr --version
tr (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.
[uidt5100@home]$ echo $SHELL
/bin/bash
[uidt5100@home]$
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 求脚本!!!!

#33

帖子 link_01 » 2011-08-05 9:29

看你给的例子,文本很简单,不就是查找是不是包含compt,然后对其用=分割取第二列组合字符串吗?
用printf而不是print可以省掉换行符,完事直接echo。
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
我就是我2
帖子: 1215
注册时间: 2008-12-13 10:55
来自: the Earth
联系:

Re: 求脚本!!!!

#34

帖子 我就是我2 » 2011-08-05 12:34

最重要的是,打印完之后不自动换行。搞不懂。此事非本人目前能解。高手啊!出现吧! :em06
苏东坡问佛印道:“人人皆念观世音菩萨,观世音菩萨念谁?”
佛印答道:“念观世音菩萨。”
苏东坡诧异:“为何亦念观世音菩萨?”
曰:“求人不如求己。”


dropbox 誰好奇,誰來點
勞碌一生又如何,虛度一生又如何,你過得開心嗎?人活著就要開心嘛!
killer2001
帖子: 16
注册时间: 2010-07-09 20:28

Re: 求脚本!!!!

#35

帖子 killer2001 » 2011-08-05 12:51

找到原因了,我是用putty登陆到服务器上做的。
以前的很多命令结果都没问题,只是在本机上显示出了问题。

谢谢各位! :em11
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 求脚本!!!!

#36

帖子 link_01 » 2011-08-05 13:19

cat c.txt
c=abc
y1=compt_a
y2=compt_b
y3=compt_c
x=3
y=4
y4=compt_d

代码: 全选

link01@debian:~/src/shell$ ss=$(awk -F"=" '/compt/{ printf "\"%s\",",$2 }' c.txt|sed -nr 's/(.*),$/\1/p' |tr a-z A-Z)
link01@debian:~/src/shell$ echo "ss=$ss"
ss="COMPT_A","COMPT_B","COMPT_C","COMPT_D"
link01@debian:~/src/shell$ ss=$(awk -F"=" '/compt/{ printf "\"%s\",",$2 }' c.txt|sed -nr 's/(.*),$/\1/p' |tr a-z A-Z);echo "ss=$ss"
ss="COMPT_A","COMPT_B","COMPT_C","COMPT_D"
link0x@debian:~/src/shell$ 
这不显示很正常嘛
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
我就是我2
帖子: 1215
注册时间: 2008-12-13 10:55
来自: the Earth
联系:

Re: 求脚本!!!!

#37

帖子 我就是我2 » 2011-08-05 21:22

把标准输出重定向到文件 再cat
:em20
苏东坡问佛印道:“人人皆念观世音菩萨,观世音菩萨念谁?”
佛印答道:“念观世音菩萨。”
苏东坡诧异:“为何亦念观世音菩萨?”
曰:“求人不如求己。”


dropbox 誰好奇,誰來點
勞碌一生又如何,虛度一生又如何,你過得開心嗎?人活著就要開心嘛!
killer2001
帖子: 16
注册时间: 2010-07-09 20:28

Re: 求脚本!!!!

#38

帖子 killer2001 » 2011-08-08 9:11

我就是我2 写了:把标准输出重定向到文件 再cat
:em20
你说得的很对,重定向到文件就看见真正的结果了。
以前我这里显示的结果都不对(实际是对的),是因为我这里离服务器有点延时,所以看上去结果颠倒了。
回复