awk: seeking to bytes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: seeking to bytes
# 1  
Old 02-07-2005
Question awk: seeking to bytes

can I seek to a particular byte in a file and replace it using awk?
if so, how?
# 2  
Old 02-07-2005
substitute the THIRD character in the record/line with 'A"
Code:
nawk '{$0=substr($0,2,1) "A" substr($0,4); print}'

# 3  
Old 02-07-2005
Quote:
Originally Posted by vgersh99
substitute the THIRD character in the record/line with 'A"
Code:
nawk '{$0=substr($0,2,1) "A" substr($0,4); print}'

What if I want to go to byte 24 and replace it, instead of knowing which character?
# 4  
Old 02-07-2005
Using sed, you can navigate to a particular offset in a line and make a substitution....

sed 's/./A/24' myfile

This'll replace the 24th character on each line with 'A'

If you want to change a particular byte offset within a multiline file online once, try something like
Code:
#!/bin/sh

replace="B"
offset=24

awk -vr=$replace -vo=$offset '
   BEGIN {
      s=o-1;
      e=o+1;
      RS="";
   } {
      printf("%s%s%s",substr($0,1,s), r, substr($0,e) );
   }
' testfile

exit 0

But beware, each newline is also a byte in itself.

Cheers
ZB
# 5  
Old 02-07-2005
Quote:
Originally Posted by zazzybob
Using sed, you can navigate to a particular offset in a line and make a substitution....

sed 's/./A/24' myfile

This'll replace the 24th character on each line with 'A'

If you want to change a particular byte offset within a multiline file online once, try something like
Code:
#!/bin/sh

replace="B"
offset=24

awk -vr=$replace -vo=$offset '
   BEGIN {
      s=o-1;
      e=o+1;
      RS="";
   } {
      printf("%s%s%s",substr($0,1,s), r, substr($0,e) );
   }
' testfile

exit 0

But beware, each newline is also a byte in itself.

Cheers
ZB
I believe the above 'awk':
1. will not work for the multi-byte character sets as 'substr' is a character based operation [not a byte oriented one].

2. will not work on LARGE files as most awks [at least Solaris' stock ones] have inherited limits on the lenght of records [RS], number of fields [NF] etc....

It might work in some isolated cases, but it is not a generic solution.
# 6  
Old 02-07-2005
Quote:
Originally Posted by zazzybob
Using sed, you can navigate to a particular offset in a line and make a substitution....

sed 's/./A/24' myfile

This'll replace the 24th character on each line with 'A'

If you want to change a particular byte offset within a multiline file online once, try something like
Code:
#!/bin/sh

replace="B"
offset=24

awk -vr=$replace -vo=$offset '
   BEGIN {
      s=o-1;
      e=o+1;
      RS="";
   } {
      printf("%s%s%s",substr($0,1,s), r, substr($0,e) );
   }
' testfile

exit 0

But beware, each newline is also a byte in itself.

Cheers
ZB

I tried the sed command, but it did not output anything???
# 7  
Old 02-07-2005
Quote:
Originally Posted by vgersh99
I believe the above 'awk':
1. will not work for the multi-byte character sets as 'substr' is a character based operation [not a byte oriented one].

2. will not work on LARGE files as most awks [at least Solaris' stock ones] have inherited limits on the lenght of records [RS], number of fields [NF] etc....

It might work in some isolated cases, but it is not a generic solution.
Do you know the limit on awks for length of records? I am only interested in the first 80 bytes of the line, does that make a difference? Also, can't I equate a byte = 1 character?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fresh Grasshopper Seeking Knowledge on inputing awk scripts and working with them?

Hey people people, I am a new grasshopper willing to learn from the masters. I type a lot when I am nervous! I have pulled tons of info off here in the last week concerning awk. I know nothing about awk, I mean nuthin. I have started work as the guy below the lowest man on the... (8 Replies)
Discussion started by: sub terra
8 Replies

2. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

3. UNIX for Dummies Questions & Answers

AWK error - string cannot be longer than X bytes

Hi Friends, Could you please tell me why i am getting the below eror while working with awk. I am confused :confused: what to do ? awk: 0602-591 String 1,9,20,6,6 cannot be longer than 399 bytes. The source line is 1. The error context is >>> <<< awk: 0602-591... (2 Replies)
Discussion started by: i150371485
2 Replies

4. UNIX for Dummies Questions & Answers

X bytes of 0, Y bytes of random data, Z bytes of 5, T bytes of 1. ??

Hello guys. I really hope someone will help me with this one.. So, I have to write this script who: - creates a file home/student/vmdisk of 10 mb - formats that file to ext3 - mounts that partition to /mnt/partition - creates a file /mnt/partition/data. In this file, there will... (1 Reply)
Discussion started by: razolo13
1 Replies

5. Shell Programming and Scripting

awk: Input line Cannot be longer than 3,000 bytes.

Guys, I want to get the high CPU utilization from top. I am using below code : top -d2 >> /home/dba_monitoring/host_top_output.txt echo "Script started `date`" > $runlog usage=`grep "^ *$1" /home/dba_monitoring/host_top_output.txt | awk '{print $12}' | sed 's/%//'` And getting below... (7 Replies)
Discussion started by: wahab
7 Replies

6. Programming

Copying 1024 bytes data in 3-bytes chunk

Hi, If I want to copy a 1024 byte data stream in to the target location in 3-bytes chunk, I guess I can use the following script. dd bs=1024 count=3 if=/src of=/dest But, I would like to know, how to do it via a C program. I have tried this with memcpy(), that did not help. (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. Shell Programming and Scripting

Error PHP Fatal error: Allowed memory size of 67108864 bytes exhausted(tried to allocate 401 bytes)

While running script I am getting an error like Few lines in data are not being processed. After googling it I came to know that adding such line would give some memory to it ini_set("memory_limit","64M"); my input file size is 1 GB. Is that memory limit is based on RAM we have on... (1 Reply)
Discussion started by: elamurugu
1 Replies

8. Homework & Coursework Questions

Problem with awk,not able print the file that is greater than 3000 bytes.

My Script: #!/bin/sh date=`date +%y%m%d -d"1 day ago"` in_dir=/vis/logfiles/to_solmis cp `grep -il ST~856~ $inbound_dir/*$date*` /vis/sumit/in_ASN/ for i in /vis/sumit/in_ASN/* do mkdir -p /vis/sumit/inboundasns.$date cp `echo $i`... (1 Reply)
Discussion started by: shrima.pratima
1 Replies

9. Shell Programming and Scripting

AWK input can not be longer than 3000 bytes

Hi, i have following line in my code. eport.pl < $4 | dos2ux | head -2000 | paste -sd\| - | awk -v S="$1" ' Issue is, i get a message saying "awk:input line | found /file/path cannot be longer than 3000 bytes." "source line number is 3" Can someone help me with this please? (4 Replies)
Discussion started by: usustarr
4 Replies

10. Shell Programming and Scripting

Remove first N bytes and last N bytes from a binary file on AIX.

Hi all, Does anybody know or guide me on how to remove the first N bytes and the last N bytes from a binary file? Is there any AWK or SED or any command that I can use to achieve this? Your help is greatly appreciated!! Best Regards, Naveen. (1 Reply)
Discussion started by: naveendronavall
1 Replies
Login or Register to Ask a Question