Visit The New, Modern Unix Linux Community


sort -t option causing code to fail need ASCII character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort -t option causing code to fail need ASCII character
# 1  
sort -t option causing code to fail need ASCII character

Hello,

When I run this UNIX code without the -t option it gives me the desired results.

The code keeps the record with the greatest datetime based on the key columns.
I sort it first then sort it again with the -u option, that's it.

I need to have a variable to specify an ASCII character such as tab or the unit separator as the field separator. This is ASCII 009 for tab or 031 for Unit Separator.

In this case I'm trying tab.
My file is in fact tab-delimited.

When I run the script I get the error:

Code:
sort: empty tab
sort: empty tab


unix command

Code:
s=$(printf "\009")

sort -t "$s" -k1,1 -k2,2 -k4,4 -k5,5r input.txt > sortedinput.txt

sort -t "$s" -k1,1 -k2,2 -k4,4 sortedinput.txt -u > nodups.txt

input file - input.txt
Code:
21erescca    010240    8    10    sct_det3_10_20110516_143936.txt
41erescca    010240    7    10    sct_det3_10_20110516_143936.txt
21erescca    010240    4    10    sct_det3_10_20110517_143936.txt
41erescca    010240    6    10    sct_det3_10_20110517_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
21erescca    010245    8    10    sct_det3_10_20110516_143936.txt
11erescca    010240    1    10    sct_det3_901_20110516_143936.txt
41erescca    4010240    6    10    sct_det3_10_20110517_143936.txt
11erescca    010240    06    10    sct_det3_901_20110517_143936.txt


sortedinput.txt - desired file
Code:
11erescca    010240    06    10    sct_det3_901_20110517_143936.txt
11erescca    010240    1    10    sct_det3_901_20110516_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
21erescca    010240    4    10    sct_det3_10_20110517_143936.txt
21erescca    010240    8    10    sct_det3_10_20110516_143936.txt
21erescca    010245    8    10    sct_det3_10_20110516_143936.txt
41erescca    010240    6    10    sct_det3_10_20110517_143936.txt
41erescca    010240    7    10    sct_det3_10_20110516_143936.txt
41erescca    4010240    6    10    sct_det3_10_20110517_143936.txt


output file - nodups.txt - desired file
Code:
11erescca    010240    06    10    sct_det3_901_20110517_143936.txt
21erescca    010240    4    10    sct_det3_10_20110517_143936.txt
21erescca    010245    8    10    sct_det3_10_20110516_143936.txt
41erescca    010240    6    10    sct_det3_10_20110517_143936.txt
41erescca    4010240    6    10    sct_det3_10_20110517_143936.txt

# 2  
By default sort takes white space chars as delimiter, in your case you fields only delimited by tab, so you don't have to specif any delimiter, I have executed on solaris, it worked fine for me
Code:
user@host> (/home/user) $ sort  -k1,1 -k2,2 -k4,4 -k5,5r test.txt >sorted.txt
user@host> (/home/user) $ sort  -k1,1 -k2,2 -k4,4  sorted.txt -u
11erescca    010240    06    10    sct_det3_901_20110517_143936.txt
21erescca    010240    4    10    sct_det3_10_20110517_143936.txt
21erescca    010245    8    10    sct_det3_10_20110516_143936.txt
41erescca    010240    6    10    sct_det3_10_20110517_143936.txt
41erescca    4010240    6    10    sct_det3_10_20110517_143936.txt
user@host> (/home/user) $ cat sorted.txt
11erescca    010240    06    10    sct_det3_901_20110517_143936.txt
11erescca    010240    1    10    sct_det3_901_20110516_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
11erescca    010240    2    10    sct_det3_10_20110516_143936.txt
21erescca    010240    4    10    sct_det3_10_20110517_143936.txt
21erescca    010240    8    10    sct_det3_10_20110516_143936.txt
21erescca    010245    8    10    sct_det3_10_20110516_143936.txt
41erescca    010240    6    10    sct_det3_10_20110517_143936.txt
41erescca    010240    7    10    sct_det3_10_20110516_143936.txt
41erescca    4010240    6    10    sct_det3_10_20110517_143936.txt

