Results 1 to 5 of 5
Hello all !!!
I have a problem with awk ignoring blanks. Consider the following example:
Code:
export test="aa a:bbb:ccc:ddd"
Now using echo and awk I want to print the first ...
- 12-05-2010 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
[SOLVED] Problem with awk: Ignores white spaces
Hello all !!!
I have a problem with awk ignoring blanks. Consider the following example:
Now using echo and awk I want to print the first two elements by parsing on ":"Code:export test="aa a:bbb:ccc:ddd"
The output is as follows:Code:echo $test | awk -F: {'print $1$2'}
As you notice a lot of white spaces has not been restituted !!!!Code:aa abbb
I absolutly need those white spaces to be printed. Does anyone have a solution to this ?
- 12-05-2010 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
welcome to the forum
try with slightly different syntax in the awk comand
awk -F: '{print $1$2}' <test
this preserves whitespacethe sun is new every day (heraclitus)
- 12-05-2010 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
Tnx tpl for your reply.
The redirection dosn't work neither in bash nor in ksh.
It says:
Moreover, I need to use this in a ksh script.Code:bash: $test: ambiguous redirect
Anyone has another idea ?
- 12-05-2010 #4
Such redirection would only work if "test" were a file, not a variable.
However, that is not the problem.
The problem here actually has to do with echo, not awk. You see, Bash scripting is very stupid. It just literally replace variables. So this line:
becomes:Code:echo $test | awk -F: '{print $1$2}'
echo separates its arguments by any amount of whitespace, so it is literally printing "aa a:bbb:ccc:ddd".Code:echo aa a:bbb:ccc:ddd | awk -F: '{print $1$2}'
To get around this problem (and as a general rule), always surround variables in quotes in Bash:
This works correctly for me.Code:echo "$test" | awk -F: '{print $1$2}'DISTRO=Arch
Registered Linux User #388732
- 12-05-2010 #5Just Joined!
- Join Date
- Dec 2010
- Posts
- 3
Thank you very much Cabhan !!!!
That was the simple trick I was looking for. That solved the problem.


