LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-12-2011, 12:24 AM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Rep: Reputation: 0
coma seperations


Dear All,

I have a file which contains items like this

1234=abc xyz,aa1@domain1.com,aa2@domain1.com
2345=nn,bb1@domain.com
3456=lsop sds fds,cc1@domain.com,cc2@domain1.com

now i need to extract 4 variables 1st before "=" sign, 2nd after equal sign and comma and third after 1st coma and 2nd coma and 4th after last coma. Also there is a possibility that 4th coma doesn't exists.

I have loop which read the file line by line and extract 1st 4 character, now i need email address(es) in that loop so i can process them. I have tried while IFS=',' but don't know how to store them in variables?

Thanks in advance for the help

Edit:

I have Code like this which works fine, but i wanna do it with clean way.

Code:
#!/bin/bash

while read line; do
        code1=`echo $line | cut -c -4`
        echo "Code1 :" $code1
        echo $line > /tmp/tmp_lines_file
        while IFS=, read garb email1 email2
        do
            echo "Garbage:" $garb
            echo "Email Address1" $email1
            echo "Email Address2" $email2
        done < /tmp/tmp_lines_file
        rm /tmp/tmp_lines_file
done < MyFile.txt

Last edited by Fracker; 09-12-2011 at 12:33 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-12-2011, 12:57 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Seems like you're already doing pretty well

Here's a snippet that might help:
Code:
#/bin/bash

while read line; do
  echo line=$line...
  code1=$(echo $line|cut -d= -f1)
  echo code1=$code1...
  email1=$(echo $line|cut -d, -f2)
  email2=$(echo $line|cut -d, -f3)
  echo email1=$email1, email2=$email2
  echo
done
Code:
# Sample output:
$ cat test.txt
1234=abc xyz,aa1@domain1.com,aa2@domain1.com
2345=nn,bb1@domain.com
3456=lsop sds fds,cc1@domain.com,cc2@domain1.com

$ ./test.sh < test.txt
line=1234=abc xyz,aa1@domain1.com,aa2@domain1.com...
code1=1234...
email1=aa1@domain1.com, email2=aa2@domain1.com

line=2345=nn,bb1@domain.com...
code1=2345...
email1=bb1@domain.com, email2=

line=3456=lsop sds fds,cc1@domain.com,cc2@domain1.com...
code1=3456...
email1=cc1@domain.com, email2=cc2@domain1.com

Last edited by paulsm4; 09-12-2011 at 12:58 AM.
 
1 members found this post helpful.
Old 09-12-2011, 01:00 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your solution is already a good way to go about it, other than the fact that it's completely pointless to use a temporary text file and second loop.

The only other real change I would make is that you can set IFS to more than one character, meaning you can avoid doing the separate cut operation. I'd also move its line outside the loop so you only have to set it once.

Code:
#!/bin/bash

IFS='=,'
while read code1 garb email1 email2 ; do

	echo "Code1 : $code1"
	echo "Garbage: $garb"
	echo "Email Address1 $email1"
	echo "Email Address2 $email2"

done < MyFile.txt
In addition, always quote your variable expansions, unless you want word-splitting to occur. In this case it means quoting the entire echo'd lines.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

And finally, $(..) is highly recommended over `..`.

Last edited by David the H.; 09-12-2011 at 01:03 AM. Reason: fixed minor typos
 
2 members found this post helpful.
Old 09-12-2011, 01:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could use awk which is a doddle for this type of thing:
Code:
awk -F"[=,]" '{printf "Code1 : %s\nGarbage : %s\nEmail Address1 %s\nEmail Address2 %s\n",$1,$2,$3,$4}' file
 
1 members found this post helpful.
Old 09-12-2011, 05:44 AM   #5
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
Thanks everyone for the tips and help me to improve my script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] writing floating point to file- coma instead of dot slomek Programming 4 04-12-2007 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration