Want to grep records in alphabetical order from a file and split into other files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to grep records in alphabetical order from a file and split into other files
# 1  
Old 07-25-2015
Want to grep records in alphabetical order from a file and split into other files

Hi All,

I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z.

I tried below but it did not work.

Code:
for i in {A..Z}; do grep ^$i ; done< erg.txt


Using this logic it just gives tables starting with A and then stops. While it should give all files in sequence, then I can use some logic to save them alphabetically Please advise.

here, erg.txt is the name of main file. Sample is like this

Code:
user@86340-hostname:~$ cat erg.txt | head
ABD_DET
ABS_MSTR
ABSC_DET
ABSCC_DET
ABSD_DET
ABSI_MSTR
ABSL_DET
ABSP_DET
ABSPLI_REF
ABSR_DET

# 2  
Old 07-25-2015
Code:
awk 'NF{if(f) close(f);f=substr($1,1,1)".txt";print >>f}' erg.txt

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-25-2015
This proposal is nice and short and solves the problem, but in case (large chunks of) the input file is sorted, it performs too many unnecessary file open/close operations. Try a small adaption:
Code:
awk '
NF      {TMP=substr($1,1,1)".txt"
         if (FN && FN != TMP) close (FN)                 
         FN=TMP
         print >> FN
        }
' file

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 07-26-2015
Quote:
Originally Posted by RudiC
This proposal is nice and short and solves the problem, but in case (large chunks of) the input file is sorted, it performs too many unnecessary file open/close operations. Try a small adaption:
Code:
awk '
NF      {TMP=substr($1,1,1)".txt"
         if (FN && FN != TMP) close (FN)                 
         FN=TMP
         print >> FN
        }
' file


Thanks..It worked like a magic Smilie


Could you please explain the logic as well? I never used FN in awk so it will be a learning for me.Smilie

Last edited by shekhar_4_u; 07-26-2015 at 01:17 AM..
# 5  
Old 07-26-2015
It extracts the first character from the first field in every line and stores it, extended by the string constant ".txt", into a temp string variable. If this differs from the old file name in variable FN, close the old file. Then assign the temp var to the file name var FN, and append the entire line to this file.

BTW, a small adaption to your own code snippet would have made it work:
Code:
for i in {A..Z}; do grep ^$i <erg.txt >$i.txt ; done

, although it would have opened and read erg.txt 26 times.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-26-2015
Quote:
BTW, a small adaption to your own code snippet would have made it work:
Code:
for i in {A..Z}; do grep ^$i <erg.txt >$i.txt ; done

, although it would have opened and read erg.txt 26 times.
Plus, it would have created 26 files with A to Z whether grep found content to place into or not from erg.txt.
# 7  
Old 07-27-2015
Thanks guys! I appreciate your responses.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. Shell Programming and Scripting

Sorting lines between patterns in alphabetical order

Hi, need help in sorting lines between strings "<section status = “ole-service”>" and "</section>" in alphabetical order, based on the text in red. Hoping for an AWK or SED solution. Thank you. ... <section status = “ole-service”>... <p service = "OOO">XZZ</p> <p service = "AAA">AAA... (3 Replies)
Discussion started by: pioavi
3 Replies

3. UNIX for Dummies Questions & Answers

Script to list applications in alphabetical order

I've looking over a script for work and I've had a problem with the script not listing the files in alphabetical order. To look up PIDs for apps, it would be beneficial to have them listed in that order. Here is what I've been reviewing. #!/usr/bin/perl $str = sprintf "%4s %-40s", "PID",... (7 Replies)
Discussion started by: whysolucky
7 Replies

4. Shell Programming and Scripting

[SHELL] Userlist alphabetical order

Hi everyone! I am new to the forum and have recently started working with Linux. Quick question, I want a user list in alphabetical order as the output of a shell script. Who can help me!? Thanks! From the netherlands ;) (5 Replies)
Discussion started by: dennisbest85
5 Replies

5. UNIX for Dummies Questions & Answers

Alphabetical sort for multi line records contains in a single file

Hi all, I So, I've got a monster text document comprising a list of various company names and associated info just in a long list one after another. I need to sort them alphabetically by name... The text document looks like this: Company Name: the_first_company's_name_here Address:... (2 Replies)
Discussion started by: quee1763
2 Replies

6. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

7. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

8. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies

9. Shell Programming and Scripting

alphabetical order with out using sort command

hai, how can i sort a file alphabetically without using sort command (6 Replies)
Discussion started by: rahul801
6 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question