简体   繁体   中英

how do I store the output of a cut -c command into a variable in shell script

I have a file name like

Incoming_file_180420053826.csv

where 180420 represents the date and 053826 represents time.
I have used echo $( cut -c 15-20 $filename ) to retrieve the date, but I'm unable to store that output date in a variable.

Try

my_var="$( echo  $filename | cut -c 15-20 )"

Demo:

$filename=Incoming_file_180420053826.csv 
$my_var="$( echo  $filename | cut -c 15-20 )"
$echo $my_var 
180420
$
filename=test.txt
aa=$( cut -c 1-6 $filename )
echo $aa

180420 180421 180422 180423 180424 180425

But I think you maybe just need first line, maybe this is what you need.

bb=$(head -1 $file|cut -c 1-6)
echo $bb

180420

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM