awk for variable with a space

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk for variable with a space
# 1  
Old 09-29-2011
awk for variable with a space

Hi experts,

why does
$ echo "one two three" | awk '{$3="my tree"; print $0}'
one two my tree

$ var="my tree"
$ echo "one two three" | awk '{$3="'$var'"; print $0}'
awk: {$3="my
awk:     ^ unterminated string

does not work?
How can the variable tha contains a space be forced into the field?

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 09-30-2011 at 03:58 AM.. Reason: Please use code tags, thank you
# 2  
Old 09-30-2011
$ var="test"
$ echo "one two three" | awk '{$3="'$var'"; print $0}'
one two test
$ echo "one two three" | nawk -v var=$var '{$3=var;print $0}'
one two test

---------- Post updated at 09:17 AM ---------- Previous update was at 09:15 AM ----------

with space

$ echo $var
my test

$ echo "one two three" | nawk -v var="$var" '{$3=var;print $0}'  
one two my test

# 3  
Old 09-30-2011
As an explanation as to why it didn't work....

awk '{$3="'$var'"; print $0}'

When var contains "my tree" expands to this:

awk '{$3="'my tree'"; print $0}'

From the point of view of the shell, there are two parameters being passed to awk:

tree'"; print $0}'

Awk sees the first parameter as the programme and the second as the file to read. In parsing the programme, it complains because the double quote isn't terminated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Space in PATH variable

Hi All, I did a review of some threads to see if someone had come across this problem. My system is an oracle virtual box solaris 10 installed on a windows 10 system. Right now I am learning scripting and am just playing with the env variables. I am trying to update the PATH to... (9 Replies)
Discussion started by: bdby
9 Replies

2. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

3. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

4. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

5. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

6. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

7. Shell Programming and Scripting

Using Variable With Space In It?

Hey , I'm new to Bash and am having trouble with writing a script. I have a variable and want to use it to create a tar file, but, I am having problems. Can anyone tell me what I'm doing wrong? filestr=`date "+%y-%m-%d %H.%M.%S"`.tar tar -cvf $filestr test.txt So, basically,... (3 Replies)
Discussion started by: beefeater267
3 Replies

8. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

9. Shell Programming and Scripting

how to merge two variable in one variable with space between them?

Dear all I had two separate variable. Now i want to make them merge into one new variable with space between them. Kindly suggest me. var1=dec 15 var2=10 i want var3=dec 15 10 My main aim is as below: op of date command: >date >Sat Dec 15 10:17:35 IST 2007 i want only Dec... (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

10. Shell Programming and Scripting

space in variable

Hi, How to check if a variable is having spaces or empty in shell scripts. Please help esham (4 Replies)
Discussion started by: esham
4 Replies
Login or Register to Ask a Question