3

I would like to use awk for a variable
that has the form abc,def

I also don't know how to use awk for a variable instead of a file
I tried the following but it doesn't works

awk -F, '{$1" "$2}' $var
and
awk -F, '{$1" "$2}' "$var"

1
  • What do you want to do with the variable? awk may not be the best tool for the job. Commented May 10, 2014 at 16:34

2 Answers 2

4

Use a herestring

awk 'commands' <<< "$string"

Also if you want to print the first two fields of a comma separated string, change the command to

awk -F, '{print $1, $2}' <<< "$string"
Sign up to request clarification or add additional context in comments.

Comments

4

You can do something like this:

echo "$variable" | awk -F, '{print $1 " " $2}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.