The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Turn For Loop into While Loop cbo0485 Shell Programming and Scripting 7 08-04-2009 01:58 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM
Is there a better way I could have run this loop. (For loop with two variables) DeCoTwc Shell Programming and Scripting 2 03-27-2009 02:07 AM
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 12:09 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-25-2009
Jahn Jahn is offline
Registered User
  
 

Join Date: May 2009
Posts: 9
Using AWK in a for loop

Hello,

I am trying to use AWK to print only the first field of numerous text files, and then overwrite these files. They are of the format 1*2,3,4,5. I have tried the following code (using tcsh):

Code:
 foreach f (file1 file2 file3)
cat $f | awk -F'*' '{print $1}' > $f
end
However, I get very inconsistent results each time I run this script. I have tried creating a tmp directory where I run the above command on a clean set of the files. But, sometimes it returns empty files, whereas other times it runs fine for all the files. Any ideas on what is going on here, or if there is a better way to get awk to do what I want?

On a side note, I have tried a similar method using cut:

Code:
 foreach f (file1 file2 file3)
cat $f | cut -d'*' -f1 > $f
end
but I get the same inconsistencies.


Thank you for your help,

-Jahn
  #2 (permalink)  
Old 08-25-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
The below line should be re-written..
Code:
cat $f | awk -F'*' '{print $1}' > $f
Try:
Code:
awk -F'*' '{print $1}' $f > _temp
mv ${_temp} $f
  #3 (permalink)  
Old 08-25-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
I'd personally get rid of the brackets around the foreach, but i dont know tcsh so it may be a requirement. It should work doing
Code:
 for file in file1 file2 file3 ; do awk -F'*' '{print $1}' < $file > $file ; done
???
Quote:
Originally Posted by Jahn View Post
Hello,

I am trying to use AWK to print only the first field of numerous text files, and then overwrite these files. They are of the format 1*2,3,4,5. I have tried the following code (using tcsh):

Code:
 foreach f (file1 file2 file3)
cat $f | awk -F'*' '{print $1}' > $f
end
However, I get very inconsistent results each time I run this script. I have tried creating a tmp directory where I run the above command on a clean set of the files. But, sometimes it returns empty files, whereas other times it runs fine for all the files. Any ideas on what is going on here, or if there is a better way to get awk to do what I want?

On a side note, I have tried a similar method using cut:

Code:
 foreach f (file1 file2 file3)
cat $f | cut -d'*' -f1 > $f
end
but I get the same inconsistencies.


Thank you for your help,

-Jahn
  #4 (permalink)  
Old 08-25-2009
Jahn Jahn is offline
Registered User
  
 

Join Date: May 2009
Posts: 9
Here is what worked for me:
Code:
 foreach f (*)
awk -F'*' '{print $1}' $f > {$f}_temp
mv {$f}_temp $f
end
I have tried using the "awk code < file > newfile" redirection before, but I always end up with an empty newfile.


Thanks again for your help, guys!
  #5 (permalink)  
Old 09-17-2009
fredrik.aslund fredrik.aslund is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
sed 's/\*.*//' deletes everything from first * on lines
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0