bash 剪切命令换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9587725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cut command newline
提问by pedr0
I have a file like this:
我有一个这样的文件:
one
two
three
four
I would like use a for loop in a bash script in order to scan the file line for line. Previously I used cut
but I was not able to give the cut
command the newline delimiter, how can I that?
我想在 bash 脚本中使用 for 循环来扫描文件行。以前我使用过,cut
但我无法为cut
命令提供换行符分隔符,我该怎么做?
In this way it does not work :
这样就行不通了:
cut -d'\n' -f1
Any suggestion ?
有什么建议吗?
采纳答案by Benoit
Use cat
for concatenating or displaying. No need for it here:
使用cat
了串联或显示。这里不需要它:
file="/path/to/file"
while read line ; do
echo "${line}"
done < "${file}"
回答by Ivan
I found myself in the same problem, this works for me:
我发现自己遇到了同样的问题,这对我有用:
cat file.cut | cut -d$'\n' -f1
Or:
或者:
cut -d$'\n' -f1 file.cut
回答by Philipp Murry
Simply use:
只需使用:
echo -n `cut ...`
This suppresses the \n at the end
这会抑制末尾的 \n
回答by DOK
So, some really good (possibly better) answers have been provided already. But looking at the phrasing of the original question, in wanting to use a BASH for-loop, it amazed me that nobody mentioned a solution with change of Field Separator IFS. It's a pure bash solution, just like the accepted read line
因此,已经提供了一些非常好的(可能更好的)答案。但是看看原始问题的措辞,想要使用 BASH for 循环,让我惊讶的是没有人提到改变 Field Separator IFS 的解决方案。这是一个纯粹的 bash 解决方案,就像接受的阅读行一样
old_IFS=$IFS
IFS='\n'
for field in $(<filename)
do your_thing;
done
IFS=$old_IFS
回答by MisterStrickland
If you are sure that the output will always be newline-delimited, use head -n 1
in lieu of cut -f1
(note that you mentioned a for loop in a script and your question was ultimately not script-related).
如果您确定输出将始终以换行符分隔,请使用head -n 1
代替cut -f1
(请注意,您在脚本中提到了 for 循环,并且您的问题最终与脚本无关)。
Many of the other answers, including the accepted one, have multiple lines unnecessarily. No need to do this over multiple lines or changing the default delimiter on the system.
许多其他答案,包括已接受的答案,都不必要地有多行。无需在多行上执行此操作或更改系统上的默认分隔符。
Also, the solution provided by Ivan with -d$'\n'
did not work for me either on Mac OSX or CentOS 7. Since his answer is four years old, I assume something must have changed on the logic of the $
character for this situation.
此外,Ivan with 提供的解决方案-d$'\n'
在 Mac OSX 或 CentOS 7 上对我都不起作用。由于他的回答已经四年了,我认为在$
这种情况下,角色的逻辑必须有所改变。
回答by 0xC0000022L
cat FILE|while read line; do # 'line' is the variable name
echo "$line" # do something here
done
or (see comment):
或(见评论):
while read line; do # 'line' is the variable name
echo "$line" # do something here
done < FILE
回答by Umae
My opinion is that "cut" uses '\n' as its default delimiter. If you want to use cut, I have two ways:
我的观点是“cut”使用 '\n' 作为其默认分隔符。如果要使用cut,我有两种方法:
cut -d^M -f1 file_cut
I make ^M By click Enter After Ctrl+V. Another way is
我通过在 Ctrl+V 后单击 Enter 来制作 ^M。另一种方式是
cut -c 1- file_cut
Does that help?
这有帮助吗?