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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Amend values of a field in a record

Status
Not open for further replies.

akelabanda

Programmer
Dec 19, 2001
61
IN
Hi

I would like to read a file, one line at a time, change a specific column value and write the output back to a file.

How can I do this ?

Example
Input
1,Rajeev,33,Reading,UK
Want to change 33 to 34 and write back as
1,Rajeev,34,Reading,UK

The actual file has some 45 columns. Can this be done using awk or do you recommend PERL ?

Thanks
Rajeev
 
#!/bin/ksh

awk ' BEGIN { IFS=","; OFS="," } { $3=34; print } ' datafile > newdata.file

#or

awk ' BEGIN { IFS=","; OFS="," } { $3=$3+1; print } ' datafile > newdata.file

 
Sorry, I'm mixing my syntax:

Code:
#!/bin/ksh

awk ' BEGIN { FS=","; OFS="," }  { $3=34; print } ' myfile.txt

#or

awk ' BEGIN { FS=","; OFS="," }  { $3=$3+1; print } ' myfile.txt
 
Thanks olded. That's really helpful.

Just one more question.

Is there a way I can combine sed with the awk to modify the field with something like this. I would like to remove all special characters in that field and retain only spaces and alphanumeric values.
s/! [0-9][a-z][A-Z]//

UK's best mobile deals online
 
olded,
Code:
BEGIN { FS=OFS="," }

akelabanda,
look into the awk's "gsub" function.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Guys

I need to replace the special characters in field containing

D0 PingChecks^M

and retain only the alpha numeric and spaces.

I haven't had any luck with the usage of gsub function. Can you help ?

,234103120076416,0,0b1208001204448720001016000000000000000000,0,,,,,2,,,,46,,80 ,255,95017,D0 PingChecks^M,255,23 88850200001854,000000000000000,0,000000000000000000000000

UK's best mobile deals online
 
This test worked for me:

[tt]awk '{gsub("\r","");print}' inputfile_containing_ctrl-ms[/tt]

Try that perhaps?

Annihilannic.
 
gsub("\r","",$fieldNumber)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Running the following

awk 'BEGIN { FS=","; OFS="," } { gsub("\r","",$39); print } ' t1.csv

shows

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

Cant figure out why. I'm new to awk.

UK's best mobile deals online
 
What OS? It's possible that your awk doesn't support gsub(), in which case, look for a nawk perhaps. Or on Solaris you can also try /usr/xpg4/bin/awk.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top