Add CR to a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add CR to a String
# 1  
Old 08-19-2005
Add CR to a String

How can I add a carriage return to a string for every 35 characters within the string?

Basically reversing a tr where I removed the CR and LF's.

Thanks for the help!
# 2  
Old 08-19-2005
Try this:

perl -e 'while(<>){s/(.{35})/$1\n/g; print;}' myfile > result


Explanation (in case you are not familiar with Perl):

while(<>) starts a loop on every line of standard input or files, listed in arguments;

s/(.{35})/$1\n/g means "substitute 'any character' 35 times with the same string and end of line; do this globally for the whole string";

print is obvious.
# 3  
Old 08-20-2005
Code:
ruby -nle 'puts gsub(/.{35}/, "\\&\n")'

# 4  
Old 08-23-2005
worked great, thank you
# 5  
Old 08-23-2005
fold -w 35 file > newFile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add NA When String is not found

I am searching for these patterns in my file between commas, when I dont find a pattern match I would Like to add NA. awk '/Device ID:/ || /address:/ || /Interface:/ || /VLAN:/ || /Platform:/ || /Software/ {print $0,"NA"}' $tempdir/shcdpnedet > $tempdir/newtt (9 Replies)
Discussion started by: dis0wned
9 Replies

2. Answers to Frequently Asked Questions

Add string to parentheses

Suppose I have this code : int main () { int i = NULL; /* incorrect */ return 0; } and I want to put the word between the two parentheses like this : int main (void) { int i = NULL; /* incorrect */ return 0; } which command is used to do it in Linux ? (2 Replies)
Discussion started by: steve120
2 Replies

3. Shell Programming and Scripting

Add an string at every x chars

Hi All, I have a file fo around 15k bytes which i need to insert a string " + "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 #!/usr/bin/perl $teststring =... (9 Replies)
Discussion started by: kadu
9 Replies

4. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. Shell Programming and Scripting

Search a string and to add another string after that in new line

Hi Guys I am facing a problem:wall: In searching a string in a file and to add another string(ie. passed through command line argument) just after this(searched) string in new line. Thanks (2 Replies)
Discussion started by: kushwaha
2 Replies

8. Shell Programming and Scripting

add a particular string to particular files

Hiii I wanted to add a particular string using grep to all the files of a specific size say add a string empty to files of zero size would appreciate if u could use "find" me not comfortable with sed or awk. Thanks (2 Replies)
Discussion started by: gr8pals
2 Replies

9. Shell Programming and Scripting

Add string after another string with special characters

Hello everyone, I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this: 1 2 3 4 5 6 6 7 8 9 And this is the script as far as I've managed: ... (2 Replies)
Discussion started by: heliode
2 Replies

10. Shell Programming and Scripting

Add string after line X

Hi all, I'm trying in a script, to add a string after the line number one. file=file.txt text="it works" sed '1i\ $text' < $file But I'm getting the following err: "Function 1i\ $text cannot be parsed." Does anyone knows the reason why this err is happening or knows another way to... (2 Replies)
Discussion started by: alienET
2 Replies
Login or Register to Ask a Question