通过猛砸文件的内容循环? [英] Looping through the content of a file in Bash?

查看:105
本文介绍了通过猛砸文件的内容循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过一个文本文件中的每一行与迭代猛砸

使用这个脚本

 回声开始!
在P(peptides.txt)

    回声$ {P}
DONE

我得到在屏幕上输出如下:

 开始!
./runPep.sh:3号线:附近意外的标记语法错误'('
./runPep.sh:3号线:在(peptides.txt)`为P'

(后来我想要做一些更复杂的使用$ P比
只输出到屏幕上。)


环境变量SHELL是(从ENV):

  SHELL = /斌/庆典

/斌/ bash的--version 输出:

  GNU bash的版本3.1.17(1) - 释放下(x86_64-SUSE-Linux的GNU)
版权所有(C)2005自由软件基金会

执行cat / proc /版本输出:

  Linux版本的2.6.18.2-34默认(geeko @ buildhost)(gcc版本4.1.2 20061115($ P $租赁前)(的SUSE Linux))#1 SMP周一11月27日11时46分27秒2006年UTC

文件peptides.txt包括:

  RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL


解决方案

正确的语法是:

 而读磷;做
  回声$ P
完成< peptides.txt


在特殊情况下,如果在<一href=\"http://unix.stackexchange.com/questions/107800/using-while-loop-to-ssh-to-multiple-servers\">loop体可以从标准输入阅读,你可以使用不同的文件描述符打开文件:

 同时读取-u 10磷;做
  ...
做10下; peptides.txt

下面,图10是刚任意数(从0不同,1,2)。

How do I iterate through each line of a text file with Bash?

With this script

echo "Start!"
for p in (peptides.txt)
do
    echo "${p}"
done

I get this output on the screen:

Start!
./runPep.sh: line 3: syntax error near unexpected token `('
./runPep.sh: line 3: `for p in (peptides.txt)'

(Later I want to do something more complicated with $p than just output to the screen.)


The environment variable SHELL is (from env):

SHELL=/bin/bash

/bin/bash --version output:

GNU bash, version 3.1.17(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

cat /proc/version output:

Linux version 2.6.18.2-34-default (geeko@buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Mon Nov 27 11:46:27 UTC 2006

The file peptides.txt contains:

RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL

解决方案

The correct syntax is:

while read p; do
  echo $p
done <peptides.txt


Exceptionally, if the loop body may read from standard input, you can open the file using a different file descriptor:

while read -u 10 p; do
  ...
done 10<peptides.txt

Here, 10 is just an arbitrary number (different from 0, 1, 2).

这篇关于通过猛砸文件的内容循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