0

I'm new in bash linux coding. Basically I would like to do a loop using a text file with two columns.

For a single column file I use this type of code:

for element in `cat /path/file.txt | tr -d '\r'`
do
operation on element
done

I would like to use the two elements of each line as argument for a function. So it would be like:

file.txt:

Column A Column B
a c
b d

And the using in the same line of code the first row of column A and B, then the second row and go on...

I don't know if it is possible to use indexes to specify the row and column that I want for each iteration.

4
  • 1
    You are new to bash, so you may not be aware that $() has been preferred over backticks for at least a few decades. Commented Apr 5, 2022 at 12:45
  • Also, there's no need to use cat; you can write $(tr -d '\r' < /path/file.txt) Commented Apr 5, 2022 at 12:46
  • 1
    You want to read the file line by line using the solutions from Looping through the content of a file in Bash Commented Apr 5, 2022 at 12:53
  • How are the column represented inside the file? Commented Apr 5, 2022 at 13:29

1 Answer 1

2

It looks like you want:

tr -d '\r' < /path/file.txt | 
while read a b; do
  operate "$a" "$b"
done
Sign up to request clarification or add additional context in comments.

1 Comment

Probably also add read -r in case the data contains backslashes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.