isme
isme

Reputation: 190

shell scripting: how to cut line in variable

I'm new to shell scripting. I've tried to search online, but I couldn't find what I was looking for - how do I cut a variable to get the value I'm looking for?

for example I have:

Result=`awk -F : -v "Title=" -v "Author=" 'tolower() == tolower(Title) && tolower() == tolower(Author)' BookDB.txt`
//which will return:
//Result= Black:Hat:12.30:20:30

I've tried doing this, but it won't work:

PRICE= cut -d ":" -f 3 $Result

Any help would be appreciated, thanks!

Upvotes: 1

Views: 1592

Answers (2)

Jose Martinez
Jose Martinez

Reputation: 11992

How about using awk...

input="Black:Hat:12.30:20:30"
first=$(echo input | awk -F":" '{print $1}')
echo $first
second=$(echo $input | awk -F":" '{print $2}')
echo $second
date=$(echo $input | awk -F":" '{print $3 ":" $4 ":" $5}')
echo $date

You might have to edit to fit your exact requirements.

Upvotes: 1

Serapheim
Serapheim

Reputation: 66

Your code is not wrong ... well, at least most of it!

Doing echo 'Result= Black:Hat:12.30:20:30' | cut -d ":" -f 3 will give the result 12.30.

The issue is that you probably want to use it on a shell script. To do that just try the following: PRICE=`cut -d ":" -f 3 $Result`

What I did was basically putting ` before and after the expression that you want to store in your variable.

Reference to learn more: http://www.freeos.com/guides/lsst/ch02sec08.html

Best of luck!

Upvotes: 1

Related Questions