Linux 获取 shell 脚本中每一行的前 5 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/20970975/
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
get the first 5 characters from each line in shell script
提问by user2622247
Here is my sample.txtfile it contains following
这是我的sample.txt文件,它包含以下内容
31113    70:54:D2 - a-31003
31114    70:54:D2 - b-31304
31111    4C:72:B9 - c-31303
31112    4C:72:B9 - d-31302
I have to write the shell script in that I am passing first 5 characters (eg 31113) as input id to other script. For this I have tried this
我必须编写 shell 脚本,因为我将前 5 个字符(例如 31113)作为输入 id 传递给其他脚本。为此我试过这个
#!/bin/sh
filename='sample.txt'
filelines=`cat $filename`
while read -r line
do
  id= cut -c-5 $line
  echo $id
  #code for passing id to other script file as parameter
done < "$filename"
but it is not working this gives me error as
但它不起作用这给了我错误
cut: 31113: No such file or directory
cut: 70:54:D2 No such file or directory
31114
31111
31112
: No such file or directory
How can I do this?
我怎样才能做到这一点?
采纳答案by fedorqui 'SO stop harming'
If you want to use cutthis way, you need to use redirection <<<(a here string) like:
如果要使用cut这种方式,则需要使用重定向<<<(此处的字符串),例如:
var=$(cut -c-5 <<< "$line")
Note the use of var=$(command)expression instead of id= cut -c-5 $line. This is the way to save the command into a variable.
请注意使用var=$(command)expression 而不是id= cut -c-5 $line。这是将命令保存到变量中的方法。
Also, use /bin/bashinstead of /bin/shto have it working.
另外,使用/bin/bash而不是/bin/sh让它工作。
Full code that is working to me:
对我有用的完整代码:
#!/bin/bash
filename='sample.txt'
while read -r line
do
  id=$(cut -c-5 <<< "$line")
  echo $id
  #code for passing id to other script file as parameter
done < "$filename"
回答by vahid abdi
If you trying to fetch the first column from file try awk:
如果您尝试从文件中获取第一列,请尝试awk:
#!/bin/sh
filename='sample.txt'
while read -r line
do
  id=$(echo $line | awk '{print }')
  echo $id
  #code for passing id to other script file as parameter
done < "$filename"
回答by BMW
Maybe you need this, awk can recognize white space automatically.
也许你需要这个,awk 可以自动识别空格。
awk '{print }' sample.txt
回答by William Pursell
Rather than piping echointo cut, just pipe the output of cutdirectly to the while loop:
而不是通过管道echo进入cut,只需将 的输出cut直接管道到while 循环:
cut -c 1-5 sample.txt |
while read -r id; do
  echo $id
  #code for passing id to other script file as parameter
done
回答by tuxdna
Well, its a one-liner cut -c-5 sample.txt. Example:
嗯,它是一个单线cut -c-5 sample.txt。例子:
$ cut -c-5 sample.txt 
31113
31114
31111
31112
From there-on, you can pipe it to any other script or command:
从那时起,您可以将其通过管道传输到任何其他脚本或命令:
$ cut -c-5 sample.txt | while read line; do echo Hello $line; done
Hello 31113
Hello 31114
Hello 31111
Hello 31112
回答by Wybo Dekker
Somewhat simpler than the top answer:
比顶级答案简单一些:
#!/bin/bash
filename='sample.txt'
while read -r line; do
  id=${line:0:5}
  echo $id
  #code for passing id to other script file as parameter
done < "$filename"
回答by kenorb
Please check the following simple example:
请检查以下简单示例:
while read line; do id=$(echo $line | head -c5); echo $id; done < file
where head -c5is the right command to get the first 5 characters from the string.
head -c5从字符串中获取前 5 个字符的正确命令在哪里。

