Split function input and output order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split function input and output order
# 1  
Old 09-02-2010
Split function input and output order

Dear Forum

I'm Trying to use split function to split a string, but the output is not as the same order as of the string, please see simple example

Code:
echo " " | nawk -v var="First;Second;Third;Fourth" '
BEGIN {split(var, arr,";") for(i in arr){print arr[i] }}'

The output is

Code:
Second
Third
Fourth
First

How can I make the output as same order as the input string ?

Needed Output
First
Second
Third
Fourth
# 2  
Old 09-02-2010
Code:
echo " " | nawk -v var="First;Second;Third;Fourth" '
BEGIN {n=split(var, arr,";") for(i=1;i<=n;i++){print arr[i] }}'

# 3  
Old 09-02-2010
Thanks a lot franklin, so this is an issue of split function ? is there any rule that can tell how "split" function do its work ?
# 4  
Old 09-02-2010
Quote:
Originally Posted by yahyaaa
Thanks a lot franklin, so this is an issue of split function ? is there any rule that can tell how "split" function do its work ?
From an excerption of the gawk manual:
Quote:
split(string, array [, fieldsep])
This function divides string into pieces separated by fieldsep and stores the pieces in array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, is a regexp describing where to split string (much as FS can be a regexp describing where to split input records). If fieldsep is omitted, the value of FS is used. split returns the number of elements created.
# 5  
Old 09-02-2010
Adding to that...

The "problem" is not with split. It's with arrays:

(also from the man page...)
Code:

 There is a form of the for statement that loops over each index    of  anarray.

        for ( var in array ) statement

sets var to each index of array and executes statement.    The order that var transverses the indices of array is not defined.

# 6  
Old 09-02-2010
To get the same order with gawk, you can use the undocument variable WHINY_USERS:
Code:
WHINY_USERS=1 gawk -v var="First;Second;Third;Fourth" '
BEGIN {split(var, arr,";") for(i in arr){print arr[i] }}'

# 7  
Old 09-02-2010
Quote:
Originally Posted by Franklin52
To get the same order with gawk, you can use the undocument variable WHINY_USERS:
Code:
WHINY_USERS=1 gawk -v var="First;Second;Third;Fourth" '
BEGIN {split(var, arr,";") for(i in arr){print arr[i] }}'

Smilie

Makes me laugh every time Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to iterate a function untill last argument with any order of input?

HI I need to get the function "kick" to get executed in any way the parameters are passed in to the function. The parameters are first stored in a dictionary self.otherlist = {} print self.otherlist self.populateTestList(self.system_type) print... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

2. Shell Programming and Scripting

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. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 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

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

5. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

6. Shell Programming and Scripting

conversion of input date in reverse order

Dear all, I am having date as input 01-08-2011 ...i want to convert it by removing - (hyphens) in reverse order...i.e 20110801 can u please help me (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

8. UNIX for Dummies Questions & Answers

split function

Hi all! I am relatively new to UNIX staff, and I have come across a problem: I have a big directory, which contains 100 smaller ones. Each of the 100 contains a file ending in .txt , so there are 100 files ending in .txt I want to split each of the 100 files in smaller ones, which will contain... (4 Replies)
Discussion started by: ktsirig
4 Replies

9. Shell Programming and Scripting

How to grep in order based on the input file

Here is an answer rather than a question that I thought I would share. In my first attempt, I was using grep to find a list from file.1 within another file, file.2 which I then needed to paste the output from file.3 to file.1 in the same order. However, the results weren't what I wanted. At... (2 Replies)
Discussion started by: Kelam_Magnus
2 Replies
Login or Register to Ask a Question