Softcoding Awk file separator

I need to be able to soft code the Awk FS element, but the attached script doesn't do it, presumably because the awk program doesn't see the effectively external variable $SEPARATOR

Is this possible?  Ultimately I am trying to extract the part of the file name before the specified separator (in this case #), giving abc


#!/bin/bash

FULLFILEPATH="/tmp/abc#123"
SEPARATOR="#"
FILENAME=`basename $FULLFILEPATH`

echo $FILENAME	# Check we have filename part

echo $FILENAME | awk 'BEGIN { FS = "#" };  {print $1}'  # This works with hardcoded FS

echo $FILENAME | awk 'BEGIN { FS = "$SEPARATOR" };  {print $1}'  # This doesn't

Open in new window

brothertomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Hi,

two options:

1) Use the "-F"  flag of awk:

awk -F"$SEPARATOR" '{print $1}'

2) Use quoting

echo $FILENAME | awk 'BEGIN { FS = "'$SEPARATOR'" };  {print $1}'

Note the single quotes ( ' ' ) directly around $SEPARATOR

wmp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woolmilkporcCommented:
A solution using just bash ("Variable Editing"):

echo ${FILENAME%%"$SEPARATOR"*}
0
woolmilkporcCommented:
Or even without any external tools like "basename" or "awk":

FILENAME=${FULLFILEPATH##*"/"}
SEPARATOR="#"
FILENAME=${FILENAME%%"$SEPARATOR"*}
echo $FILENAME
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

brothertomAuthor Commented:
Many thanks - used the awk -F flag
0
brothertomAuthor Commented:
As a matter of interest, what's going on in the FILENAME=${FULLFILEPATH##*"/"} code?

0
woolmilkporcCommented:
## means: delete the longest leftmost part of the variable's content up to (and including) the given expression (*"/" = a slash and everything before it).

%% means: delete the longest rightmost part of the variable's content up to (and including) the given expression ("$SEPARATOR"* = the separator character and everything which follows).

Thx for the points!

wmp
0
woolmilkporcCommented:
Forgot:

A single "#" or "%" means the shortest leftmost resp. rightmost part ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.