Create Carriage Return using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create Carriage Return using SED
# 1  
Old 09-17-2012
Create Carriage Return using SED

I am hoping someone can help me with a solution to this problem using SED. My issue is actually two-fold. First, I have an order file that is sent to us from another system, however, there has been a change in current processes that requires a carriage return added to the end of each order. The file being sent to us is a .CSV with the orders running into each other as shown below:

Code:
0000235298~0080387170~~0000013338~COMPANY~CONTACT~ADDRESS~CITY~STATE~ZIP~US~INFO1~EMAIL~~INFO2~INFO3~PS0000235700~0080387811~~0000402296~ COMPANY ~~ ADDRESS ~ CITY ~STATE~ZIP~US~~ EMAIL ~~INFO2~INFO3~FS

The first order ends at PS and the next order begins directly after. The current software is capable of reading this properly. The problem begins with an additional software package that requires there to be a Carriage Return after PS.

My second dilemma is that there are two new fields being added to each order as shown below:

Code:
0000235298~0080387170~~0000013338~COMPANY~CONTACT~ADDRESS~CITY~STATE~ZIP~US~INFO1~EMAIL~~INFO2~INFO3~PS~Y~30000235700~0080387811~~0000402296~ COMPANY ~~ ADDRESS ~ CITY ~STATE~ZIP~US~~ EMAIL ~~INFO2~INFO3~FS
~Y~3]

Now there is a 3 at the end of the order. The final output of the information above must look like the format below:

Code:
0000235298~0080387170~~0000013338~COMPANY~CONTACT~ADDRESS~CITY~STATE~ZIP~US~INFO1~EMAIL~~INFO2~INFO3~PS~Y~3
0000235700~0080387811~~0000402296~ COMPANY ~~ ADDRESS ~ CITY ~STATE~ZIP~US~~ EMAIL ~~INFO2~INFO3~FS ~Y~

Also keep in mind there may or may not be a Y or even a 3 at the end, leaving data possibly looking like this PS~~. However, the system will know what to do as long as there is a Carriage Return at the end. [FONT=Calibri]The only tip i can think of regarding patterns is there will always be 18 ~ delimiters if that helps with finding a pattern.

I hope I have not confused this and that someone might be able to help me out with this. Thank you for any help you might have as well as your time.

---------- Post updated at 02:44 PM ---------- Previous update was at 12:14 PM ----------

In addition to the pattern I mentioned already, you can basically state that after the 18th ~ delimiter if there is a 3 then add a carriage return after the 3 otherwise add a carriage return after the 18th ~ delimiter.

Last edited by Corona688; 09-17-2012 at 01:59 PM..
# 2  
Old 09-17-2012
awk solution. not sed.

Code:
 
awk -f a.awk infile

where a.awk:

Code:
 
{
 FS="~";
 for (i=1; i<=NF; i++) {
  if (length($(i))>10 && $i ~ /.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/) {
    w1=$(i);
    sub(/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/, "", w1);
    print w1 "";
    w2=substr($(i), 1+length(w1));
    printf (w2) "~";
  } else {
    if (i==NF) {
       print $(i);
    } else {
       printf $(i) "~";
    }
  }
 }
}

Works for first field of 10 digits.
# 3  
Old 09-17-2012
This is nearly exactly what we need rdrtx1 thank you. However, for some reason it adds delimiters where they shouldn't be but only in the first line. Otherwise, all other lines are perfect.

Original first line:
Code:
0000255844~0080417885~~0000031501~FIRST LAST~DBA ANY COMPANY~142 W 1050 N~CITY~STATE~ZIP~US~INFO1~EMAIL~INFO2~INFO3~INFO4~CS~Y~3

After script:
Code:
0000255844~0080417885~~0000031501~FIRST~LAST~DBA~ANY~COMPANY~142~W~1050~N~CITY~STATE~ZIP~US~INFO1~EMAIL~INFO2~INFO3~INFO4~CS~Y~3

