Write to a file in a specified order


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Write to a file in a specified order
# 1  
Old 10-05-2010
Write to a file in a specified order

This is my input file :

Code:
FCBAFE572C5E4BA29B3F8030BD480A94,907 ,1/1/2003,12/31/2005,ACT,1,2010-05-07 04:42:26.380000
2345AD5D2BFB29C821C1BC3DE8B746A7,907 ,1/1/2004,1/31/2005,ACT,1,2010-05-07 04:42:26.380000
E45B7371EEC0D1AB00E1750B5BC661F7,907 ,1/1/2004,12/31/2006,ACT,1,2010-05-07 04:42:26.380000

I am using this command :

Code:
cut -d ',' -f 1,2,4,3,5 f1.txt > temp.txt

but the output is still formed in the existing order rather the order i have mentioned.

Can anyone help?

Last edited by Scott; 10-05-2010 at 01:57 PM.. Reason: Please use code tags
# 2  
Old 10-05-2010
You could postprocess with awk or sed to swap fields. Awk would gladly do the cut, too.
# 3  
Old 10-05-2010
Hi.

The only cut I know that prints in the order you specify is on AIX, so I think you're stuck with it.

Use awk, or something else, instead.

i.e.
Code:
awk -F, '{print $1, $2, $4, $3, $5}' OFS=, f1.txt > temp.txt

# 4  
Old 10-05-2010
The HP-UX "cut" behaves the same. It just outputs your selection of fields working left-to-right regardless of the order of the parameters on the command line.
# 5  
Old 10-06-2010
@scottn :

The solution provided by you was really good.

I just want to make that reusable.

The values $1,$2,$4,$3 will be passed from a variable.

Can we loop this awk script by getting the required as parameters as input ?
# 6  
Old 10-06-2010
Sounds like a nice addition to open source cut code, if it is missing. Oops, it is now a Google world, so I should go look myself; apparently, opinions vary and IBM does as it pleases:

Quote:
http://pwet.fr/man/linux/commandes/posix/cut

A proposal to enhance cut with the following option:

-o
Preserve the selected field order. When this option is specified, each byte, character, or field (or ranges of such) shall be written in the order specified by the list option-argument, even if this requires multiple outputs of the same bytes, characters, or fields.
was rejected because this type of enhancement is outside the scope of the IEEE P1003.2b draft standard.
This User Gave Thanks to DGPickett For This Post:
# 7  
Old 10-06-2010
Quote:
Originally Posted by DGPickett
Sounds like a nice addition to open source cut code, if it is missing. Oops, it is now a Google world, so I should go look myself; apparently, opinions vary and IBM does as it pleases:
Interesting!

I've just looked at a couple of our older AIX's and can't reproduce the behaviour I said existed.

But I vividly remember debugging a problem when migrating a script from AIX to Linux that exhibited odd behaviour, and it was because cut on the AIX box was preserving the order as given on the command line.

That server has since been rebuilt and is at a higher technology level.

So, either
a) I dreamt it (vividly!)
b) there was such a version distributed at some point in time
c) someone went to some trouble to replace standard cut with a modified one

Oh well, it's no big deal Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File globbing order

Hi , I'm facing a different behaviour with one of my shell script for last few days. It was working good before that. here is my code for the script FileRemove.sh #get the file name# file1=$1 file2=$2 rm $file1 # delete the old file mv $file2 <target path> #move the new file to the target... (5 Replies)
Discussion started by: poova
5 Replies

2. Shell Programming and Scripting

Order of data in Spool File

Hello, I have a shell script through which I am executing .sql file and spooling the result of Query from .sql . I want to spool the result in ascending order. Is there any parameter to be set to print result in ascending or descending order. Thanks in advance. (4 Replies)
Discussion started by: Aparna.N
4 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

File listing in Ascending order

I have a multiple file with the following name like. file_0.csv file_1.csv file_2.csv file_3.csv file_4.csv file_5.csv file_6.csv file_7.csv file_7.csv file_8.csv file_9.csv file_10.csv file_11.csv file_12.csv file_13.csv file_14.csv (2 Replies)
Discussion started by: rakesh_arxmind
2 Replies

5. Solaris

Specifying file system mount order

Does anyone know if it's possible to specify the mount order of local and remote file systems? I'd like to mount a local ZFS file system on top of an NFS mount automatically and have that repeated on every reboot. I've checked man pages and Google, but can't find anything that addresses,... (6 Replies)
Discussion started by: Monty51
6 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. Shell Programming and Scripting

Order file by lines

My script(3 arguments $1 = folder,$2 extension,$3 string) should do the following things: -Enter in the folder of $1(if exists). -Put ls *.$2 > temp.txt ( I use a temp file to store the result of ls command and if $2 = txt in this file I'll have all the .txt files of the folder) -Now I want to... (2 Replies)
Discussion started by: Max89
2 Replies

8. Shell Programming and Scripting

sed or awk to order a file

Hi - I have a file with lots of lines in that I need to order based on the number of commas! e.g the file looks something like :- cn=john,cn=users,cn=uk,dc=dot,dc=com cn=john,cn=users,dc=com cn=users,cn=groups,dc=com cn=john,cn=admins,cn=users,cn=uk,dc=dot,dc=com... (4 Replies)
Discussion started by: sniper57
4 Replies

9. Shell Programming and Scripting

Reversing file order using SED

Im trying to develop a shell script that will change the content order of the file. For example I have a file that says a b c d I want to change this to be d c b a Im trying to use sed to this by reading the file and then inserting each line at the top #!/usr/bin/ksh ... (3 Replies)
Discussion started by: MBGPS
3 Replies

10. Shell Programming and Scripting

creating a file in reverse order of another file

Hi, I have a requirement where i need to write a script to create the new file from the given input file with the data in reverse order (bottom to top) Sample data: Input File-------------- Java VB Oracle Teradata Informatica Output file:----------------- Informatica Teradata Oracle... (3 Replies)
Discussion started by: srilaxmi
3 Replies
Login or Register to Ask a Question