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



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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.


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


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
A solution using just bash ("Variable Editing"):

Or even without any external tools like "basename" or "awk":

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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

## 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!


A single "#" or "%" means the shortest leftmost resp. rightmost part ...
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.