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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem to send rtf inline body srik_ux UNIX for Dummies Questions & Answers 7 05-30-2008 08:30 AM
Array Printing Inline vakharia Mahesh Shell Programming and Scripting 2 05-21-2008 09:53 AM
Inline Parameters-Urgent indira Shell Programming and Scripting 2 05-02-2007 01:02 PM
variables don't work for inline "su" script joekreif UNIX for Advanced & Expert Users 3 04-18-2007 01:30 PM
Will exe file size decrease while using inline function vijaysabari High Level Programming 1 08-31-2005 07:45 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-2007
Registered User
 

Join Date: Feb 2006
Posts: 65
Lightbulb sed / awk - inplace or inline edit

I need to remove the '&' from a file.

In each line of the file, the fields are separated by ^K.
I only want to remove '&' if it exists in field number 9. (example of field 9: abc&xyz)
I need to do an in place/in line edit.

So far I have accomplished the following:

awk -F '^K' '{print $9}' $file | grep "&" | sed 's/&//g' > ./log


In the above line - awk gets the field 9
- filtering using grep and only looking for field 9 with '&'
- sed removes the '&' from the field and sends output to log file (input file is untouched).


But, I want the input file itself to be edited. I dont need a log file.

Any other options or suggestions will be helpful.
Thank you

Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-21-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,390
Code:
{ rm file && awk '{sub(/&/,"",$9)}1' FS="\\\^K" OFS="^K" > file; } < file
Use gsub for more than one occurrence.
Reply With Quote
  #3 (permalink)  
Old 02-21-2007
Registered User
 

Join Date: Feb 2006
Posts: 65
Thumbs up

Two Thumbs Up.. - Worked like a charm

Thanks alot.

Would you be kind enough to explain what exactly happens when the command executes? I only understand couple of pieces of it.

Reply With Quote
  #4 (permalink)  
Old 02-21-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,390
The content of the input file becomes stdin for rm and awk,
rm ignores it and removes the file, but its file descriptor remains open until both commands (rm and awk) complete, so awk process this "nameless" file and creates a new file.
Reply With Quote
  #5 (permalink)  
Old 02-21-2007
Registered User
 

Join Date: Feb 2006
Posts: 65
Smile Couple of questions:

Thank you.

{ rm file && awk '{sub(/&/,"",$9)}1' FS="\\\^K" OFS="^K" > file; } < file

Questions:

Does the '1' in the awk mean the first occurence of '&'?

For the variable FS why do to have '\\\^K' and different for OFS "^K" ?

Thanks again.
Reply With Quote
  #6 (permalink)  
Old 02-21-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,390
sub removes (replaces with "") the first occurrence of & in the 9th field (use gsub for global substitution or GNU Awk's gensub for more articulated substitutions).
1 is just a shortcut, it means print the current record;
it can be explained like this:

1!=0 -> true -> use default action -> print $0

FS and OFS are treated differently, so in the first case we have to escape the ^ character.

Last edited by radoulov; 02-21-2007 at 10:38 PM.
Reply With Quote
  #7 (permalink)  
Old 02-22-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,158
If ^K is the character VT 'vertical tab'\v (ascii 11(10)), modify the command :
Code:
{ rm file && awk '{sub(/&/,"",$9)}1' FS="^K" OFS="^K" > file; } < file
You get ^K by entering Ctrl/V+CTRL/K


Jean-Pierre.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0