Given a previously defined $LINE in a shell script, I do the following
var1=$(echo $LINE | cut -d, -f4)
var2=$(echo $LINE | cut -d, -f5)
var3=$(echo $LINE | cut -d, -f6)
Is there any way for me to combine it into one command, where the cut is run only once? Something like
var1,var2,var3=$(echo $LINE | cut -d, -f4,5,6)
Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.
Now if you want to store the output of above cut command in a variable, just wrap the above command in $(…) as shown below. As you can see, $list variable stores the 2nd column of your data. txt file, extracted using cut command.
The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text.
The builtin read
command can assign to multiple variables:
IFS=, read _ _ _ var1 var2 var3 _ <<< "$LINE"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With