2 carriage return within a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 carriage return within a record
# 1  
Old 08-18-2011
2 carriage return within a record

Hi all,

need your help in replacing carriage return in a record.

Input:
Code:
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:
Code:
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

Currently i execute script to get rid of carriage return, but it concatenates records start from line2 into one whole record
Code:
grep -v '^$' Input.txt | nawk -F\| 'FNR==1{n=NF}NF<n{for (i=1;i<=n;i++) if (i<n) {l=$0;getline;$0=l":::"$0}}1'

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

Your help is much appreciate.

Moderator's Comments:
Mod Comment Please use code tags, thanks.

Last edited by zaxxon; 08-18-2011 at 01:59 AM.. Reason: code tags
# 2  
Old 08-18-2011
You have already a lot of posts - you should be familiar with using code tags.

Code:
awk -F\| 'NF<10 {l=l? l OFS $0: l $0; next} l {print l} 1 ' OFS=":::" infile
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

# 3  
Old 08-18-2011
I think I saw the similar request somewhere else before.

Code:
awk '{printf /^col1/||/^[0-9]/?RS $0:":::" $0}' infile

# 4  
Old 08-18-2011
Yes, rdcwayx, i have been posting the carriage-return-related topic as i yet resolve the issue Smilie

Code:
awk '{printf /^col1/||/^[0-9]/?RS $0:":::" $0}' infile

I executed the above code, running fine with the given sample file.

As the actual source file is too lengthy, i avoid to post it here.

When i applied the code above, it doesnt work for my actual source file Smilie

The actual source file has 16 columns in total, first column name is VALUE_ID and thus i modified the code as

Code:
/usr/xpg4/bin/awk '{printf /^VALUE_ID/||/^[0-15]/?RS $0:":::" $0}' KP_IU_KP_FT_KP_VALUE_new.txt

it hits error:
/usr/xpg4/bin/awk: line 0 (NR=5): insufficient arguments to printf or sprintf

Thanks in advance for your help.
# 5  
Old 08-18-2011
Did you try out my example?
# 6  
Old 08-18-2011
Yea, zaxxon. I have tested with your code also.
It works with sample file, but not with my actual file.

Modified to be

Code:
/usr/xpg4/bin/awk -F\| 'NF<16 {l=l? l OFS $0: l $0; next} l {print l} 1 ' OFS=":::" KP_IU_KP_FT_KP_VALUE.txt


It chopped off all the records with carriage return, left header and one data row which has no carriage return.
# 7  
Old 08-18-2011
Could you please provide a longer and more accurate snippet of your input please? Use code tags when doing so, thanks.
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

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. Shell Programming and Scripting

Remove carriage return in a record

Hi all gurus, I need help in removing carriage return existed within a record delimited by pipe <|>. Sample: A_01|Test1|Testing1|Remarks1 A_02|Test2|Test ing2|Remarks2 A_03|Test3|Testing3| Remarks3 Desire output: A_01|Test1|Testing1|Remarks1 A_02|Test2|Testing2|Remarks2... (10 Replies)
Discussion started by: agathaeleanor
10 Replies

6. Shell Programming and Scripting

Lost carriage return when assign

Hello . Now i have a different problem, lost "carriage return" when assigning a variable. The assignation is done in a peculair way but its ok. The variable "v_tmp" have spaces and carriage return. Its created with v_tmp=`echo $i | awk '.........'` And the assignation where i lost all the... (6 Replies)
Discussion started by: trutoman
6 Replies

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

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

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