Seperate contents in a file with | as delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Seperate contents in a file with | as delimiter
# 1  
Old 01-22-2008
Seperate contents in a file with | as delimiter

Hi,

I do have a file with follwoing as contents:
816|817118|
816|933370|
816|1215241|

I want to store the above values into two arrays as follows:

arr1[] = { 816,816,816}
arr2[] = {817118,933370,1215241}

How it can be achieved ?
# 2  
Old 01-22-2008
What language do you want to use? C, C++, BASH, AWK, or what?
# 3  
Old 01-22-2008
Sorry for not provding that .. Its BASH Scripts..
# 4  
Old 01-22-2008
Code:
unset arr1 arr2 i j
while IFS=\| read one two;do 
  arr1[i++]="$one"
  arr2[j++]="$two"
done<filename

# 5  
Old 01-23-2008
Hi rad..
I am sorry .. not able to understand the code given by you.. Will that work out in shell scripting ??
# 6  
Old 01-23-2008
You may need to change the syntax for older bash versions:

Code:
bash 3.2.25(1)$ cat file
816|817118|
816|933370|
816|1215241|
bash 3.2.25(1)$ unset arr1 arr2 i j
bash 3.2.25(1)$ while IFS=\| read one two;do    arr1[i++]="$one";   arr2[j++]="$two"; done<file
bash 3.2.25(1)$ echo ${arr1[0]}
816
bash 3.2.25(1)$ echo ${arr1[2]}
816
bash 3.2.25(1)$ echo ${arr2[2]}
1215241

Or, if you prefer:

Code:
$ arr1=($(cut -d\| -f1 file))
$ arr2=($(cut -d\| -f2 file))
$ echo ${arr1[0]}
816
$ echo ${arr1[@]}
816 816 816
$ echo ${arr2[@]}
817118 933370 1215241
$ echo ${arr2[1]}
933370


Last edited by radoulov; 01-23-2008 at 05:31 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. Shell Programming and Scripting

delimiter contents

Hi All, I have a file with contents : cat >file1.sh qw^A34^Aer the command i am using is: cut -f1,2,3 -d"^A" file1.sh which gave output as: qw^A34^Aer and the command cut -f2 -d"^A" file1.sh gave an output : A34 i need the output as : (15 Replies)
Discussion started by: gsandhya31
15 Replies

6. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

7. Shell Programming and Scripting

Seperate file content

hi all, i have some file whoes contents are 0 /home8/mc09ats/UnixCw/a1 1 /home8/mc09ats/UnixCw/a2 2 /home8/mc09ats/b3 3 /home8/mc09ats/UnixCw/d1 i want to seperate the content following way... fileindex= 0 filepath=... (4 Replies)
Discussion started by: AbhijitIT
4 Replies

8. UNIX for Dummies Questions & Answers

Script for parsing details in a log file to a seperate file

Hi Experts, Im a new bee for scripting, I would ned to do the following via linux shell scripting, I have an application which throws a log file, on each action of a particular work with the application, as sson as the action is done, the log file would vanish or stops updating there, the... (2 Replies)
Discussion started by: pingnagan
2 Replies

9. Shell Programming and Scripting

seperate elements of a file

i want to write a script in Bash Shell that accept a list of files.an example of file is 4334:234 322.345:32 234:3452 e.t.c each file only contain lines like num1:num2 i want to count the lines of this file and find the summary of X=4334+322.345+234 and Y=234+32+3452 (1 Reply)
Discussion started by: nektarios4u
1 Replies

10. Shell Programming and Scripting

Split File into seperate files

Hi, So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example: blah blah blah ------ more text ----- and some more text So basically the first part should be... (4 Replies)
Discussion started by: eltinator
4 Replies
Login or Register to Ask a Question