# 4  
Old 09-17-2012
Using 1 line in input file:

Code:
0000255844~0080417885~~0000031501~FIRST LAST~DBA ANY COMPANY~142 W 1050 N~CITY~STATE~ZIP~US~INFO1~EMAIL~INFO2~INFO3~INFO4~CS~Y~3

Output:

Code:
 
0000255844~0080417885~~0000031501~FIRST LAST~DBA ANY COMPANY~142 W 1050 N~CITY~STATE~ZIP~US~INFO1~EMAIL~INFO2~INFO3~INFO4~CS~Y~3


I do not get spaces replaced by "~".

Maybe using sledge hammer to escape spaces:

New awk:

Code:
 
{
 FS="~";
 gsub(" ","!");
 for (i=1; i<=NF; i++) {
  if (length($(i))>10 && $i ~ /.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/) {
    w1=$(i);
    sub(/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/, "", w1);
    gsub("!"," ", w1);
    print w1 "";
    w2=substr($(i), 1+length(w1));
    gsub("!"," ", w2);
    printf (w2) "~";
  } else {
    gsub("!"," ", $(i));
    if (i==NF) {
       print $(i);
    } else {
       printf $(i) "~";
    }
  }
 }
}


Last edited by rdrtx1; 09-17-2012 at 06:01 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 09-17-2012
Don't know if I understand the problem exactly, but this sed maybe solve it:
Code:
sed -E 's/([^~]*~){18}3?/&\
/'


Last edited by 244an; 09-17-2012 at 11:01 PM..
This User Gave Thanks to 244an For This Post:
# 6  
Old 09-18-2012
Success!! That worked great rdrtx1. Thank you again. 244an I have not tried your solution yet but thank you for responding. I will post again when I try it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2 carriage return within a record

Hi all, need your help in replacing carriage return in a record. Input: col1|col2|col3|col4|col5|col6|col7|col8|col9|col10 1|aa|bb|cc|dd|eee eee|ff|ggggg|hh hhh|iii 2|zz|yy|xx|ww|vv|uu|tt|ss|rr Output: col1|col2|col3|col4|col5|col6|col7|col8|col9|col10... (12 Replies)
Discussion started by: agathaeleanor
12 Replies

2. Shell Programming and Scripting

Why sed command deletes last line in a file if no carriage return?

Hi I am using sed command to make SCORE=somevalue to SCORE=blank in a file. Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line. cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt Why does sed command remove the... (3 Replies)
Discussion started by: ashok.k
3 Replies

3. UNIX for Dummies Questions & Answers

carriage return and linefeed

hi can anyone please tell me the difference between carriage return, linefeed and newline ? (2 Replies)
Discussion started by: streetfi8er
2 Replies

4. UNIX for Dummies Questions & Answers

use sed to replace whitespace with a carriage return

Greetings I need to replace "whitespace" in a file with the newline character aka carriage return My command is either wrong or not interpreted properly by me shell sed s/" "/\\n" "/g nets > nets1 or sed s/" "/\n" "/g nets > nets1 nets (input file) 13MHZ_IN... (4 Replies)
Discussion started by: awk_sed_hello
4 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. Shell Programming and Scripting

Delete carriage return in SED

Hi everybody! I'm working in one script with sed, I have file with the next content: <voms.db.type value="changeme"/> <voms.db.host value="changeme"/> <voms.admin.smtp.host value="changeme"/> <voms.mysql.admin.password value="changeme"/> <glite.installer.verbose value="true"/> ... (3 Replies)
Discussion started by: juedsivi
3 Replies

7. Shell Programming and Scripting

How to delete carriage return in SED

Could someone tell me how to do the below without opening the file? (eg in sed or awk) I have a file with the contenst below: $ more file1.txt 10 AAA; 200 BBB; 3 CCC; I want to delete the carriage return of one line above the line which has ";" at the end to get the... (3 Replies)
Discussion started by: stevefox
3 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. 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