Removing Carriage return and merging data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing Carriage return and merging data
# 1  
Old 03-09-2010
Removing Carriage return and merging data

Hi, I am trying to remove the carriage return on the record which starts with ADD, MODIFY, or DELETE keyword as the first value in the record. If the records does not start with anyone of these keywords then combine the records with the previous record (line).

Input

File name xyz.txt
Code:
ADD|123|123|444|555|  

ADD|N000|Smith, John  
BNSA-D'Fico,T#13|3|2010|
 
MODIFY|N000|D'Smith, John  
D'AMICO  
BNSA-D'FILO,T#1|3|2010|
 
DELETE|N000|SNELL, KENNETH L 
NATI-SNELL,K#13|3|2010|

Output
Code:
ADD|123|123|444|555|
ADD|N000|Smith, John BNSA-D'Fico,T#13|3|2010|
MODIFY|N000|D'Smith, John D'AMICO BNSA-D'FILO,T#1|3|2010|
DELETE|N000|SNELL, KENNETH L NATI-SNELL- ,K#13|3|2010|

I would appreciate your help. Thanks in advance.

Last edited by Scott; 03-09-2010 at 04:03 PM.. Reason: Code tags please...
# 2  
Old 03-09-2010
Code:
awk '{ if( $0 ~ /^(ADD|MODIFY|DELETE)/ && NR>1) {print ""; print$0} else {print $0} } ' filename | sed '/^$/d'

# 3  
Old 03-09-2010
Delete the CR at the END of the line with ADD, DELETE, MODIFY so that the next line joins this line or at the end of the previous line?

Example desired output is helpful...
# 4  
Old 03-09-2010
The desired output will be like
ADD|123|123|444|555|
ADD|N000|Smith, John BNSA-D'Fico,T#13|3|2010|
MODIFY|N000|D'Smith, John D'AMICO BNSA-D'FILO,T#1|3|2010|
DELETE|N000|SNELL, KENNETH L NATI-SNELL- ,K#13|3|2010|

Remove the CR from the line starting with ADD, MODIFY, or DELETE and merge it with the next line (record), if it does not start with the keyword.

This following command does not work awk '{ if( $0 ~ /^(ADD|MODIFY|DELETE)/ && NR>1) {print ""; print$0} else {print $0} } ' filename | sed '/^$/d'
# 5  
Old 03-09-2010
This works I believe:

Code:
perl  -0777 -nle 's/\n\s*(ADD|MODIFY|DELETE)/==$1/g; s/\n//g; s/==(ADD|MODIFY|DELETE)/\n$1/g;  print "$_\n"' filename


Last edited by drewk; 03-09-2010 at 05:07 PM..
# 6  
Old 03-09-2010
Try:
Code:
awk 'NR==1{printf("%s",$0);next}
{printf("%s%s",/^(ADD|MODIFY|DELETE)/?"\n":"",$0)}
END{print ""}' file

# 7  
Old 03-09-2010
Code:
awk '{printf ((/^(ADD|MODIFY|DELETE)/ && NR-1)?RS:S)$0}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage return/line feeds on multiple lines

I would like to remove carriage returns/line feeds in a text file, but in a specific cadence: Read first line (Header Line 1), remove cr/lf at the end (replace it with a space ideally); Read the next line (Line of Text 2), leave the cr/lf intact; Read the next line, remove the cr/lf; Read... (14 Replies)
Discussion started by: tomr2012
14 Replies

2. 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

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

removing spaces after carriage return

I have a file that I have to place a carriage return at the end of each line for another program to process it. I also need to remove all spaces after the carriage return. I searched the forums and found this command, but it removes all spaces: sed "s/*//g" ic527.txt > ic527.new The... (9 Replies)
Discussion started by: jyoung
9 Replies

6. UNIX for Advanced & Expert Users

Issue with Removing Carriage Return (^M) in delimited file

Hi - I tried to remove ^M in a delimited file using "tr -d "\r" and "sed 's/^M//g'", but it does not work quite well. While the ^M is removed, the format of the record is still cut in half, like a,b, c c,d,e The delimited file is generated using sh script by outputing a SQL query result to... (7 Replies)
Discussion started by: sirahc
7 Replies

7. Shell Programming and Scripting

Removing Carriage Return and or line feed from a file

Hello I'm trying to write a shell script which can remove a carriage return and/or line feed from a file, so the resulting file all ends up on one line. So, I begin with a file like this text in file!<CR> line two!<CR> line three!<CR> END!<CR> And I want to end up with a file... (1 Reply)
Discussion started by: tbone231
1 Replies

8. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies

9. Shell Programming and Scripting

Removing Carriage return to create one record

I have a file with multiple records in it and want to create a single record by removing all the carriage returns, is there a sed command or another command that will easily allow this to happen. current layout 813209A 813273C 812272B expected result 813209A813273C812272B previously I... (3 Replies)
Discussion started by: r1500
3 Replies

10. UNIX for Dummies Questions & Answers

Removing carriage return characters from file

Hello there, I need to remove carriage return characters (\n and \r) from any input file specified. This is what I am doing right now: - dumping the file to octal format using the command 'od -c file_name - removing and \s and \n characters using sed commands What I need to do now is... (3 Replies)
Discussion started by: b1saini
3 Replies
Login or Register to Ask a Question