Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple cut commands into variable assignations into one cut command

Tags:

bash

shell

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)
like image 563
saccharine Avatar asked Dec 21 '12 23:12

saccharine


People also ask

How to combine two commands in linux?

Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.

How do you store the output of Cut command in a variable?

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.

How do you cut a variable command in Unix?

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.


1 Answers

The builtin read command can assign to multiple variables:

IFS=, read _ _ _ var1 var2 var3 _ <<< "$LINE"
like image 190
glenn jackman Avatar answered Oct 04 '22 07:10

glenn jackman