Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtitution on one field only

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
Can anyone tell me how I can make a substitution on one field only. My input file consists of rows like:

"69500 - 69524","00561000.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69500 - 69524"
"69525","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69525"
"792265","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69525"
"69531","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69531"
"792696 792697 792698 792699","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69544"
"792334","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69552"
"69553","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69553"
"792707 792708","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69553"
"792709","00561001.TIF","\\NAME OF SERVER\Z\xyz","Paybill048\69553"

I want all spaces in field 1 (fields separated by commas) to be changed into underlines, but all other spaces in the file to remain unchanged.

I'm assuming awk is the best way to do this - I know I could do it by splitting the file into 2, 1 for field 1, the other for the rest, sed-ding file 1 & paste-ing them back together but there must be more efficient way.

Hope someone can help
Chris
 
How about this?
awk -F"," ' {
for (x=1 ; x <= NF ; x++) {
if (x == 1) {
gsub(/[ ]/,&quot;_&quot;,$0)
}
}
print
}' target > tmp && mv tmp target
 
Hi marsd,

Thanks for the fast reply. Unfortunately I'm getting:

awk: syntax error near line 4
awk: illegal statement near line 4

After I cut & paste your script I had a space between the open quote & open brace on line 1 (which I've removed); also 2 spaces between the square brackets (which I've changed to 1). But I still got the same error before & after these changes.

I'll keep looking at what could be wrong but would appreciate being pointed in the right direction.

Chris
 
No problem - if I use nawk your code works fine.

Many thanks

 
Yes, but it should be $1 instead of $0. It's just as easy to fix it.(I hope)
if (x != 1) {
gsub(/_/,&quot; &quot;,$0)
}

 
marsd

Thanks for your time - nearly there but now I look more closely I see that the field separators are disappearing for records where the substitution has occurred ... (ie, for any record where space has been replaced by an underline, the comma field separator has now been replaced by a space throughout the record!)

Any idea?
 
BEGIN {
FS=OFS=&quot;,&quot;
}

{
gsub(&quot;[ ]&quot;, &quot;_&quot;, $1);
print;
} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
(n)?awk is a FUNDERFULL tool if it has to something intelligent
like: compound, summ, diff ^^ and so on.
if you just will replace, allways try the good old sed
in your exemple:
&quot;aaa - bbb&quot;,qqqq,zzz ....
you could try the old FASTER sed:

sed -e '/&quot;/,/&quot;/s/ /_/g' filename
 
Thanks again for all the contributions.

vlad's code worked fine.

I'd considered sed but didn't know how to only substitute in the first field, unfortunately jamisar's code also changes other occurrences of <space> (because all fields are surrounded by quotes I guess).

Would still be interested to see a sed solution though ...

Thanks again, Chris
 

here are two solutions so far [no line wraps] - no very elegant, but. Where are our sed experts when you need one?:

# assuming ',' separated columns
sed -e 'h;s/^\([^,][^,]*,\)\(.*\)/\1/;s/[ ]/_/g;x;s/^\([^,][^,]*,\)\(.*\)/\2/;x;G;s/\n//'

# assuming quoted fields
sed 'h; y/ /_/; G; s/\(&quot;[^&quot;]*&quot;\).*\n&quot;[^&quot;]*&quot;/\1/' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
a bit better/cleaner solution:

sed 'h; y/ /_/; G; s/^\([^,]*\),.*\n[^,]*,/\1,/' vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Amazimg what you learn on Tek-Tips isn't it? vlad's sed code works - I'll now spend the rest of the day trying to figure out why but will soon give up & go home ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top