【解决方案】mac os 使用sed -i 出现sed: -i may not be used with stdin

本文介绍在MacOS 10.14.6环境下使用sed命令删除指定文件开头几行的具体方法。通过在指令前添加额外的单引号,解决了sed -i may not be used with stdin的问题。
问题:

在写shell 脚本的时候想要删除单个文件夹内所有的文件开头的几行
脚本部分如下:

 for ((i=1;i<=22;i++)); do
    sed -i  '1,6d' $QUERIES_PATH/${i}.sql
 done

在自己mac 上跑的时候出现问题

sed: -i may not be used with stdin

查看处理文本没生效

解决方案

经过问题索引,查阅资料
尝试方案
在sed指令中添加 ''空指令前缀
例如:

 sed -i '1,6d' $QUERIES_PATH/${i}.sql

更改为

 sed -i '' '1,6d' $QUERIES_PATH/${i}.sql

方案源于:
http://www.itkeyword.com/doc/2029474726744985593/sed-i-may-not-be-used-with-stdin-on-mac-os-x

方案测试有效。
ps:系统版本:mac os 10.14.6

if [ "true" = "${IncreaseProjectVersion}" ]; then echo "# -------- Increasing project version" git checkout . # 安全提取当前 revision 值 revision_tag=$(grep "<revision>" pom.xml) revision_value=$(echo "$revision_tag" | sed -E 's/.*<revision>([^<]+)<\/revision>.*/\1/') if [ -z "$revision_value" ]; then echo "Error: Cannot find <revision> in pom.xml" exit 1 fi echo "Found revision: $revision_value" # 拆分版本号 IFS='.' read -r firstVersion middleVersion oldVersion oldVersion2 <<EOF $revision_value EOF # 构造旧字符串和新字符串 if [ -z "$oldVersion2" ]; then # 三段式版本号:x.y.z → x.y.(z+1) newVersion=$((oldVersion + 1)) oldVersionStr="${firstVersion}.${middleVersion}.${oldVersion}" newVersionStr="${firstVersion}.${middleVersion}.${newVersion}" else # 四段式版本号:x.y.z.w → x.y.z.(w+1) newVersion=$((oldVersion2 + 1)) oldVersionStr="${firstVersion}.${middleVersion}.${oldVersion}.${oldVersion2}" newVersionStr="${firstVersion}.${middleVersion}.${oldVersion}.${newVersion}" fi echo "Old version: $oldVersionStr" echo "New version: $newVersionStr" # 替换 pom.xml 中的版本(跨平台兼容) if [[ "$OSTYPE" == "darwin"* ]]; then # macOS sed -i '' "s/${oldVersionStr}/${newVersionStr}/g" pom.xml else # Linux sed -i "s/${oldVersionStr}/${newVersionStr}/g" pom.xml fi cat pom.xml echo "# Push commit to Gerrit" git add pom.xml git commit -m "Jenkins: update pom version to ${newVersionStr}" git push origin HEAD:${BuildBranch#*/} else echo "# -------- No need increase project version" fi 在本地写个脚本测试一下,我的文件路径为D:pom.xml
最新发布
10-25
(repo-venv) user@user-HP-Pavilion-Gaming-Desktop-690-07xx:~/Code/Polus/VSB-0804$ repo init -u ssh://gerrit6.labcollab.net:9418/vendor/qualcomm/yocto/echo/qcom-yocto-manifest -b vega/echo/mainline --repo-url=ssh://gerrit6.labcollab.net:9418/amazon/repo --repo-branch=stable Traceback (most recent call last): File "/home/user/Code/Polus/VSB-0804/.repo/repo/main.py", line 54, in <module> from subcmds.version import Version File "/home/user/Code/Polus/VSB-0804/.repo/repo/subcmds/__init__.py", line 34, in <module> mod = __import__(__name__, File "/home/user/Code/Polus/VSB-0804/.repo/repo/subcmds/help.py", line 20, in <module> from formatter import AbstractFormatter, DumbWriter File "/home/user/Code/Polus/VSB-0804/.repo/repo/formatter.py", line 327 print "new_alignment(%r)" % (align,) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? (repo-venv) user@user-HP-Pavilion-Gaming-Desktop-690-07xx:~/Code/Polus/VSB-0804$ cat .repo/repo/formatter.py """Generic output formatting. Formatter objects transform an abstract flow of formatting events into specific output events on writer objects. Formatters manage several stack structures to allow various properties of a writer object to be changed and restored; writers need not be able to handle relative changes nor any sort of ``change back'' operation. Specific writer properties which may be controlled via formatter objects are horizontal alignment, font, and left margin indentations. A mechanism is provided which supports providing arbitrary, non-exclusive style settings to a writer as well. Additional interfaces facilitate formatting events which are not reversible, such as paragraph separation. Writer objects encapsulate device interfaces. Abstract devices, such as file formats, are supported as well as physical devices. The provided implementations all work with abstract devices. The interface makes available mechanisms for setting the properties which formatter objects manage and inserting data into the output. """ import sys AS_IS = None class NullFormatter: """A formatter which does nothing. If the writer parameter is omitted, a NullWriter instance is created. No methods of the writer are called by NullFormatter instances. Implementations should inherit from this class if implementing a writer interface but don't need to inherit any implementation. """ def __init__(self, writer=None): if writer is None: writer = NullWriter() self.writer = writer def end_paragraph(self, blankline): pass def add_line_break(self): pass def add_hor_rule(self, *args, **kw): pass def add_label_data(self, format, counter, blankline=None): pass def add_flowing_data(self, data): pass def add_literal_data(self, data): pass def flush_softspace(self): pass def push_alignment(self, align): pass def pop_alignment(self): pass def push_font(self, x): pass def pop_font(self): pass def push_margin(self, margin): pass def pop_margin(self): pass def set_spacing(self, spacing): pass def push_style(self, *styles): pass def pop_style(self, n=1): pass def assert_line_data(self, flag=1): pass class AbstractFormatter: """The standard formatter. This implementation has demonstrated wide applicability to many writers, and may be used directly in most circumstances. It has been used to implement a full-featured World Wide Web browser. """ # Space handling policy: blank spaces at the boundary between elements # are handled by the outermost context. "Literal" data is not checked # to determine context, so spaces in literal data are handled directly # in all circumstances. def __init__(self, writer): self.writer = writer # Output device self.align = None # Current alignment self.align_stack = [] # Alignment stack self.font_stack = [] # Font state self.margin_stack = [] # Margin state self.spacing = None # Vertical spacing state self.style_stack = [] # Other state, e.g. color self.nospace = 1 # Should leading space be suppressed self.softspace = 0 # Should a space be inserted self.para_end = 1 # Just ended a paragraph self.parskip = 0 # Skipped space between paragraphs? self.hard_break = 1 # Have a hard break self.have_label = 0 def end_paragraph(self, blankline): if not self.hard_break: self.writer.send_line_break() self.have_label = 0 if self.parskip < blankline and not self.have_label: self.writer.send_paragraph(blankline - self.parskip) self.parskip = blankline self.have_label = 0 self.hard_break = self.nospace = self.para_end = 1 self.softspace = 0 def add_line_break(self): if not (self.hard_break or self.para_end): self.writer.send_line_break() self.have_label = self.parskip = 0 self.hard_break = self.nospace = 1 self.softspace = 0 def add_hor_rule(self, *args, **kw): if not self.hard_break: self.writer.send_line_break() self.writer.send_hor_rule(*args, **kw) self.hard_break = self.nospace = 1 self.have_label = self.para_end = self.softspace = self.parskip = 0 def add_label_data(self, format, counter, blankline = None): if self.have_label or not self.hard_break: self.writer.send_line_break() if not self.para_end: self.writer.send_paragraph((blankline and 1) or 0) if isinstance(format, str): self.writer.send_label_data(self.format_counter(format, counter)) else: self.writer.send_label_data(format) self.nospace = self.have_label = self.hard_break = self.para_end = 1 self.softspace = self.parskip = 0 def format_counter(self, format, counter): label = '' for c in format: if c == '1': label = label + ('%d' % counter) elif c in 'aA': if counter > 0: label = label + self.format_letter(c, counter) elif c in 'iI': if counter > 0: label = label + self.format_roman(c, counter) else: label = label + c return label def format_letter(self, case, counter): label = '' while counter > 0: counter, x = divmod(counter-1, 26) # This makes a strong assumption that lowercase letters # and uppercase letters form two contiguous blocks, with # letters in order! s = chr(ord(case) + x) label = s + label return label def format_roman(self, case, counter): ones = ['i', 'x', 'c', 'm'] fives = ['v', 'l', 'd'] label, index = '', 0 # This will die of IndexError when counter is too big while counter > 0: counter, x = divmod(counter, 10) if x == 9: label = ones[index] + ones[index+1] + label elif x == 4: label = ones[index] + fives[index] + label else: if x >= 5: s = fives[index] x = x-5 else: s = '' s = s + ones[index]*x label = s + label index = index + 1 if case == 'I': return label.upper() return label def add_flowing_data(self, data): if not data: return prespace = data[:1].isspace() postspace = data[-1:].isspace() data = " ".join(data.split()) if self.nospace and not data: return elif prespace or self.softspace: if not data: if not self.nospace: self.softspace = 1 self.parskip = 0 return if not self.nospace: data = ' ' + data self.hard_break = self.nospace = self.para_end = \ self.parskip = self.have_label = 0 self.softspace = postspace self.writer.send_flowing_data(data) def add_literal_data(self, data): if not data: return if self.softspace: self.writer.send_flowing_data(" ") self.hard_break = data[-1:] == '\n' self.nospace = self.para_end = self.softspace = \ self.parskip = self.have_label = 0 self.writer.send_literal_data(data) def flush_softspace(self): if self.softspace: self.hard_break = self.para_end = self.parskip = \ self.have_label = self.softspace = 0 self.nospace = 1 self.writer.send_flowing_data(' ') def push_alignment(self, align): if align and align != self.align: self.writer.new_alignment(align) self.align = align self.align_stack.append(align) else: self.align_stack.append(self.align) def pop_alignment(self): if self.align_stack: del self.align_stack[-1] if self.align_stack: self.align = align = self.align_stack[-1] self.writer.new_alignment(align) else: self.align = None self.writer.new_alignment(None) def push_font(self, font): size, i, b, tt = font if self.softspace: self.hard_break = self.para_end = self.softspace = 0 self.nospace = 1 self.writer.send_flowing_data(' ') if self.font_stack: csize, ci, cb, ctt = self.font_stack[-1] if size is AS_IS: size = csize if i is AS_IS: i = ci if b is AS_IS: b = cb if tt is AS_IS: tt = ctt font = (size, i, b, tt) self.font_stack.append(font) self.writer.new_font(font) def pop_font(self): if self.font_stack: del self.font_stack[-1] if self.font_stack: font = self.font_stack[-1] else: font = None self.writer.new_font(font) def push_margin(self, margin): self.margin_stack.append(margin) fstack = filter(None, self.margin_stack) if not margin and fstack: margin = fstack[-1] self.writer.new_margin(margin, len(fstack)) def pop_margin(self): if self.margin_stack: del self.margin_stack[-1] fstack = filter(None, self.margin_stack) if fstack: margin = fstack[-1] else: margin = None self.writer.new_margin(margin, len(fstack)) def set_spacing(self, spacing): self.spacing = spacing self.writer.new_spacing(spacing) def push_style(self, *styles): if self.softspace: self.hard_break = self.para_end = self.softspace = 0 self.nospace = 1 self.writer.send_flowing_data(' ') for style in styles: self.style_stack.append(style) self.writer.new_styles(tuple(self.style_stack)) def pop_style(self, n=1): del self.style_stack[-n:] self.writer.new_styles(tuple(self.style_stack)) def assert_line_data(self, flag=1): self.nospace = self.hard_break = not flag self.para_end = self.parskip = self.have_label = 0 class NullWriter: """Minimal writer interface to use in testing & inheritance. A writer which only provides the interface definition; no actions are taken on any methods. This should be the base class for all writers which do not need to inherit any implementation methods. """ def __init__(self): pass def flush(self): pass def new_alignment(self, align): pass def new_font(self, font): pass def new_margin(self, margin, level): pass def new_spacing(self, spacing): pass def new_styles(self, styles): pass def send_paragraph(self, blankline): pass def send_line_break(self): pass def send_hor_rule(self, *args, **kw): pass def send_label_data(self, data): pass def send_flowing_data(self, data): pass def send_literal_data(self, data): pass class AbstractWriter(NullWriter): """A writer which can be used in debugging formatters, but not much else. Each method simply announces itself by printing its name and arguments on standard output. """ def new_alignment(self, align): print "new_alignment(%r)" % (align,) def new_font(self, font): print "new_font(%r)" % (font,) def new_margin(self, margin, level): print "new_margin(%r, %d)" % (margin, level) def new_spacing(self, spacing): print "new_spacing(%r)" % (spacing,) def new_styles(self, styles): print "new_styles(%r)" % (styles,) def send_paragraph(self, blankline): print "send_paragraph(%r)" % (blankline,) def send_line_break(self): print "send_line_break()" def send_hor_rule(self, *args, **kw): print "send_hor_rule()" def send_label_data(self, data): print "send_label_data(%r)" % (data,) def send_flowing_data(self, data): print "send_flowing_data(%r)" % (data,) def send_literal_data(self, data): print "send_literal_data(%r)" % (data,) class DumbWriter(NullWriter): """Simple writer class which writes output on the file object passed in as the file parameter or, if file is omitted, on standard output. The output is simply word-wrapped to the number of columns specified by the maxcol parameter. This class is suitable for reflowing a sequence of paragraphs. """ def __init__(self, file=None, maxcol=72): self.file = file or sys.stdout self.maxcol = maxcol NullWriter.__init__(self) self.reset() def reset(self): self.col = 0 self.atbreak = 0 def send_paragraph(self, blankline): self.file.write('\n'*blankline) self.col = 0 self.atbreak = 0 def send_line_break(self): self.file.write('\n') self.col = 0 self.atbreak = 0 def send_hor_rule(self, *args, **kw): self.file.write('\n') self.file.write('-'*self.maxcol) self.file.write('\n') self.col = 0 self.atbreak = 0 def send_literal_data(self, data): self.file.write(data) i = data.rfind('\n') if i >= 0: self.col = 0 data = data[i+1:] data = data.expandtabs() self.col = self.col + len(data) self.atbreak = 0 def send_flowing_data(self, data): if not data: return atbreak = self.atbreak or data[0].isspace() col = self.col maxcol = self.maxcol write = self.file.write for word in data.split(): if atbreak: if col + len(word) >= maxcol: write('\n') col = 0 else: write(' ') col = col + 1 write(word) col = col + len(word) atbreak = 1 self.col = col self.atbreak = data[-1].isspace() def test(file = None): w = DumbWriter() f = AbstractFormatter(w) if file is not None: fp = open(file) elif sys.argv[1:]: fp = open(sys.argv[1]) else: fp = sys.stdin for line in fp: if line == '\n': f.end_paragraph(1) else: f.add_flowing_data(line) f.end_paragraph(0) if __name__ == '__main__': test()
08-06
<think>好的,我现在需要帮助用户理解和修正复杂的sed命令中的多分隔符替换语法,特别是关于使用s:::这样的分隔符。首先,我要确保自己完全理解这个问题,然后整理出清晰的步骤和例子,用中文回答用户。 首先,用户提到了sed命令中的多分隔符替换语法,比如s:::。我知道在sed中,替换命令的基本结构是s/old/new/,其中斜杠/是默认的分隔符。但有时候,当替换的字符串中包含斜杠时,为了避免转义麻烦,可以使用其他分隔符,比如冒号:、井号#或者竖线|等。用户的问题是如何正确使用这些不同的分隔符,尤其是当需要在一个命令中使用多个不同的分隔符时,如何处理。 接下来,我需要回忆sed的相关文档和使用案例。例如,当替换路径中的斜杠时,使用冒号作为分隔符会更方便,比如s:/old/path:/new/path:。这样就不需要对路径中的斜杠进行转义了。另外,用户可能遇到的情况是在同一个sed命令中需要执行多个替换操作,每个替换使用不同的分隔符,这时候需要注意分隔符的选择和命令的分隔方式,比如用分号;分隔不同的替换命令。 然后,我需要考虑用户可能的常见错误。例如,如果在替换字符串中使用了和分隔符相同的字符,但没有正确转义,会导致命令解析错误。比如,如果使用冒号作为分隔符,但替换内容中也包含冒号,这时需要转义那个冒号,或者选择另一个分隔符。此外,当在命令行中使用特殊字符时,可能需要用引号包裹整个表达式,避免shell的解析问题。 接下来,我需要组织回答的结构。首先解释分隔符的作用和为什么需要更换分隔符,然后给出基本语法,接着是多个例子说明不同情况的使用方法,包括单一替换、多个替换命令的组合,以及处理包含特殊字符的情况。同时,需要指出常见错误和注意事项,比如分隔符的一致性、转义处理,以及-i选项的使用注意事项。 还需要检查是否有相关的引用可以支持回答。根据用户提供的引用,引用[1]提到可以在同一个sed命令中使用多个替换规则,用分号分隔,引用[2]和[3]涉及基本的替换语法和正则表达式使用。这些可以适当引用,增强回答的可信度。 最后,生成相关问题,帮助用户进一步学习,比如如何同时使用多个替换规则,或者如何处理特殊字符的替换。 现在,我需要确保所有数学公式或代码块都符合用户给定的格式要求。例如,行内代码用$...$,独立代码段用```包裹,并且正确使用中文回答。同时,检查是否有遗漏的注意事项,比如分隔符不能出现在替换内容中,或者使用-i选项备份文件的重要性。 总结思考步骤后,就可以开始撰写结构清晰的回答,分步骤解释,并附上示例代码,确保用户能够逐步理解和应用正确的sed命令语法。</think>### sed命令多分隔符替换语法详解 在sed命令中,分隔符的主要作用是区分替换命令的各个部分(匹配模式、替换内容、修饰符)。默认使用斜杠`/`作为分隔符,但可通过自由选择其他符号(如`:`、`#`、`|`)优化可读性并减少转义需求。 #### 一、基本语法结构 替换命令格式为: $$s[分隔符][匹配内容][分隔符][替换内容][分隔符][修饰符]$$ 例如: ```bash # 默认分隔符 sed 's/old/new/g' # 使用冒号作为分隔符 sed 's:old:new:g' # 使用井号作为分隔符 sed 's#old#new#g' ``` #### 二、多分隔符应用场景 1. **路径替换**(避免转义斜杠) ```bash # 将路径中的/usr/local替换为/opt sed 's:/usr/local:/opt:g' file.txt ``` 2. **特殊字符处理**(如替换包含斜杠的URL) ```bash # 替换http://为https:// sed 's#http://#https://#g' file.txt ``` 3. **多规则混合分隔符** ```bash # 第一个替换用冒号,第二个用井号 sed 's:pattern1:replacement1:g; s#pattern2#replacement2#g' file.txt ``` #### 三、常见错误与解决方法 | 错误类型 | 示例 | 修正方法 | |-------------------|---------------------|-----------------------------| | 分隔符未转义 | `s:/a/b:/a/c:g` | 替换为`sed 's:/a/b:/a/c:g'` | | 混合分隔符未分隔 | `s:pat#rep:g` | 统一分隔符或转义`#` | | 修饰符位置错误 | `s/old/new/g/` | 移除多余斜杠`sed 's/old/new/g'` | #### 四、高级操作示例 1. **同时替换多组内容** ```bash # 替换日期格式(/ -> -)和路径(/usr -> /opt) sed -e 's#/usr#/opt#g' -e 's:2023/01/01:2023-01-01:g' file.txt ``` 2. **处理含分隔符的替换内容** ```bash # 替换包含冒号的字符串(需转义) sed 's:name\:old:name:new:g' data.txt ``` [^1]: 引用说明:多替换规则通过分号分隔实现组合操作。 [^2]: 引用说明:`-i`参数直接修改文件时建议先备份,例如`sed -i.bak 's/old/new/g' file.txt`。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值