[英]Running loop number of times as per number of lines in the content of the text file ( Unix Shell Scripting )
I have some multiple lines in the text file . 我的文本文件中有多行。 Example: I have 3 lines in the text file and 示例:我在文本文件中有3行
Emp_Table_Columns.txt|Employee_Temp.csv|Emp.csv
Dep_Table_Columns.txt|Dep_Temp.csv|Dep.csv
Line_Table_Columns.txt|Line_Temp.csv|Line.csv
I want to run my FOR loop ( or any other loop ) for 3 times and while running i want to pass the values Field 1 , Field 2 , field 3 into a variables. 我想将FOR循环(或任何其他循环)运行3次,而在运行时,我想将值Field 1,Field 2,field 3传递给变量。
Lets say if 1st Loop is running , then i will be reading Emp_Table_Columns.txt . 可以说如果1st Loop正在运行,那么我将在读取Emp_Table_Columns.txt。 If 2nd loop is running i will be reading Dep_Table_Columns.txt. 如果第二循环正在运行,我将读取Dep_Table_Columns.txt。 Please help me with some suggestion in Shell Script or linux. 请在Shell Script或linux中为我提供一些建议。
You don't want a for loop. 您不需要for循环。 You want to iterate over all the lines in the file. 您要遍历文件中的所有行。 eg 例如
while IFS=\| read col1 col2 col3 etc; do
...
done < text_file
In the body of the loop, the variables $col1
, $col2
, and $col3
will contain the values in column 1, column 2, and column 3, respectively. 在循环的主体中,变量$col1
, $col2
和$col3
将分别包含第1列,第2列和第3列中的值。 The variable $etc
will contain columns 4 and beyond (if there is any; you probably expect $etc
to be the empty string). 变量$etc
将包含第4列及以后的列(如果有的话;您可能希望$etc
为空字符串)。 Name the variables whatever you desire, hopefully with names that are descriptive of the contents of the columns. 可以根据需要命名变量,最好使用描述列内容的名称。 Note that we explicitly set IFS
to use |
请注意,我们明确设置了IFS
以使用|
as the column separator, as read
will by default split on whitespace (depending on the current value of IFS.) 作为列分隔符,默认情况下, read
将在空白处进行分割(取决于IFS的当前值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.