How to sort lines according words?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort lines according words?
# 1  
Old 01-28-2014
How to sort lines according words?

Hello

I greped some lines from an xml file and generated a new file.
but some entries are missing my table is unsorted.
e.g.
Code:
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel" NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"

now I want to sort the lines according the string before =
all NAME= or ADDRESS= should be in one column íf this doesn't exist it should include a Tab or incluse like NAME=""

any idea?

thanks a lot

Last edited by Franklin52; 01-28-2014 at 05:59 AM.. Reason: Please use code tags
# 2  
Old 01-28-2014
Can you post expected output (sample)?
# 3  
Old 01-28-2014
e.g.

Code:
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel"  .......................  NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." .................POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"        
..................ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"

the ...... should be something like tab
all NAME= shuld be in one column, if it doesn't exist it should insert a \t
all ADDRESS= shuld be in one column if it doesn't exist it hould insert a \t

Last edited by witchblade; 01-28-2014 at 06:17 AM..
# 4  
Old 01-28-2014
It's not that easy - need to use a "_" to preserve the TAB in the first column. Not sure how to overcome that behaviour.
Code:
awk     '$1!~/^NAME/    {$1="_\t"$1; $0=$0}
         $2!~/^ADDR/    {$2="_\t"$2; $0=$0}
         $3!~/^NUMB/    {$3="_\t"$3; $0=$0}
         1
        ' file
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
NAME="Adel" _     NUMBER="2" POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." _     POSTCODE="33333"
NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"
_     ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333"

---------- Post updated at 12:40 ---------- Previous update was at 12:29 ----------

Or
Code:
awk     'BEGIN          {n=split ("NAME ADDR NUMB POST", TAG)}
         NF != n        {for (i=0; i<=NF; i++) if ($(NF-i) !~ "^"TAG[n-i]) $(NF+1-i)="\t"$(NF+1-i++)}
         1
        ' file

# 5  
Old 01-29-2014
Hi RudiC

thanks for your answere. I tried both ways and the otput is unexpected.

1. CODE:
in the original file is between TAG and content a TAB
Code:
 
NAME="Adel" TAB ADDRESS="Donaustr." TAB NUMBER="2" TAB POSTCODE="33333"
NAME="Adel" TAB ADDRESS="Donaustr." TAB NUMBER="2" TAB POSTCODE="33333"
NAME="Adel" TAB NUMBER="2" TAB POSTCODE="33333"
NAME="Adel" TAB ADDRESS="Donaustr." TAB POSTCODE="33333"

After execute the first code the TAB is removed from all lines where TAG is missing except the missing TAG
E.G

Code:
NAME="Adel" TABMISSING ADDRESS="Donaustr."TABMISSING NUMBER="2" TABMISSING POSTCODE="33333"
NAME="Adel" _TAB NUMBER="2" TABMISSING  POSTCODE="33333"
NAME="Adel" TABMISSING ADDRESS="Donaustr." _TAB POSTCODE="33333"

2. CODE:
in case of a Description field, were is an space included the end will be cutted somehow I miss parts of description content.

so i would prefere the first code, maybe you have a solution for this?

Thank you very much
# 6  
Old 01-29-2014
In the sample you posted there's no TAB, and it is not mentioned in the specification. The latter neither said there's spaces, one of the default field delimiters for awk, in the fields. Anyhow, try adding FS="\t" OFS="\t" before the file name. Same might help for code 2.
Try also
Code:
awk     '$1!~/^NAME/    {$1="NAME=\"\"\t"$1; $0=$0}
         $2!~/^ADDR/    {$2="ADDRESS=\"\"\t"$2; $0=$0}        
         $3!~/^NUMB/    {$3="NUMBER=\"\"\t"$3; $0=$0}
         1
        ' FS="\t" OFS="\t" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

How can I sort by n number is like words?

I want to sort a file with a list of words, in order of most occuring words to least occurring words as well as alphabetically. ex: file1: cat 3 cat 7 cat 1 dog 3 dog 5 dog 9 dog 1 ape 4 ape 2 I want the outcome to be: file1.sorted: dog 1 (12 Replies)
Discussion started by: castrojc
12 Replies

4. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. UNIX for Dummies Questions & Answers

words sort

hello guys i need a command that take the words from multiple files and put them in another file this way: one word needs to appear only once in the destination file with small letters no matter how it appears in source files , the words from destination file needs to be alphabetical ordered and... (10 Replies)
Discussion started by: G30
10 Replies

6. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - c 2 b 5 c 5 b 6 a 10 b 16 c 18 a 19 b 21 c 27 a 28 b 33 a 76 a 115 c 199 c 251 a 567 a 1909 (4 Replies)
Discussion started by: maq
4 Replies

7. Shell Programming and Scripting

Keep lines with specific words up in an order

I hava a file with following data: number|CREDIT_ID|NULL date|SYS_CREATION_DATE|NULL varchar2|GGS_COMMIT_CHAR|NULL varchar2|GGS_OP_TYPE|NULL number|GGS_SCN|NULL| number|GGS_LOG_SEQ|NULL number|GGS_LOG_POS|NULL number|GGS_ORACREC_SCN|NULL varchar2|BATCH_ID|NULL char|GGS_IMAGE_TYPE|NULL ... (6 Replies)
Discussion started by: kolesunil
6 Replies

8. Shell Programming and Scripting

Count the no of lines between two words

Please help in the following problem: Input is: Pritam 123 456 Patil myname youname Pritam myproject thisproject iclic Patil remaining text some more text I need the command which will display the no of lines between two words in the whole file. e.g. Display all the no of lines... (5 Replies)
Discussion started by: zsudarshan
5 Replies

9. Shell Programming and Scripting

sort words in a line

Hi Im looking for a way, hopefully a one-liner to sort words in a line e.g "these are the words in a line" to "a are in line the these words" Thanks! (15 Replies)
Discussion started by: rebelbuttmunch
15 Replies

10. Shell Programming and Scripting

Delete lines that contain 3 or more words?

How can I delete lines that contain 3 or more words? I have a file, old.txt, that has multi-word phrases in it and I want to remove the lines with 3 words or more and send the output to new.txt. I've tried the following using sed but it doesn't seem to work: sed '/(\b\w+\b){3,}/d' old.txt >... (5 Replies)
Discussion started by: revax
5 Replies
Login or Register to Ask a Question