# 3  
Quote:
Originally Posted by kumaran_5555
By default sort takes white space chars as delimiter, in your case you fields only delimited by tab, so you don't have to specif any delimiter, I have executed on solaris, it worked fine for me
Hi !

thanks for testing it Smilie

Yes I know it works the way it is but I will be using it with different files and I need to be able to change the field separator with a variable.

So I need to use the -t option and specify an ASCII code. Some files use the ASCII 031 Unit Separator. Some use tab which is ASCII 009.

ASCII 031 doesn't show up well on the forum post so I put the example for ASCII tab 009.

This is definitely a requirement for me to always specify the field delimiter as an ASCII code.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #916
Difficulty: Easy
In Javascript, the console is an object which provides access to the browser debugging console.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What is ASCII character?

Hi Guru, I have put one post yesterday and get answer. thanks for your help. my question today is: what is ascii character for following non printable characters: ( we need filter these characters out in another process) ^MM-^E^MM-^E. Old post link: ... (5 Replies)
Discussion started by: ken002
5 Replies

2. UNIX for Advanced & Expert Users

Veritas Cluster automatic fail-back option on Solaris

Hi - Please help me to understand the Veritas Cluster fail-over capability. We configured oracle database file system on veritas cluster file system and it is automatically failing-over from node 1 to node 2. Does Veritas cluster softward have any option to fail-back from node 2 to node 1... (6 Replies)
Discussion started by: Mansoor8810
6 Replies

3. Shell Programming and Scripting

Print the next ASCII character

Hi, In my file, for few field I have to print the next ASCII character for every character. In the below file, I have to do for the 2,3 and 5th fields. Input File ======== 1|abc|def|5|ghi 2|jkl|mno|6|pqr Expected Ouput file ======= 1|bcd|efg|5|hij 2|klm|nop|6|qrs (2 Replies)
Discussion started by: machomaddy
2 Replies

4. UNIX for Dummies Questions & Answers

How to grep for a non-standard ASCII character?

A very simple question but I have scoured the web and can't find an answer. How do I search for a character by ASCII code in a regular expression using grep? For example, we use the End of Medium symbol as a delimiter in certain files. (this is ascii 031 in oct, displays as ^Y) I want to grep... (6 Replies)
Discussion started by: DJR
6 Replies

5. UNIX for Advanced & Expert Users

ASCII Character Set

I thought I would point this out. This has a lot of the non printing characters. ASCII Character Set (7 Replies)
Discussion started by: cokedude
7 Replies

6. Shell Programming and Scripting

perl sort unicode non-ascii letters

In another thread (field separator in Perl) I nearly solved my sorting problem and I finally understood the Schwartzian transform especially thank to KevinADC. After that I've found out that the sorting was not done the way I need it. I did not notice it at first because I used all vowels as a... (6 Replies)
Discussion started by: ahsog
6 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. UNIX for Dummies Questions & Answers

Ascii value of character?

Is there a way to determine the ascii value of a character? For example, let's say a shell variable has the value 'A'. I would like it's ascii value (e.g. 65 in this case). I would like to do this from a script (preferably ksh). (12 Replies)
Discussion started by: sszd
12 Replies

9. UNIX for Dummies Questions & Answers

curl and --fail option

--fail seems to not be working in cURL. When using cURL to download multiple files, --fail is supposed to keep cURL from making files that don't exist, on 404 errors. But when I use --fail or -f, it still makes those files. I've tried it on both Cygwin cURL and Win32 cURL. Anyone know anything... (2 Replies)
Discussion started by: Pulseczar
2 Replies

10. Shell Programming and Scripting

Environment variables causing cronjob to fail

I am trying to run a script which makes use of these environment variables, hence i need to export them out. i've posted a similar post in an oracle forum. I've posted the same qn here to get some advise on the scripting aspect. i execute my program in an unix environment by typeing... (2 Replies)
Discussion started by: new2ss
2 Replies

Featured Tech Videos