简体   繁体   中英

Linux substring (cut) output in shell script

In Linux (Raspbian distro) I'm trying to extract the date part of a file name which works when I type it in directly into the terminal (see below).

$ file1="access_point20140821.csv"
$ echo $file1 | cut -c13-20
$ 20140821

However, when I put this into a shell script I can't seem to extract the date part of the file name. The echo line just returns "Date Part" with nothing following it. I suspect this is something to do with how I am assigning the variable DATE_PART. Can anyone help?

FILENAME="access_point20140821.csv"
DATE_PART=$FILENAME | cut -c13-20
echo "Date Part $DATE_PART"

You are not echoing the file, you are trying to execute it, you should do:

FILENAME="access_point20140821.csv"
DATE_PART=$(echo $FILENAME | cut -c13-20)
echo "Date Part $DATE_PART"

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