Extract semicolon separated delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract semicolon separated delimiter
# 1  
Old 01-06-2010
Java Extract semicolon separated delimiter

The log reads as follows.
Code:
fname1;lname1;eid1;addr;pincode1;
fname2;lname2;eid2;addr2;pincode2;
fname3;lname3;eid3;addr3;pincode3;
fname4;lname4;eid;addr4;pincode4;

how do i extract only fname and save it in an array
similarly for lname and so on
i tried reading a file and cutting each fields..
can u help me in writing a shell script.

Last edited by Yogesh Sawant; 01-06-2010 at 02:33 AM.. Reason: added code tags
# 2  
Old 01-06-2010
how about:
Code:
cut -d ';' -f1 filename.log

# 3  
Old 01-06-2010
Try:

Code:
IFS=";"
while read line
do
set $line
A[$i]=$1;
B[$i]=$2;
C[$i]=$3;
D[$i]=$4;
(( i = i + 1 ))
done<file

# 4  
Old 01-06-2010
Code:
nawk -F";" '{for (i=1;i<=NF;i++) {a[i]=$i} print a[1]}'  infile.txt

Code:
o/p

fname1
fname2
fname3
fname4

# 5  
Old 01-06-2010
how bout this
Code:
$ cat infile
fname1;lname1;eid1;addr;pincode1;
fname2;lname2;eid2;addr2;pincode2;
fname3;lname3;eid3;addr3;pincode3;
fname4;lname4;eid;addr4;pincode4
 
$ cat tmp
#!/bin/bash
i=0
for line in `cat infile`
do
 fn[$i]=`echo $line |cut -d\; -f1`
 ln[$i]=`echo $line |cut -d\; -f2`
 ei[$i]=`echo $line |cut -d\; -f3`
 adr[$i]=`echo $line |cut -d\; -f4`
 pin[$i]=`echo $line |cut -d\; -f5`
 i=$(($i+1))
done
echo -e "\n${fn[*]}\n${ln[*]}\n${ei[*]}\n${adr[*]}\n${pin[*]}\n"
 
 
$ sh tmp
fname1 fname2 fname3 fname4
lname1 lname2 lname3 lname4
eid1 eid2 eid3 eid
addr addr2 addr3 addr4
pincode1 pincode2 pincode3 pincode4

# 6  
Old 01-06-2010
Quote:
Originally Posted by vkca
The log reads as follows.
Code:
fname1;lname1;eid1;addr;pincode1;
fname2;lname2;eid2;addr2;pincode2;
fname3;lname3;eid3;addr3;pincode3;
fname4;lname4;eid;addr4;pincode4;

how do i extract only fname and save it in an array
similarly for lname and so on
i tried reading a file and cutting each fields..
can u help me in writing a shell script.
in perl I did this.
Code:
gaurav@localhost:~$ echo 'fname1;lname1;eid1;addr;pincode1;
fname2;lname2;eid2;addr2;pincode2;
fname3;lname3;eid3;addr3;pincode3;
fname4;lname4;eid;addr4;pincode4;' | perl -le 'while(<>) {split(/;/);push(@fname,$_[0]);push(@lname,$_[1]);push(@eid,$_[2]);push(@addr,$_[3]);push(@pincode,$_[4]);}$f1=join("\n",@fname);$f2=join("\n",@lname);$f3=join("\n",@eid);$f4=join("\n",@addr);$f5=join("\n",@pincode);print $f1;print;print $f2;print;print $f3;print;print $f4;print;print $f5;'
fname1
fname2
fname3
fname4

lname1
lname2
lname3
lname4

eid1
eid2
eid3
eid

addr
addr2
addr3
addr4

pincode1
pincode2
pincode3
pincode4
gaurav@localhost:~$

Regards.Image

Enjoy.

Last edited by gaurav1086; 01-06-2010 at 04:21 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicates separated by delimiter

First post, been browsing for 3 days and came out with nothing so far. M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A2,A1-4 B4-B6,B2-B4,B4-B6,B1-B2output should be M3 C2 V5 D5 HH:FF A1-A2,A5-A6,A1-A4 B2-B4,B4-B6,B1-B2On col 6 and 7 there are strings in form of Ax-Ax and Bx-Bx respectively. Each string are... (9 Replies)
Discussion started by: enrikS
9 Replies

2. Shell Programming and Scripting

Replace semicolon within double quotes in a file with semicolon delimiter

Hello Team, Could you please help me with the below question? I have a file with the following properties 1) File Delimiter is ; 2) Text columns are within double quotes 3) Numeric columns will not have double quotes 4) File has total 6 columns Please see a sample record from file ... (3 Replies)
Discussion started by: sam99
3 Replies

3. Shell Programming and Scripting

How to use substr to extract character between two semicolon?

Dear folks Hello I have a data set which one of the column of this data set are string and I want to extract numbers which is between two ":". However, I know the substr command which will do this operation but my problem is the numbers between two ":" have different digits. this will make my... (11 Replies)
Discussion started by: sajmar
11 Replies

4. Shell Programming and Scripting

Extract string between two delimiter

I want a string between two delimeter like ( ) from file. Input File, 2007_08_07_IA-0100-014_(January).PDF 2007_08_07_IA-0100-031_(January February March April June July).PDF 2008-02-28_KR-1022-003_(January febuary march april may).CSV Output File, January January February... (19 Replies)
Discussion started by: Pratik Majithia
19 Replies

5. UNIX for Dummies Questions & Answers

Delete a semicolon and numbers after a semicolon

I have this: ((9:0.010,(11:0.089,13:0.004)) and I would like this: ((A9,(A11,A13)) How do I delete the semi colon and the number (i.e. 0.010) after the semi colon? Also, how can I add the letter before the number that is NOT removed? Thank you in advance! ---------- Post updated... (4 Replies)
Discussion started by: MDeBiasse
4 Replies

6. Shell Programming and Scripting

grep lines separated with semicolon

Hello, I would like to kindly ask you for help. I have a file with some lines in one row separated by semicolon. I need to find out, if the line I have in different variable is included in this file. e.g I have a file foo.txt with lines A=hello there;hello world;hello there world In... (6 Replies)
Discussion started by: satin1321
6 Replies

7. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

8. Shell Programming and Scripting

Compare files with fields separated with semicolon

Dear experts I have files like ABD : 5869 events, relative ratio : 1.173800E-01 , sum of ratios : 1.173800E-01 VBD : 12147 events, relative ratio : 2.429400E-01 , sum of ratios : 3.603200E-01 SDF : 17000 events, relative ratio : 3.400000E-01 , sum of ratios : 7.003200E-01 OIP: 14984... (9 Replies)
Discussion started by: Alkass
9 Replies

9. Shell Programming and Scripting

Extract comma separated value in unix

Hello All Can anyone please guide me how to solve the issue In the below code I am getting concat of two value in result variable with comma separated result=`sqlplus -s / <<EOF set pages 0 feed off; set feedback off; spool abc.txt select... (4 Replies)
Discussion started by: Pratik4891
4 Replies

10. Shell Programming and Scripting

need help extracting values from string separated by a delimiter

hi guys, basically what i'm trying to do is fetching a set of columns from an oracle database like so... my_row=`sqlplus -s user/pwd << EOF set head off select user_id, username from all_users where rownum = 1; EOF` echo $my_row the code above returns... 1 ADSHOCKER so then i... (3 Replies)
Discussion started by: adshocker
3 Replies
Login or Register to Ask a Question