免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3874 | 回复: 12
打印 上一主题 下一主题

[文本处理] shell重定向问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-03-28 15:02 |只看该作者 |倒序浏览

  1. str1='1 2 3 4'
  2. str2='2 4'

  3. echo $(comm -23 <(echo $str1 |xargs -n1|sort) <(echo $str2 |xargs -n1|sort))
复制代码
当使用#!/bin/bash的时候没有问题 但是#!/bin/sh的时候就会报错

syntax error near unexpected token `('

难道上面的那种写法不符合postfix规范吗? 如果在#!/bin/sh 该如何写?

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
2 [报告]
发表于 2014-03-28 15:11 |只看该作者
本帖最后由 seesea2517 于 2014-03-28 15:12 编辑

回复 1# ccnuzxg


    是啊,<() 这个语法是bash特性。用临时文件就好了呗。

quote 标签会解释出表情来,看来还是code标签好啊。
  1. Things bash has that sh does not:
  2.    long invocation options
  3.    [+-]O invocation option
  4.    -l invocation option
  5.    `!' reserved word to invert pipeline return value
  6.    `time' reserved word to time pipelines and shell builtins
  7.    the `function' reserved word
  8.    the `select' compound command and reserved word
  9.    arithmetic for command: for ((expr1 ; expr2; expr3 )); do list; done
  10.    new ...' and $"..." quoting
  11.    the $(...) form of command substitution
  12.    the $(<filename) form of command substitution, equivalent to
  13.       $(cat filename)
  14.    the ${#param} parameter value length operator
  15.    the ${!param} indirect parameter expansion operator
  16.    the ${!param*} prefix expansion operator
  17.    the ${param:offset[:length]} parameter substring operator
  18.    the ${param/pat[/string]} parameter pattern substitution operator
  19.    expansions to perform substring removal (${p%[%]w}, ${p#[#]w})
  20.    expansion of positional parameters beyond $9 with ${num}
  21.    variables: BASH, BASH_VERSION, BASH_VERSINFO, UID, EUID, REPLY,
  22.          TIMEFORMAT, PPID, PWD, OLDPWD, SHLVL, RANDOM, SECONDS,
  23.          LINENO, HISTCMD, HOSTTYPE, OSTYPE, MACHTYPE, HOSTNAME,
  24.          ENV, PS3, PS4, DIRSTACK, PIPESTATUS, HISTSIZE, HISTFILE,
  25.          HISTFILESIZE, HISTCONTROL, HISTIGNORE, GLOBIGNORE, GROUPS,
  26.          PROMPT_COMMAND, FCEDIT, FIGNORE, IGNOREEOF, INPUTRC,
  27.          SHELLOPTS, OPTERR, HOSTFILE, TMOUT, FUNCNAME, histchars,
  28.          auto_resume
  29.    DEBUG trap
  30.    ERR trap
  31.    variable arrays with new compound assignment syntax
  32.    redirections: <>, &>, >|, <<<, [n]<&word-, [n]>&word-
  33.    prompt string special char translation and variable expansion
  34.    auto-export of variables in initial environment
  35.    command search finds functions before builtins
  36.    bash return builtin will exit a file sourced with `.'
  37.    builtins: cd -/-L/-P, exec -l/-c/-a, echo -e/-E, hash -d/-l/-p/-t.
  38.         export -n/-f/-p/name=value, pwd -L/-P,
  39.         read -e/-p/-a/-t/-n/-d/-s/-u,
  40.         readonly -a/-f/name=value, trap -l, set +o,
  41.         set -b/-m/-o option/-h/-p/-B/-C/-H/-P,
  42.         unset -f/-v, ulimit -i/-m/-p/-q/-u/-x,
  43.         type -a/-p/-t/-f/-P, suspend -f, kill -n,
  44.         test -o optname/s1 == s2/s1 < s2/s1 > s2/-nt/-ot/-ef/-O/-G/-S
  45.    bash reads ~/.bashrc for interactive shells, $ENV for non-interactive
  46.    bash restricted shell mode is more extensive
  47.    bash allows functions and variables with the same name
  48.    brace expansion
  49.    tilde expansion
  50.    arithmetic expansion with $((...)) and `let' builtin
  51.    the `[[...]]' extended conditional command
  52.    process substitution
  53.    aliases and alias/unalias builtins
  54.    local variables in functions and `local' builtin
  55.    readline and command-line editing with programmable completion
  56.    command history and history/fc builtins
  57.    csh-like history expansion
  58.    other new bash builtins: bind, command, compgen, complete, builtin,
  59.              declare/typeset, dirs, enable, fc, help,
  60.              history, logout, popd, pushd, disown, shopt,
  61.              printf
  62.    exported functions
  63.    filename generation when using output redirection (command >a*)
  64.    POSIX.2-style globbing character classes
  65.    POSIX.2-style globbing equivalence classes
  66.    POSIX.2-style globbing collating symbols
  67.    egrep-like extended pattern matching operators
  68.    case-insensitive pattern matching and globbing
  69.    variable assignments preceding commands affect only that command,
  70.       even for builtins and functions
  71.    posix mode and strict posix conformance
  72.    redirection to /dev/fd/N, /dev/stdin, /dev/stdout, /dev/stderr,
  73.       /dev/tcp/host/port, /dev/udp/host/port
  74.    debugger support, including `caller' builtin and new variables
  75.    RETURN trap
  76.    the `+=' assignment operator
  77. Things sh has that bash does not:
  78.    uses variable SHACCT to do shell accounting
  79.    includes `stop' builtin (bash can use alias stop='kill -s STOP')
  80.    `newgrp' builtin
  81.    turns on job control if called as `jsh'
  82.    $TIMEOUT (like bash $TMOUT)
  83.    `^' is a synonym for `|'
  84.    new SVR4.2 sh builtins: mldmode, priv
  85. Implementation differences:
  86.    redirection to/from compound commands causes sh to create a subshell
  87.    bash does not allow unbalanced quotes; sh silently inserts them at EOF
  88.    bash does not mess with signal 11
  89.    sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100
  90.    bash splits only the results of expansions on IFS, using POSIX.2
  91.       field splitting rules; sh splits all words on IFS
  92.    sh does not allow MAILCHECK to be unset (?)
  93.    sh does not allow traps on SIGALRM or SIGCHLD
  94.    bash allows multiple option arguments when invoked (e.g. -x -v);
  95.       sh allows only a single option argument (`sh -x -v' attempts
  96.       to open a file named `-v', and, on SunOS 4.1.4, dumps core.
  97.       On Solaris 2.4 and earlier versions, sh goes into an infinite
  98.       loop.)
  99.    sh exits a script if any builtin fails; bash exits only if one of
  100.       the POSIX.2 `special' builtins fails
复制代码

论坛徽章:
0
3 [报告]
发表于 2014-03-28 15:25 |只看该作者
回复 2# seesea2517

原来如此,除了临时文件还有没有其他简单的招数


   

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
4 [报告]
发表于 2014-03-28 15:40 |只看该作者
回复 3# ccnuzxg


try:
  1. echo "$str1 $str2" | sed -r ':a;s/\<(.+)\>(.*)\<\1\>/\2/;ta'
复制代码

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
5 [报告]
发表于 2014-03-28 15:40 |只看该作者
回复 3# ccnuzxg


    用bash嘛。。。。想了想没别的简单方案了……

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
6 [报告]
发表于 2014-03-28 15:47 |只看该作者
seesea2517 发表于 2014-03-28 15:40
回复 3# ccnuzxg


对于sh的重定向的办法而言。

如果为了达到你脚本语句的功能,则可以这样(当然多个空格,要不要这么追求完美呢?):
  1. [seesea@UC ~]$ echo "$str1" | tr -d "${str2// }"
  2. 1  3
  3. [seesea@UC ~]$ echo "$str1" | sed -r "s/${str2// /|}//g"
  4. 1  3
复制代码

论坛徽章:
0
7 [报告]
发表于 2014-03-28 17:16 |只看该作者
回复 4# ly5066113

多谢,不过代码看不懂啊,还有2个问题

剔除的时候会多个空格,还有一种情况,如果str2不在str1中,应该是打印出str1 不是str1+str2


   

论坛徽章:
0
8 [报告]
发表于 2014-03-28 17:17 |只看该作者
回复 6# seesea2517

代码不错,
不过俺确实是完美主义,
中间会多出空格,怎么破

   

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
9 [报告]
发表于 2014-03-28 17:19 |只看该作者
回复 8# ccnuzxg

空格过滤一下喽:
  1. [seesea@UC ~]$ echo "$str1" | tr -d "${str2// }" | tr -s " "
  2. 1 3
  3. [seesea@UC ~]$ echo "$str1" | sed -r "s/${str2// /|}//g; s/ +/ /g"
  4. 1 3
复制代码

论坛徽章:
0
10 [报告]
发表于 2014-03-28 17:45 |只看该作者
回复 9# seesea2517

完美解决了 膜拜。。。


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP