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!

Awk Help needed

Status
Not open for further replies.

chomps303

MIS
Sep 30, 2003
83
0
0
US
I have a large file 180,000 records in it. I need to take the first field whatever it is and change it to a -1 AND KEEP the record length to 304 bytes.

The first field could contain several different lengths ie:

10
5
-24
100
-250

Any ideas?


Thanks in advance

Brandt
PMP

 
Have you tried something like this ?
awk '{$1="-1";printf "%-304.304s\n",$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If the first field is always at least 2 characters long, you may want to preserve the width of the first field.
Code:
{ match( $0, /^[ \t]*[^ \t]+[ \t]*/ )
  $0 = substr( $0, RLENGTH  + 1 )
  printf "%-" RLENGTH "d%s\n", -1, $0
}
If the input is:
[tt]
10 foo is
5 bar was
-24 baz will be
100 bimble has been
-250 bumble had been
[/tt]
The output is:
[tt]
-1 foo is
-1 bar was
-1 baz will be
-1 bimble has been
-1 bumble had been
[/tt]
 
PHV you are close here is the output. I need the rest of the line!!

Thanks

Brandt


-1 ^M



-1 ^M



-1 ^M



-1 ^M



-1 ^M
 
Futurelet

What do I do with you code syntax wise?

Thanks

Brandt
 
Well, some ooooold awk I guess.
What about this ?
awk '{x="-1";for(i=2;i<=NF;++i)x=x" "$i;printf "%-304.304s\n",x}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

PHV

SORRY!! Same result.

Thanks

Brandt

-1 ^M



-1 ^M



-1 ^M



-1 ^M



-1 ^M



-1 ^M
 
You try to play with binary file ?
Can you please post some input samples and expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Save the code in file named, say, one.awk.
[tt]
awk -f one.awk infile >outfile
[/tt]
 
OK,

This browser will not allow "-" as the first chr (Don't know why) My example below I am using a "=" instead of "-"

The 1st field could be

1 chr 6
2 chr 62
3 chr 620

I need to replace the 1st field with a "-1" (no quotes) The main issue is I MUST keep the record length to 304 char.


=62,557746,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,""
,"","","","150471",0,1,467,1,"",0,0,"N","","","","1B",0


=2,557747,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,""
,"","","","150472",0,1,3058,1,"",0,0,"Y","","","","1B",0

Thanks

Brandt
 
awk [highlight]-F','[/highlight] '{$1="-1";printf "%-304.304s\n",$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, sorry ...
awk 'BEGIN{FS=OFS=","}{$1="-1";printf "%-304.304s\n",$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
BEGIN { FS=OFS="," }
{ $1 = -1; printf "%-304s\n", $0 }

If the width of the 1st field is always at least 2:

Code:
BEGIN { FS="," }
{ match( $0, /^[^,]+/ )
  $0 = substr( $0, RLENGTH  + 1 )
  printf "%-" RLENGTH "d%s\n", -1, $0
}
Save the code in file named, say, one.awk.

awk -f one.awk infile >outfile
 
The second program can be shortened to
Code:
{ match( $0, /^[^,]+/ )
  $0 = substr( $0, RLENGTH  + 1 )
  printf "%-" RLENGTH "d%s\n", -1, $0
}
 


PHV

REAL Close!!

It seems that it is not keeping the 304 length.

thanks

Brandt
 
It seems that it is not keeping the 304 length.
Really ? How do count record length ?
To get YOUR expected result, just tweak the -304.304 stuff.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV

See the middle record.

This is the result of you script.

Thanks

Brandt


1,557802,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,"",
"","","","150494",0,1,4015,1,"",0,0,"Y","","","","1U",0

^M

=1,557803,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,"",
"","","","150495",0,1,5137,1,"",0,0,"N","","","","1B",0

^M
=1,557804,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,"",
"","","","150496",0,1,5878,1,"",0,0,"N","","","","1B",0

^M
=1,557805,2,0,"","",20041014,20041014,"","",0,0,0,20041014,20041014,0,0,"",0,"",
"","","","150497",0,1,5286,1,"",0,0,"N","","","","1B",0
 
Brandt, are there any tab characters in the file?
Try this:
[tt]
BEGIN { FS=OFS="," }
{ gsub(/\t/," "); $1 = -1; printf "%-304s\n", $0 }
[/tt]
 
THANKS to all your help

It worked great!! Futurelet

Thanks to ALL

Brandt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top