linux - how to convert the script from using command,read to commmand,cut? -


here test sample:

test_catalog,test_title,test_type,test_artist 

and can use following sript cut off text above comma , set variable respectively:

ifs="," read cdcatnum cdtitle cdtype cdac < $temp_file 

(ps:and $temp_file dir of test sample)

and if want replace read command,cut.any idea?

there many solutions:

line=$(head -1 "$temp_file") echo $line | cut -d, ... 

or

cut -d, ... <<< "$line" 

or can tell bash copy line array:

typeset ifs=, set -a array $(head -1 "$temp_file") # use echo $array[0] # test_catalog echo $array[1] # test_title ... 

i prefer array solution because gives distinct data type , communicates intent. echo/cut solution slower.

[edit] on other hand, read command splits line individual variables gives each value name. more readable: $array[0] or $cdcatnum?

if move columns around, need rearrange arguments read command - if use arrays, have update array indices will wrong.

also read makes more simple process whole file:

while read cdcatnum cdtitle cdtype cdac ;     .... done < "$temp_file" 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -