ksh string replacement over a carriage return


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh string replacement over a carriage return
# 1  
Old 11-12-2013
ksh string replacement over a carriage return

Hi.

I need to make multiple string replacements in a file but several of the strings have been broken up by a carriage return so that the first few characters are on one line and the remainder on the following line.

E.g like the word 'post' in the following:

Use descriptive thread titles when posting. For example, do not po
st questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Po
st subjects like "Execution Problems with Cron"


I'm using a korn shell script with embedded perl. Should run on both linux and solaris. I tried the following:

#rm all carriage returns
Code:
perl  -0777 -nle 's/\n\s*( )/==$1/g; s/\n//g; s/==()/\n$1/g;  print "$_\n"' input.file > tmpfile

#replace the string
Code:
sed "s|string1|string2|g" tmpfile > input.file

#add carriage returns every 80 characters
Code:
perl -pi  -e's/^(.{79,79})(.*)/$1\n $2/' input.file

Seems to replace the string ok. However the format of the new output file is a bit messed up still when i try to put it back together again. Blank line are not handled for example. Not every line is cut to the correct length.

Any help greatly appreciated.
J

Last edited by Franklin52; 11-12-2013 at 09:35 AM.. Reason: Please use code tags
# 2  
Old 11-12-2013
Code:
cat file | tr -d '\n' | sed 's/.\{79\}/&\n/g'


Last edited by balajesuri; 11-12-2013 at 07:15 AM.. Reason: P.S.: I know it's useless use of cat :-P
# 3  
Old 11-12-2013
Blank lines are a sequence of two (or more) line feed chars. If you remove all of those, that info is lost.
Did you consider awk with its multi-line record feature?
# 4  
Old 11-13-2013
thanks for the feedback guys.

I think perhaps my first step is not as it should be. If I use the following perl to merge a line with the one before it I get better formatting. It keeps the blank lines. However it does not keep lines that are less than 80 chars as they were. If I could get it to leave lines that are less than 80 chars alone it would mean only the code that needs to be changed is affected. So the last step of putting it back together would be simplified.
Code:
perl -ne '$a.=$_;END{$_=$a;s/  */ /g;s/[^\n]\n[^\n]/ /g;s/\n\n\n*/\n\n/g;print}' a.in > a.out

what do you think?

---------- Post updated 13-11-13 at 11:39 AM ---------- Previous update was 12-11-13 at 03:37 PM ----------

I thought I had the solution with the following but again the final file output contains lines longer than 79 characters. I'm operating on an ldif file so max line length and a space on the following line if it wraps around are essential.

# if a line begins with a space append it to previous line
Code:
sed -e :a -e '$!N;s/\n //;ta' -e 'P;D' ${ldif_file} > ${ldif_file}_tmp

# replace variable
Code:
sed "s|$string1|$tring3|g" ${ldif_file}_tmp  > ${ldif_file}_tmp1 && mv ${ldif_file}_tmp1 ${ldif_file}_tmp

# indent wrapped lines
Code:
perl -pi.bak  -e's/^(.{79,79})(.*)/$1\n $2/' ${ldif_file}_tmp

# backup input file and split lines over 80 chars long
Code:
fmt -s -w 80  ${ldif_file}_tmp > ${output_file}


Alas the ouput file format is still not correct. Any help would be greatly appreciated. Thanks again.

Last edited by Franklin52; 11-13-2013 at 08:34 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove carriage return

I need to remove the carriage return comes inbetween the record. Need to have CR only at the end. I used the below command. tr -d '\n' < filewithcarriagereturns > filewithoutcarriagereturns But its removing all the CR and giving one line output. Input File: 12345 abcdegh... (11 Replies)
Discussion started by: srvn_saru
11 Replies

2. Shell Programming and Scripting

Substitute \n with carriage return

Hello all, I've a flat file in the following format: AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003 And I want to substitute \n with the carriage return. Any help is appreciated! Regards, - Seth (8 Replies)
Discussion started by: sethmj
8 Replies

3. Shell Programming and Scripting

Carriage return ksh

Hello, How do i usecarriage return in ksh. I want to do an echo "bla bla" and another echo "bla bla" will appear and replace the first echo on screen. I tried: until ; do echo "bla bla \r" done please advice. Thanks. (3 Replies)
Discussion started by: LiorAmitai
3 Replies

4. Shell Programming and Scripting

Removing Carriage return in a file after particular string

Hi All, I want to remove carriage return in a file using some unix command without writing a script my file is as follows abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 I want the output as follows: abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 , Please... (7 Replies)
Discussion started by: manish8484
7 Replies

5. Shell Programming and Scripting

Search_Replace with a Carriage Return

Hey folks, I've been working on this for some time. Seems simple, but I'm stumped. I need the following data format: New_York:Commercial Geology Geophysics Petrophysics Production_Engineering Reservoir_Engineering Pasadena:Commercial ... (5 Replies)
Discussion started by: leepet01
5 Replies

6. UNIX for Dummies Questions & Answers

Remove ^M (carriage return) with string manipulation

Hello, I want remove ^M at end of my files line if I use command : tr -d '\r' <inp>out it work fine but get I the same result by manipulating the string ? I want this because in my text file I manipulate some other part I have input "the cat^M" I want output "the cat" I have made... (3 Replies)
Discussion started by: aquila_1
3 Replies

7. Shell Programming and Scripting

KSH Functions String return

Hy everybody I'm trying to write a function like function(){ final=$1 return $final } but I get following error: "return: bad non-numeric arg ''" what shall I do? Isn't it possible to return strings?:confused: thanks lucae (2 Replies)
Discussion started by: lucae
2 Replies

8. Shell Programming and Scripting

deleting 'carriage return' from string

hi there I'm getting a string from a sqlplus query, and I need to compare it with another string Problem here, is that the string i get from query brings a 'carriage return' with it, and comparing throws always false value. i need to delete all carriage retun charactres in string. how... (6 Replies)
Discussion started by: viko
6 Replies

9. Shell Programming and Scripting

Dont want carriage return

I have observed with print & echo, they produce carriage return <CR> or newline, after they display string next to them. Is there anyway to avoide these <CR> after the intended string is displayed? (3 Replies)
Discussion started by: videsh77
3 Replies

10. Shell Programming and Scripting

Capture carriage return.

I try to test the carriage return in a variable. $ LENGTH=`expr $VARIABLE : ".*"` will return the length of the variable. But this doesn't work if $VARIABLE has zero length. Any help will be well appreciated. Thanks in advance. Giovanni (4 Replies)
Discussion started by: gio123bg
4 Replies
Login or Register to Ask a Question