Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个文件,我希望在整个文件中只替换一次字符串,即它找到的第一个匹配项应该被替换。sed -i 's/foo/foo1/1'
尝试以下操作:
sed -i '0,/foo/s//foo1/' file
这里,
/foo/将获取foo第一次出现的行号,比如说#n
/foo/
#n
然后将搜索相同的单词直到 line #n并替换为foo1
foo1
awk '!f{f=sub(/foo/,"foo1")} 1' file