sed / awk - inplace or inline edit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed / awk - inplace or inline edit
# 1  
Old 02-21-2007
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

Smilie
# 2  
Old 02-21-2007
Code:
{ rm file && awk '{sub(/&/,"",$9)}1' FS="\\\^K" OFS="^K" > file; } < file

Use gsub for more than one occurrence.
# 3  
Old 02-21-2007
MySQL

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.

Smilie
# 4  
Old 02-21-2007
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.
# 5  
Old 02-21-2007
Bug 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.
# 6  
Old 02-21-2007
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-22-2007 at 02:38 AM..
# 7  
Old 02-22-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed, Inline replacement of string with spaces

Hello, Just surfed on the web for probable answers but could not get them working. I wish to replace the string containing spaces by another phrase but below answers did not work. My string is: PAIN & GAIN I wish to convert it to: P&G I just need it working with sed with function -i ... (6 Replies)
Discussion started by: baris35
6 Replies

2. UNIX for Dummies Questions & Answers

sed replacement inplace

I need to make permanent changes in the file after find and replace using sed. for this i am using sed -i However this is not working. says sed: illegal option -- i I am working on Sun Solaris uname -a SunOS aspsun14 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise any other work... (3 Replies)
Discussion started by: gotamp
3 Replies

3. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

4. Shell Programming and Scripting

Inline edit using sed / awk

Hi, I have file with all the lines as following format <namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding" name="ENV_CONFIG_PATH" nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH" stringToBind="test"/> I want to replace (all the lines) value of... (8 Replies)
Discussion started by: shuklaa02
8 Replies

5. Shell Programming and Scripting

edit field using sed or awk

please help me to edit the second field using awk or sed i have input file below aa1001 000001 bb1002 000002 cc1003 000003 so i want the output file like below aa1001 01 bb1002 02 cc1003 03 (38 Replies)
Discussion started by: zulabc
38 Replies

6. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

7. UNIX for Advanced & Expert Users

awk - remove block of text, multiple actions for 'if', inline edit

I'm having a couple of issues. I'm trying to edit a nagios config and remove a host definition if a certain "host_name" is found. My thought is I would find host definition block containing the host_name I'm looking for and output the line numbers for the first and last lines. Using set, I will... (9 Replies)
Discussion started by: mglenney
9 Replies

8. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

9. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

10. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies
Login or Register to Ask a Question