LinuxQuestions.org
Visit Jeremy's Blog.
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, 01:24 AM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 86

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 01:33 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-12-2011, 01: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 01:58 AM.
 
1 members found this post helpful.
Old 09-12-2011, 02:00 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 02:03 AM. Reason: fixed minor typos
 
2 members found this post helpful.
Old 09-12-2011, 02:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 06:44 AM   #5
Fracker
Member
 
Registered: Mar 2009
Posts: 86

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:23 PM


All times are GMT -5. The time now is 01:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration