24小时热门版块排行榜    

CyRhmU.jpeg
查看: 954  |  回复: 3
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

yjcmwgk

禁虫 (文坛精英)

密度泛函·小卒

优秀版主

[求助] 请问,一个非常简单的shell脚本,我却无法让他正常运行,请问我做错了什么吗?已有2人参与

有个文件,内容如下:
CODE:
-----------------------------
/home/g/zly/subsmbinpc/S-f8bophf-aicd.log
Leave Link    1 at Mon Jun 15 15:25:55 2015, MaxMem=  5368709120 cpu:         0.7
#p m06/epr-iii nmr(csgt) iop(10/93=1) scf(conver=6)
-----------------------------
/home/g/q/42pie-irc.log
Leave Link    1 at Mon Jun 22 11:09:03 2015, MaxMem=  8053063680 cpu:         2.4
#p 6-311+g(d,p) scrf=(iefpcm,solvent=methanol) empiricaldispersion=gd3
Job cpu time:      13 days  7 hours 27 minutes 12.8 seconds.
Normal termination of Gaussian 09 at Tue Jun 23 03:11:10 2015.
-----------------------------
/home/g/wcm/SubPc/Exercise/17-th-irc-4.log
Leave Link    1 at Tue Jun 23 10:00:51 2015, MaxMem=  8053063680 cpu:         1.1
#p 6-311+g(d) geom=connectivity empiricaldispersion=gd3 m062x scf=conv

我想把它改成这样
CODE:
-----------------------------
File Name: /home/g/zly/subsmbinpc/S-f8bophf-aicd.log
Start Time: Mon Jun 15 15:25:55 2015
CMD Line: #p m06/epr-iii nmr(csgt) iop(10/93=1) scf(conver=6)
-----------------------------
File Name: /home/g/q/42pie-irc.log
Start Time: Mon Jun 22 11:09:03 2015
CMD Line: #p 6-311+g(d,p) scrf=(iefpcm,solvent=methanol) empiricaldispersion=gd3
Total Time: 13 days  7 hours 27 minutes 12.8 seconds.
State: Normal
-----------------------------
File Name: /home/g/wcm/SubPc/Exercise/17-th-irc-4.log
Start Time: Tue Jun 23 10:00:51 2015
CMD Line: #p 6-311+g(d) geom=connectivity empiricaldispersion=gd3 m062x scf=conv

我弄了这么个脚本
CODE:
cat whsglswjgcngswgthbwxs | while read line;do
  if  [[ ${line:0:2} = " L" ]] ; then
    echo -e 'Starting Time: \c'
    echo ${line:20:44}
  else
    if [[ ${line:0:2} = " #" ]] ; then
      echo -e 'CMD Line: \c'
      echo $line
    else
      if [[ ${line:0:2} = "--" ]] ; then
        echo $line
      else
        if [[ ${line:0:2} = " J" ]] ; then
          echo -e 'Total Time: \c'
          echo ${line:22:59}
        else
          echo ${line:0:7}
        fi
      fi
    fi
  fi
done

按说很简单啊,但是屏幕上却输出了这些东西
CODE:
-----------------------------
/home/g
Leave L
#p m06/
-----------------------------
/home/g
Leave L
#p 6-31
Job cpu
Normal
-----------------------------
/home/g
Leave L
#p 6-31

请问 我是做错了哪儿了?非常感谢各位高手!!!!!
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

独孤狼

金虫 (正式写手)

独行者

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
yjcmwgk: 金币+35, ★★★★★最佳答案, 其实楼上不知道我这几年一直python哈哈 2015-07-09 10:20:01
cat tv | while read line;do
  if  [[ ${line:0:1} = "L" ]] ; then
    echo -e 'Starting Time: \c'
    echo ${line:20:44}
  else
    if [[ ${line:0:1} = "#" ]] ; then
      echo -e 'CMD Line: \c'
      echo $line
    else
      if [[ ${line:0:1} = "--" ]] ; then
        echo $line
      else
        if [[ ${line:0:1} = "J" ]] ; then
          echo -e 'Total Time: \c'
          echo ${line:22:59}
        else
          if [[ ${line:0:1} = "/" ]] ; then
            echo -e 'File Name :\c'
            echo $line
          else
            if [[ ${line:0:1} = "N" ]] ; then
               echo "State: Normal"
                 else
                    echo ${line:0:10}

            fi
          fi
        fi
      fi
    fi
  fi
done
发过来抢BB了,话说上面人用python打你脸了
轮回的过往,注定的未来;独孤的行者,孤独的路途……
3楼2015-06-23 16:37:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 4 个回答

Logic0

新虫 (初入文坛)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
yjcmwgk: 金币+50, ★★★★★最佳答案, 谢谢!其实我一直用大蟒蛇,遇到同行好感动!握爪 2015-07-09 10:19:02
第一次在小木虫回答问题,希望能帮助到你。
这个字符串处理的问题,非常建议楼主使用 Python 来做,整个就会变得非常简单。
如果在某种情况下必须使用 shell,建议使用 awk 进行文本处理。
这里附上python 2.7 的代码,可以看到逻辑非常清晰易懂。
CODE:
# 遍历文件行
for line in file("t.txt"):
    if( line.startswith("-----") ):
        continue
    if( line.startswith("/home") ):
        print "File Name: %s" % line.strip()
        continue
    if( line.startswith("Leave") ):
        print "Start Time: %s" % line.strip()[11:line.find(",")]
        continue
    if( line.startswith("#p") ):
        print "CMD Line: %s" % line.strip()
        continue
    if( line.startswith("Job cpu time") ):
        print "Total Time: %s" % line.strip()[14:]
        continue

    print "State: %s" % line.strip().split(" ")[0]


关键点讲解:
字符串.startswith("start"  ----> 判断字符串是否以 start 开头
字符串.strip()  ---> 删除多余的不可见字符,这里用来删除每行的换行符
字符串[x:y]  --->  截取从 第x个字符开始到 第y 个字符结束的子串,y 不写 表示 到末尾
2楼2015-06-23 15:51:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Logic0

新虫 (初入文坛)

引用回帖:
3楼: Originally posted by 独孤狼 at 2015-06-23 16:37:16
cat tv | while read line;do
  if  [] ; then
    echo -e 'Starting Time: \c'
    echo ${line:20:44}
  else
    if [] ; then
      echo -e 'CMD Line: \c'
      echo $line
    else
      if []  ...

哈哈哈,合适的工作做合适的事情嘛
4楼2015-06-23 16:56:15
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见