0

My intention is to take the second line and look for the characters after the . delimiter. It works like this:

prompt> awk -F '.' 'FNR == 2 {print $NF}' file_name.txt
Sam

However, I wish to store that output in the variable, but its showing an error:

prompt> Name= $(awk -F '.' 'FNR == 2 {print $NF}' file_name.txt)
Sam: command not found

How do I fix that?

0

1 Answer 1

4

You should not have a space between Name= and $(awk. That turns it into a temporary variable assignment for one command, similar to the difference between:

name=bob    # sets name to bob
name= bob   # sets name to nothing while running bob

In other words, use:

prompt> Name=$(awk -F '.' 'FNR == 2 {print $NF}' file_name.txt)
prompt> echo $Name
Sam
Sign up to request clarification or add additional context in comments.

2 Comments

$ test23=awk -F '.' 'FNR == 2 {print $NF}' file_name.txt -ksh: -F: not found [No such file or directory] $ awk -F '.' 'FNR == 2 {print $NF}' file_name.txt Sam $ doesnot work!!
@PiyushAnand, why are you testing something different to what I suggested? :-) You're missing the $() stuff around the awk command. That makes it an attempt to set test23 to awk then run the -F command.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.