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 )
bjeAsked:
Who is Participating?
 
MikeOM_DBACommented:
Instead of sed, you can use the gsub awk command.
name=$(awk -F"," 'NR==18 {gsub(/ /,"",$2 }' $file)

Open in new window

:p
0
 
woolmilkporcCommented:
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!
0
 
simon3270Commented:
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

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MikeOM_DBACommented:
Thanks simon, It's Friday dyslexia!
0
 
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
  then
     do something
 else
    do something
fi
0
 
woolmilkporcCommented:
You can use "cut", but it wants to read from stdin or from a file.

bash:

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

bash's builtin variable editing can be used alternatively:

first=${name:0:1}

non-bash:

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.
0
 
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
0
 
simon3270Commented:
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.
0
 
bjeAuthor Commented:
My apologies for the confusion.  Thank you
0
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.

All Courses

From novice to tech pro — start learning today.