简体   繁体   中英

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

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.

Lets say if 1st Loop is running , then i will be reading Emp_Table_Columns.txt . If 2nd loop is running i will be reading Dep_Table_Columns.txt. Please help me with some suggestion in Shell Script or linux.

You don't want a for loop. 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. The variable $etc will contain columns 4 and beyond (if there is any; you probably expect $etc to be the empty string). 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 | as the column separator, as read will by default split on whitespace (depending on the current value of IFS.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM