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

replace space in column with another character sed/awk 2

Status
Not open for further replies.

bosoxer1

Programmer
Jan 22, 2005
20
0
0
US
Hi,

I have a batch file with a header and trailer record, with dataline records in the middle. I need to replace only the rows with a space in column 10 with the number 3, and not alter other rows, or the header and trailer.

99999123 ACCOUNT DATA Header
123456789012345678901234567890
123456789 12345678901234567890
123456789012345678901234567890
123456789 12345678901234567890
99999300 Account Data Trailer

The output should look like this

99999123 ACCOUNT DATA Header
123456789012345678901234567890
123456789312345678901234567890
123456789012345678901234567890
123456789312345678901234567890
99999300 Account Data Trailer


Can anyone suggest me how to achieve this?

Thanks
Dave
 
Oh yea...The column I really need to replace is in column 307. I used column 10 just as an example. That may have an impact on the answer, incase you were thinking of using "." as a wild card.
 
nawk -v pos=10 -v last=`wc -l < bosox.txt` -f bosox.awk bosox.txt

here's bosox.awk:

Code:
FNR!=1 && FNR !=last && substr($0, pos, 1) == " " { $0=substr($0,1,pos-1) "3" substr($0, pos+1); }
1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
awk -f plugin3.awk col=307 infile >outfile
Code:
1==NR { print; next }
NR>2 { print fix(line) }
{line = $0}
END { print line }
function fix( s )
{ if (" "==substr(s,col,1) )
    s = substr(s,1,col-1) "3" substr(s,col+1)
  return s
}
 
Thanks Vlad...

I guess I'm having a brain freeze. I created the bosox.awk file, and tried to run the script. I'm getting a cannot find or open file -f. Here is the line i'm trying to run.

nawk -v pos=10 -v last=`wc -l < bosox.txt` -f bosox.awk bosox.txt

I used chmod to grant x for everyone, but still the same message. I'm running the line in the same directory as the files. I created the .awk file using vi. Any ideas?
 
sorry 'bout that - here's another try:

nawk -v pos=10 -v last="`wc -l < bosox.txt`" -f bosox.awk bosox.txt

Code:
BEGIN { last=int(last) }
FNR!=1 && FNR !=last && substr($0, pos, 1) == " " { $0=substr($0,1,pos-1) "3" substr($0, pos+1); }
1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
futurelet, very nice [as always]!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Great stuff so far, but using vlads code, the last row is getting updated with the 3. It's not recognizing the last row code. Also the output is displayed to the screen, but the file is not getting updated.

Thanks again for your help...
 
make sure the LAST line/header is REALLY the last line - and there are not empty trailing lines.

It works fine iwth my test setup with the sample file you're provided.

AWK does not update the file, but rather outputs to the screeen you've to update the file manullay [mv].

Try futurelet's code - it's much more graceful.


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
awk -f plugin3.awk -v col=307 infile >outfile

File plugin3.awk contains:
Code:
# Read file twice to get number of last line.
BEGIN { ARGV[ARGC++]=ARGV[1]
  if (!col) {print "Use -v col=n";exit} }
NR==FNR { if (NF) last = NR; next }
1==FNR || last==FNR { print; next }
{ print fix($0) }
function fix( s )
{ if (" "==substr(s,col,1) )
    s = substr(s,1,col-1) "3" substr(s,col+1)
  return s
}
This should work even if the file contains trailing blank lines.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top