how to use awk and sed in the same command

Trying to use awk and sed together

Have a file and would like to pull out of the file a value and put into a variable.

this is what i have.  need to remove the spaces in the vaule

name=$(awk -F"," 'NR==18 {print $2}' | sed 's/ //g' $file )
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.

Instead of sed, you can use the gsub awk command.
name=$(awk -F"," 'NR==18 {gsub(/ /,"",$2 }' $file)

Open in new window


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
If you want to keep using your version do it this way:

name=$(awk -F"," 'NR==18 {print $2}' $file | sed 's/ //g' )

$file is a parameter to awk, sed just reads from stdin here and thus doesn't need a filename.

But pure awk as suggested by MikeOM_DBA is certainly better!
I certainly prefer Mike's solution (I keep on seeing sequences of sed, grep, tail, awk and others in scripts, thinking that a simple awk script would do all of that in one go).

I think there's a bit missing from the solution though - a trailing ")" and the "print $2":
name=$(awk -F"," 'NR==18 {gsub(/ /,"",$2); print $2 }' $file)

Open in new window

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Thanks simon, It's Friday dyslexia!
bjeAuthor Commented:
is cut the best way to determine the first position of a value

so for my variable name, will need to pull out the first position and put into a variable and if it begins with a 4 then do something.

first=$(cut -c1 $name)
 if $first = 4
     do something
    do something
You can use "cut", but it wants to read from stdin or from a file.


first=$(cut -c1 <<< $name)

bash's builtin variable editing can be used alternatively:



first=$(echo $name | cut -c1)

But your awk can be enhanced to do it for you (if you don't need the full "name" variable for other purposes, that is):

first=$(awk -F"," 'NR==18 {gsub(/ /,"",$2); print substr($2,1,1) }' $file)

Now $first  just consists of the first character of the field in question.
bjeAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 200 points for woolmilkporc's comment #a40767584
Assisted answer: 150 points for woolmilkporc's comment #a40767044
Assisted answer: 150 points for simon3270's comment #a40767464
Assisted answer: 0 points for bje's comment #a40767577

for the following reason:

Thank you
Mike should have the points for #40767023 - all I did in #a40767464 was add a small missing section - the idea of sticking with just awk was his.
bjeAuthor Commented:
My apologies for the confusion.  Thank you
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.