cat command space problem with awk in for loop.
Dear all,
I am trying to echo some value using awk, and for reading the file i am using cat command. If there no space in the file content then the following script result correctly. ------------------------------------------------ console: # cat 1 one;two;three; four;five;six; console: # cat 2 for J in `cat 1` do A=`echo $J | awk -F ';' '{ print $1 }'` echo $A done console: # ./2 one four console: # -------------------------------------------------- In case any space is there in the file 1 then i am facing the problem in the output value . ------------------------------------------------- console: # cat 1 o ne;two;three; four;five;six; console: # cat 2 for J in `cat 1` do A=`echo $J | awk -F ';' '{ print $1 }'` echo $A done console: # ./2 o ne four console: # -------------------------------------------------- Can some one tell me how to omit that space. Thanks. |
The problem is in the statement:
Code:
for J in `cat 1` 1) o 2) ne;two;three; 3) four;five;six; Anyway, you don't really need such a loop, since awk can parse the content of a file line after line. You can simply do Code:
awk -F";" '{print $1}' 1 Code:
awk -F";" '{print gensub(/ /,"","g",$1)}' 1 |
Regarding the way the shell splits lines, you have to consider the IFS environment variable. From the bash man page:
Code:
IFS The Internal Field Separator that is used for word splitting after expansion and to Code:
#!/bin/bash |
why don't you try the tr command?
file_1=`cat 1 | tr -d ' '` # this removes spaces from your file then use file_1 as your new temporary file like this: cat $file_1 instead of: cat 1 |
All times are GMT -5. The time now is 04:57 AM. |