Add an string at every x chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add an string at every x chars
# 1  
Old 12-05-2014
Add an string at every x chars

Hi All,

I have a file fo around 15k bytes which i need to insert a string
Code:
" + "

at every 250 bytes.

I found some ideas here using perl to split into lines and tried to addapt it but the results where not satisfactory


for instance i tried to change
Code:
#!/usr/bin/perl
$teststring = "1234567890abcdefghij0987654321ABCDEFGHIJlmnop";
@chunks = split /(.{10})/, $teststring;
foreach (@chunks) {
  printf "%s\n", $_;
}

into
Code:
  printf "%s\"  \+ \"", $_;

but somewho it ended up outputing the string i want to add twice and also it removed some of the code, where I have
Code:
\"

it removed the slash and left as
Code:
"

Any help appreciated.
# 2  
Old 12-05-2014
Hi kadu...

I have a file fo around 15k bytes which i need to insert a string

How are you expecting a file of size, say for example, 15173 bytes is size which is not exactly divisible by 250 to be handled?

With or without a final " + " ?

Do you want the above to be at the very start also?

Is the file going to contain any control characters or special characters or is it a pure alpha-numeric flat file?

An example real world file would help...

EDIT:
A simple longhand version using the shell only on OSX 10.7.5 default bash terminal every 7th character...
Code:
#!/bin/sh
# REMEMBER!!! A newline and/or carriage return are bytes also...
> /tmp/test_string
> /tmp/new_test_string
echo '1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop' > /tmp/test_string
count=0
text=$(cat /tmp/test_string)
echo "$text"
echo ""
while [ $count -lt ${#text} ]
do
	printf "${text:$count:7}" >> /tmp/new_test_string
	count=$((count+7))
	printf '" x "' >> /tmp/new_test_string
done
cat /tmp/new_test_string
echo ""

Results:-
Code:
Last login: Fri Dec  5 23:51:43 on ttys000
AMIGA:barrywalker~> cd Desktop
AMIGA:barrywalker~/Desktop> cd Code
AMIGA:barrywalker~/Desktop/Code> cd Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./addtext.sh
1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop
1234567890abcdefghij0987654321ABCDEFGHIJlmnop

1234567" x "890abcd" x "efghij0" x "9876543" x "21ABCDE" x "FGHIJlm" x "nop
123" x "4567890" x "abcdefg" x "hij0987" x "654321A" x "BCDEFGH" x "IJlmnop" x "
123456" x "7890abc" x "defghij" x "0987654" x "321ABCD" x "EFGHIJl" x "mnop
12" x "3456789" x "0abcdef" x "ghij098" x "7654321" x "ABCDEFG" x "HIJlmno" x "p" x "
AMIGA:barrywalker~/Desktop/Code/Shell> _

Note the newlines are counted too.

Last edited by wisecracker; 12-05-2014 at 08:22 PM.. Reason: See above...
# 3  
Old 12-05-2014
Instead of the split you could try:
Code:
@chunks = ($teststring =~ /.{1,10}/g);

and then
Code:
printf "%s\" \+ \"\n", $_;

But then it will not work correctly for the last odd bit if string which is less than 10 characters. Also you mention bytes, which is not the same since the file may contain multibyte characters.

Also you seem to be introducing newlines in the which you did not mention in your requirement.
# 4  
Old 12-05-2014
Code:
sed 's/.\{250\}/&" + "/g' file

Quote:
Disclaimer: I'm aware of potential side effects.
# 5  
Old 12-05-2014
Quote:
Originally Posted by junior-helper
Code:
sed 's/.\{250\}/&" + "/g' file

Quote:
Disclaimer: I'm aware of potential side effects.
And, are you also aware that it only works if the entire file is a single line?

And, that since the standards only require sed to work on text files that on many systems are limited to lines no longer than 2048 bytes (including the <newline> characters), it might not work with a file that is around 15kb?
# 6  
Old 12-05-2014
  1. It depends on how one is interpreting this requirement. (I could be wrong.) In general, it will "work" on multiple lines too. (I understand what you are pointing to.)
  2. I'm aware of that (Actually, I've learned that from your previous postings, to be exact), but fortunately GNU sed has no such limit ¹
¹ https://www.gnu.org/software/sed/manual/sed.txt (Section: GNU `sed''s Limitations and Non-limitations)
# 7  
Old 12-06-2014
@junior-helper @2. Then you should explicitly mention with your solution that it needs GNU sed..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - print only the chars that match a given set in a string

For a given string that may contain any ASCII chars, i.e. that matches .*, find and print only the chars that are in a given subset. The string could also have numbers, uppercase, special chars such as ~!@#$%^&*(){}\", whatever a user could type in without going esoteric For simplicity take... (1 Reply)
Discussion started by: naderra
1 Replies

2. UNIX for Dummies Questions & Answers

How to search for a string with special chars?

Hi guys, I am trying to find the following string in a file, but I always get pattern not found error, not sure what is missing here. Can you help please? I do a less to open the xrates.log and then do a /'="18"' in the file and tried various combinations to search the below string. String... (8 Replies)
Discussion started by: santokal
8 Replies

3. Shell Programming and Scripting

XML - Split And Extract String between Chars

Hi, I am trying to read the records from file and split into multiple files. SourceFile.txt <?xml version="1.0" encoding="UTF-8"?>... (2 Replies)
Discussion started by: unme
2 Replies

4. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

5. Shell Programming and Scripting

[Solved] print chars of a string

how can i print all the chars of a string one by line? i have thought that use a for cicle and use this command inside: ${VARIABLE:0:last}but how can i make last? because string is random P.S. VARIABLE is the string or can i make a variable for every chars of this string? this was my idea... (10 Replies)
Discussion started by: tafazzi87
10 Replies

6. UNIX for Dummies Questions & Answers

regexp: match string that contains list of chars

Hi, I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out. If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep: echo $STRING|grep a|grep b|grep c but, how would you do that in a single... (9 Replies)
Discussion started by: jimcanoa
9 Replies

7. Shell Programming and Scripting

Repeatable chars in a string

I have a string I keep appending too upto certain amount of chars. Is there some sort of way for me to check the string to see if I hit my limit of repeatable characters? For example, assume I allow for 2 repeatable chars, this will be a valid string Xxh03dhJUX, so I can append the last... (3 Replies)
Discussion started by: BeefStu
3 Replies

8. Shell Programming and Scripting

Matching 2 chars of a string that repeat

Hello Unix gurus, I have a gzipped file where each line contains 2 street addresses in the US. What I want to do is get a count for each state that does not match. What I have so far is: $ gzcat matched_10_09.txt.gz |cut -c 106-107,184-185 | head -5 CTCT CTNY CTCT CTFL CTMA This cuts... (5 Replies)
Discussion started by: sitney
5 Replies

9. Shell Programming and Scripting

Retreive string between two chars

I want to write a shell script in order to retreive some data from a log file that i have written into. The string that i want to get is the number 2849 (that is located between | | ). To explain further, this is the result i get after running "grep LOGIN filename.log" but i need to get the... (25 Replies)
Discussion started by: danland
25 Replies

10. UNIX for Dummies Questions & Answers

Extracting the last 3 chars from a string using sed

Hi. Can I extract the last 3 characters from a given string using sed ? Why the following doesn't work (it prints the full string) : echo "abcd" | sed '/\.\.\.$/p' doesn't work ? output: abcd Thanks in advance, 435 Gavea. (7 Replies)
Discussion started by: 435 Gavea
7 Replies
Login or Register to Ask a Question