LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2009, 09:35 AM   #1
shan_nathan
Member
 
Registered: Jun 2007
Location: India
Distribution: Redhat
Posts: 137

Rep: Reputation: 15
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.
 
Old 06-17-2009, 09:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The problem is in the statement:
Code:
for J in `cat 1`
because the shell uses space as field separator, so that the loop is executed three times on the following strings:
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
If you want to remove the blank space, just do something like this:
Code:
awk -F";" '{print gensub(/ /,"","g",$1)}' 1
The gensub statement just replace any space in the first field with the null string.
 
Old 06-17-2009, 09:58 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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
       split lines into words with the read builtin command. The default value is
       "<space><tab><newline>".
You can temporarily change the value of IFS in a script to avoid behaviors like that shown above. For example:
Code:
#!/bin/bash
OLD_IFS=$IFS
IFS=$(echo)
for J in `cat 1`
do
  A=`echo $J | awk -F ';' '{ print $1 }'`
  echo $A
done
IFS=$OLD_IFS
 
Old 06-18-2009, 06:51 AM   #4
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unix cat command problem UnixKiwi Programming 11 11-25-2008 07:52 PM
cat|grep|awk pudhiyavan Programming 6 07-14-2008 02:56 AM
AWK: problem getline inside while loop jjhall Programming 2 02-27-2008 04:19 PM
for loop cat problem jong357 Slackware 4 04-09-2007 01:05 PM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration