2

I have a text file having multiple rows. I am using the while command to read the file line-wise but I need to store the values in separate variables and use them in the trailing shell-script.

The cut command delimits the complete file not the read line.

Example my_file.txt:

d1:jobid1
id2:jobid2

I am using the below while loop to read the file line-wise

while IFS= read -r line; do
    jobid=`cut -d: -f2`
    id=`cut -d: -f1`

#remaining shell-script dependent upon variables#

done < my_file.txt

I want to store the jobid and id for every line in my_file.txt and run the trailing script. Each line is in a new line

Need help to store the values in the variables recursively. If there is a better way to read line wise and delimit and store the two fields separately.

6

cut reads from standard input by default. In your command substitution, standard input is inherited from the loop's standard input (since you provide no other explicit input for the calls to cut). The loop's standard input comes from your input file.

The effect becomes that the first line of your file is read by read, while all other lines are read by the very first call to cut. The value in the jobid variable would therefore be a new-line delimited string of job IDs while the id variable would be empty. The loop would run a single iteration only.

You could use, for example,

id=$( printf '%s\n' "$line" | cut -d: -f1 )

# or: id=$( cut -d: -f1 <<<"$line" )
# (in shells that supports "here-strings")

but that would be unnecessary as it would be cleaner and much more efficient to do

while IFS=: read -r id jobid; do
    # code that uses "$id" and "$jobid" goes here
done <my_file.txt

This uses the fact that read can read data from a line of input into multiple variables at once. By setting IFS to a colon, the line is split on the colon and the first field is read into id while the rest is read into jobid. There is no need to call cut at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.