Sponsored Content
Top Forums Shell Programming and Scripting How to read records in a file and sort it? Post 302744029 by Corona688 on Thursday 13th of December 2012 04:28:31 PM
Old 12-13-2012
Code:
sort -t'|' -k 3,3 inputfile | while IFS="|" read ...
do
        ...
done

Note that because of the pipe, variables set inside the while-read loop won't be changed outside of it.

When you're setting that many variables at once with read, I'd be tempted to just set -- instead of typing out 37 names all the time.

Code:
OLDIFS="$IFS"
IFS="|"
sort -t'|' -k 3,3 inputfile | while IFS="" read LINE
do
        set -- $LINE
        # ${1} through ${14} are now set to column values
done
IFS="$OLDIFS"


Last edited by Corona688; 12-13-2012 at 05:34 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a particular line in a file with 500000 records init

I am trying to open a file and check a record. The file has 543267 records and I want to check the record 514455. I am trying to open in vi and go to that line :514455 but i get the error not enough lines in buffer How should i overcome this? Please help. (6 Replies)
Discussion started by: mhssatya
6 Replies

2. Shell Programming and Scripting

Sort & Split records in a file

Hi, I am new to scripting. I need a script to sort and the records in a file and then split them into different files. For example, the file is: H1...................... H2...................... D2.................... D2.................... H1........................... (15 Replies)
Discussion started by: Sunitha_edi82
15 Replies

3. Shell Programming and Scripting

read records from a file

Hi all, I have a requirement where I need to read records one by one from a file. I have tried this below code: while read mLine do echo 'Line = '${mLine} done < input_file --- But the problem here is im getting the records with removed spaces. --Supposer if the record is like... (3 Replies)
Discussion started by: srilaxmi
3 Replies

4. Shell Programming and Scripting

need shell script to read particular records from a file

i am reading an i/p file input.txt as below and want to read all filenames as in highlighted in bold below and put them in a different file output.txt. can someone help me with a shell script to do this? thanks in advance regards brad input.txt --------- START TYPE:OPT INIT_SEQ:01... (8 Replies)
Discussion started by: bradc
8 Replies

5. Shell Programming and Scripting

sort a file which has 3.7 million records

hi, I'm trying to sort a file which has 3.7 million records an gettign the following error...any help is appreciated... sort: Write error while merging. Thanks (6 Replies)
Discussion started by: greenworld
6 Replies

6. Shell Programming and Scripting

How to read each 2 records from a file

Hi, I have 10000 records in my test.dat file All records are under the following format a,12,45,bn,c a,16,46,bn1,c a,18,47,bn2,c a,12,47,bn3,c a,11,49,bn4,c I have to read each 2 records and assign it into a temp file .Can anybody help me in this? Thanks (3 Replies)
Discussion started by: kavithakuttyk
3 Replies

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

8. Shell Programming and Scripting

Read a file with n records as one big string using linux

Hello! Is there a way i can read a file with n records as one big string using linux shell script? I have a file in the below format - REC1 REC2 REC3 . . . REC4 Record length is 3000 bytes per record and with a newline char at the end. What i need to do is - read this file as one... (5 Replies)
Discussion started by: mailme0205
5 Replies

9. Shell Programming and Scripting

Read File and check records for length

I need a script that will run in unix to: 1) Read and input file with 1 column that contains for ex: 0123456789 1234567890 ...etc 2) Checks the first column if it is: a. Numeric from 0 - 9 b. if it is not less... (4 Replies)
Discussion started by: mrn6430
4 Replies

10. Shell Programming and Scripting

How to read a particular records from a file?

Hi All Can anybody let me know the code to read a particular record from a file For e.g. File Name: File.txt File content: Script_path=/abc/def/script/ File_path=/xyz/data/ Business Date=19990905 SERVER_NAME=Server DATABASE_NAME=Database Login=NewUser Password=NewPassword ... (3 Replies)
Discussion started by: Siddhartha9833
3 Replies
XMLSORT(1p)						User Contributed Perl Documentation					       XMLSORT(1p)

NAME
xmlsort - sorts 'records' in XML files SYNOPSIS
xmlsort -r=<recordname> [ <other options> ] [ <filename> ] Options: -r <name> name of the elements to be sorted -k <keys> child nodes to be used as sort keys -i ignore case when sorting -s normalise whitespace when comparing sort keys -t <dir> buffer records to named directory rather than in memory -m <bytes> set memory chunk size for disk buffering -h help - display the full documentation Example: xmlsort -r 'person' -k 'lastname;firstname' -i -s in.xml >out.xml DESCRIPTION
This script takes an XML document either on STDIN or from a named file and writes a sorted version of the file to STDOUT. The "-r" option should be used to identify 'records' in the document - the bits you want sorted. Elements before and after the records will be unaffected by the sort. OPTIONS
Here is a brief summary of the command line options (and the XML::Filter::Sort options which they correspond to). For more details see XML::Filter::Sort. -r <recordname> (Record) The name of the elements to be sorted. This can be a simple element name like 'person' or a pathname like 'employees/person' (only person elements contained directly within an employees element). -k <keys> (Keys) Semicolon separated list of elements (or attributes) within a record which should be used as sort keys. Each key can optionally be followed by 'alpha' or 'num' to indicate alphanumeric of numeric sorting and 'asc' or 'desc' for ascending or descending order (eg: -k 'lastname;firstname;age,n,d'). -i (IgnoreCase) This option makes sort comparisons case insensitive. -s (NormaliseKeySpace) By default all whitespace in the sort key elements is considered significant. Specifying -s will case leading and trailing whitespace to be stripped and internal whitespace runs to be collapsed to a single space. -t <directory> (TempDir) When sorting large documents, it may be prudent to use disk buffering rather than memory buffering. This option allows you to specify where temporary files should be written. -m <bytes> (MaxMem) If you use the -t option to enable disk buffering, records will be collected in memory in 'chunks' of up to about 10 megabytes before being sorted and spooled to temporary files. This option allows you to specify a larger chunk size. A suffix of K or M indicates kilobytes or megabytes respectively. SEE ALSO
This script uses the following modules: XML::SAX::ParserFactory XML::Filter::Sort XML::SAX::Writer AUTHOR
Grant McLean <grantm@cpan.org> COPYRIGHT
Copyright (c) 2002 Grant McLean. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2002-06-14 XMLSORT(1p)
All times are GMT -4. The time now is 03:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy