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!

database programming with awk? 1

Status
Not open for further replies.

hackey37

Technical User
Aug 10, 2001
4
US
If I have a simple pipe delimited flat file with 3 fields, can I use awk to update any filed any where in the file?
 
sure - the magic command is "man awk"

But... give us a sample of input and what needs to accomplished.

vlad
 
Here is what I am trying to do...

Sample data file
080914|xx xxx
080915|xxx x
080916|xx xx
080917|xxxxxx
080918|xxxxxx
080919|x x

This is so simple I am sure but I have reached a bit of a wall. I want to update $2 (FS="|") at will. Here is the catch though. If the last record $2=="x x " and I want it to be updated to "x x x", how would I do that? IN other words I want it to go from x-space-space-x-space to x-space-space-x-space-x or maybe even x-space-space-x-space-space. I hope my description is good enough but if you require more info, let me know. In this file, $2 will never be longer than 6 bytes but if it is the last record, it could vary from 0-6 bytes and I just want to update $2 with an additional x or space(s)!
 
I don't understand exactly what the criteria are for modifying field 2, but to do just the change you gave as an example, the following awk program should do it. Also, you said the file has 3 fields but your example only has 2.

BEGIN { FS="|"}
{
if ($2 == "x x ") {
$2 = "x x x"
}
print
}
 
I just noticed that my solution loses the | character on output. This might be better.

BEGIN { FS="|"}
{
if ($2 == "x x ") {
print $1 FS "x x x"
}
else
print
}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top