Sort data in text file in particular format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort data in text file in particular format
# 8  
Old 08-29-2017
Quote:
Originally Posted by RavinderSingh13
Hello Adfire,

Please always use code tags as per forum rules for your commands/codes/Input_files, could you please try following and let me know if this helps you.
Code:
awk -F, '
val && /^[0-9]+/ && val !~ $0{
    print val;
    for(i in b){
       if(a[i]){
         print c[i],a[i];
         delete a[i]
};
       print b[i]
};
    delete b;
    delete c
}
/^[0-9]+/{
    val=$0;
    next
}
{
    c[$1]=$1 FS $2;
    a[$1]+=$3;
    b[$1]=b[$1]?b[$1] ORS $0:$0
}
END{
    print val;
    for(i in b){
       if(a[i]){
         print c[i],a[i];
         delete a[i]
};
    print b[i]
}
}
'   Input_file

Output will be as follows.
Code:
20170308
PM,U 246
PM,I,123
PM,U,123
DA,U 135
DA,I,113
DA,U,22
20170309
PM,U 256
PM,I,23
PM,U,233
DA,U 134
DA,I,11
DA,U,123

Thanks,
R. Singh
Quote:
Hi Ravinder it works .In case we have multiple rows for single line its should display consolidated count for that row.in below example we hv two rows for PM and U .It should display consolidated count.
Code:
20170308
PM,U 246
PM,U, 113
PM,I,123
PM,U,123
DA,U 135
DA,I,113
DA,U,22

# 9  
Old 08-30-2017

Hi Ravinder
Could you please explain the code if possible? Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert text file data into XL file format

Hi i have a file containing below info and want it to put in xl format 2878042 455134 3333176 24.231979 23.81 2880246 453022 3333268 24.141338 23.81 2879677 453495 3333172 24.310986 23.81i want this data in XL file format and want that my linux system should send me that file on my mail.... (8 Replies)
Discussion started by: scriptor
8 Replies

2. Shell Programming and Scripting

Merge and Sort tabular data from different text files

I have 42 text files; each containing up to 34 lines with following structure; file1 H-01 23 H-03 5 H-05 9 H-02 14 . . file2 H-01 17 H-02 43 H-04 7 H-05 8 H-03 7 . . file3 (6 Replies)
Discussion started by: Syeda Sumayya
6 Replies

3. Shell Programming and Scripting

Sort data file by case

Hello, I'm trying to sort a large data file by the 3rd column so that all of the first words in the 3rd column that are in all uppercase appear before (or after) the non uppercase words. For example, Data file: xxx 12345 Rat in the house xxx 12345 CAT in the hat xxx 12345 Dog in the... (4 Replies)
Discussion started by: palex
4 Replies

4. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

5. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

6. UNIX for Advanced & Expert Users

Sort mixed data file

I have a text file and each field is separated by semicolon ( ; ). Field number 7 is internally separated by comma ( , ) and pipe ( | ) symbol. I want to sort file based on three different fields which are marked in BOLD. Here first BOLD field will have numbers upto the length of 9 characters,... (6 Replies)
Discussion started by: jnrohit2k
6 Replies

7. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

8. Shell Programming and Scripting

Sort a big data file

Hello, I have a big data file (160 MB) full of records with pipe(|) delimited those fields. I`m sorting the file on the first field. I'm trying to sort with "sort" command and it brings me 6 minutes. I have tried with some transformation methods in perl but it results "Out of memory". I was... (2 Replies)
Discussion started by: rubber08
2 Replies

9. UNIX for Dummies Questions & Answers

Arrangeing Swap text data in format

Hi, Please guide me if there is any option of converting text from the following format using regexp etc, 1,a,b,c,d,e,f,d 2,aa,bb,cc,dd,ee,ff 1,6a,6b,6c,6d,6e,6c 2,7a,7b,7c,7d,7e,7f to be group into 1'ns together and 2's together as follows. 1,a,b,c,d,e,f,d 1,6a,6b,6c,6d,6e,6c... (1 Reply)
Discussion started by: shar_prabs
1 Replies

10. UNIX for Dummies Questions & Answers

write data into a text file in bold format

Hi, can anyone help to write data into a text file in bold format and rollback to actual format. Thanks, Regards, Milton Y. (1 Reply)
Discussion started by: miltony
1 Replies
Login or Register to Ask a Question