Add a carriage return after every sorted field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a carriage return after every sorted field
# 1  
Old 07-12-2016
Add a carriage return after every sorted field

Hi All,

I have a text file -

Code:
nmn-smt-1039.test.com:SearchService-WW:x:8277
nmn-smt-1102.test.com:AdminConsole-ww:x:8536
nmn-smt-1041.test.com:SearchService-WW:x:8277
nmn-wsf-1007.test.com:Service-ww:x:8532
nmn-smt-1042.test.com:SearchService-WW:x:8277
nmn-wsf-1008.test.com:Service-ww:x:8532

I am sorting it by field 2 using sort -t: -k2.

1)
I want to add a "carriage return" after every sorted field.So that the output become -

Code:
nmn-smt-1039.test.com:SearchService-WW:x:8277
nmn-smt-1041.test.com:SearchService-WW:x:8277
nmn-smt-1042.test.com:SearchService-WW:x:8277

nmn-smt-1102.test.com:AdminConsole-ww:x:8536

nmn-wsf-1007.test.com:Service-ww:x:8532
nmn-wsf-1008.test.com:Service-ww:x:8532

2)
Once 1 is done.
I want to introduce a text on places where there is a carriage return and place the last value in the field on it ($4).

So the file should become -

Code:
text:8277
nmn-smt-1039.test.com:SearchService-WW:x:8277
nmn-smt-1041.test.com:SearchService-WW:x:8277
nmn-smt-1042.test.com:SearchService-WW:x:8277
text:8536
nmn-smt-1102.test.com:AdminConsole-ww:x:8536
text:8532
nmn-wsf-1007.test.com:Service-ww:x:8532
nmn-wsf-1008.test.com:Service-ww:x:8532

thanks.
Moderator's Comments:
Mod Comment This is your fifth infraction for refusing to use CODE and ICODE tags in your posts.

Continued posting requiring moderators and administrators to edit your posts for you may result in being banned from this site.

Please use CODE tags when displaying sample input, sample output, and code segments as required by forum rules.

Last edited by Don Cragun; 07-13-2016 at 01:14 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-13-2016
The output you provided for your sample input doesn't even come close to your stated requirements.

You said you want to sort on fields 2, 3, 4, and 1 which with your sample input would be:
Code:
nmn-smt-1102.test.com:AdminConsole-ww:x:8536
nmn-smt-1039.test.com:SearchService-WW:x:8277
nmn-smt-1041.test.com:SearchService-WW:x:8277
nmn-smt-1042.test.com:SearchService-WW:x:8277
nmn-wsf-1007.test.com:Service-ww:x:8532
nmn-wsf-1008.test.com:Service-ww:x:8532

Then you say you want to insert a carriage return after every sorted field which would insert four <carriage-return> characters into each line of your output (shown below using <CR> as a representation of the <carriage-return> characters instead of using actual <carriage-return> characters which would cause overprinting of characters in your output lines:
Code:
nmn-smt-1102.test.com<CR>:AdminConsole-ww<CR>:x<CR>:8536<CR>
nmn-smt-1039.test.com<CR>:SearchService-WW<CR>:x<CR>:8277<CR>
nmn-smt-1041.test.com<CR>:SearchService-WW<CR>:x<CR>:8277<CR>
nmn-smt-1042.test.com<CR>:SearchService-WW<CR>:x<CR>:8277<CR>
nmn-wsf-1007.test.com<CR>:Service-ww<CR>:x<CR>:8532<CR>
nmn-wsf-1008.test.com<CR>:Service-ww<CR>:x<CR>:8532<CR>

If what you meant was that you wanted to insert an empty LINE after every change in the field 4 value in the sorted output and add the string txt. followed by the field 4 value on the preceding lines in that group, that output would be:
Code:
nmn-smt-1102.test.com:AdminConsole-ww:x:8536
txt:8536
nmn-smt-1039.test.com:SearchService-WW:x:8277
nmn-smt-1041.test.com:SearchService-WW:x:8277
nmn-smt-1042.test.com:SearchService-WW:x:8277
txt:8277
nmn-wsf-1007.test.com:Service-ww:x:8532
nmn-wsf-1008.test.com:Service-ww:x:8532
txt:8532

But, of course, your sample output has SearchService-WW sorting before AdminConsole-ww and puts the inserted lines before changes in field 4 values instead of after.

Please explain these discrepancies and provide new sample output (in CODE tags), new requirements, or both so that the output you show us matches the requirements you have provided.
# 3  
Old 07-13-2016
Code:
sort -t: -k2 infile | awk -F: '!a[$NF]++ {print "text:" $NF} 1'

# 4  
Old 07-13-2016
thanks @rdrtx1.

That works for the most part.I am good.

@Don I will keep that in mind for next time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Command to add Carriage Return and Line Feed

Hello, Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist ! Thanks (16 Replies)
Discussion started by: rosebud123
16 Replies

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

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

4. Shell Programming and Scripting

Awk to remove carriage return from 65th field

Hi, I have a pipe delimited file. There are around 700 columns in the file. The 65th column has carriage return which is causing read issue with our ETL process. I would like to replace the new line characters in 65th field with "nothing" i have return the following code and need help to... (7 Replies)
Discussion started by: pinnacle
7 Replies

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

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

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

add carriage return at end of file

Hi I would like to add carriage return at end of file, because we need to mask the customer names for detailed records. Some what the file doesnot have carriage at end of line of last record.So that i 'll get 2 records when use ---aa.txt----- 1|aaa|bbb|ccc 2|bbbb|hghgh|ggg 000002 tail... (2 Replies)
Discussion started by: HAA
2 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