Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-15-2012
Registered User
 
Join Date: Jul 2010
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
Data Selective case conversion of a file

Hi all,

I have a requirement to convert a file from one format to another.

Parent file:
Quote:
Sample: ABCdefgh
domain: when you think abcd@tester.com
domain: abcdefgh@tester2.com
Goods for purchase:234
domain: efgh@anyother.com
Sample: PQRST
Sample: nmjkl
Sample: 123jjkl Mer.
Goods for purchase: No ChaNGE
to

Quote:
Sample: abcdefgh
domain: when you think abcd
domain: abcdefgh
Goods for purchase:234
domain: efgh
Sample: pqrst
Sample: nmjkl
Sample: 123jjkl mer.
Goods for purchase: No ChaNGE
i.e.,
1.) all the lines which begin with "Sample:", should be converted in such a way that entire text after ":" gets converted into all-lower case.
2.) all the lines which begin with "domain:" get stripped off the text mentioned after "@" sign.


Please guide.

Thanks in advance
Sponsored Links
    #2  
Old 06-15-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Location: Out running a Marathon.
Posts: 2,194
Thanks: 48
Thanked 128 Times in 120 Posts
One of your needed functions


Code:
$ echo Sample: ABCdefGHiJ | awk '{if($1=="Sample:") print tolower($_)}'
sample: abcdefghij

Sponsored Links
    #3  
Old 06-15-2012
Registered User
 
Join Date: Jul 2010
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
Hi joyeg ,

Thanks for the reply, but unfortunately it did not work

Quote:
$ echo Sample: ABCdefGHiJ | awk '{if($1=="Sample:") print tolower($_)}'
Sample: ABCdefGHiJ
$ echo $SHELL
/usr/bin/ksh
$ uname
SunOS
Please guide where i am wrong
    #4  
Old 06-15-2012
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Location: Out running a Marathon.
Posts: 2,194
Thanks: 48
Thanked 128 Times in 120 Posts
Confused...


Code:
$ cat sample16.txt
Sample: ABCdefgh
domain: when you think abcd@tester.com
domain: abcdefgh@tester2.com
Goods for purchase:234
domain: efgh@anyother.com
Sample: PQRST
Sample: nmjkl
Sample: 123jjkl Mer.
Goods for purchase: No ChaNGE

$ awk '{if($1=="Sample:") {print tolower($_)} else {print}}' <sample16.txt | sed 's/^sample:/Sample:/'
Sample: abcdefgh
domain: when you think abcd@tester.com
domain: abcdefgh@tester2.com
Goods for purchase:234
domain: efgh@anyother.com
Sample: pqrst
Sample: nmjkl
Sample: 123jjkl mer.
Goods for purchase: No ChaNGE

The Following User Says Thank You to joeyg For This Useful Post:
dipanchandra (06-15-2012)
Sponsored Links
    #5  
Old 06-15-2012
Registered User
 
Join Date: Jul 2010
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
Thanks Joeyg !! It works !!! .. For that @ part, just found out a simple-non surgical solution:


Code:
$  echo  "domain: efgh@anyother.com" | cut -f1 -d"@"
domain: efgh


Last edited by Scrutinizer; 06-15-2012 at 02:04 PM.. Reason: quote tags to code tags
Sponsored Links
    #6  
Old 06-15-2012
neutronscott's Avatar
script kiddie
 
Join Date: Jun 2011
Location: Charleston, SC
Posts: 649
Thanks: 18
Thanked 189 Times in 179 Posts
Assuming no ':' occurs after the first one ...


Code:
$ awk -F: '$1=="Sample"{print $1,tolower($2);next}$1=="domain"&&e=index($2,"@"){print $1,substr($2,1,e-1);next}1' OFS=: input
Sample: abcdefgh
domain: when you think abcd
domain: abcdefgh
Goods for purchase:234
domain: efgh
Sample: pqrst
Sample: nmjkl
Sample: 123jjkl mer.
Goods for purchase: No ChaNGE

Much more general:

Code:
#!/usr/bin/awk -f
$1=="Sample:"{s=$0;sub(/^[^:]*:/,"",s);print $1 tolower(s);next}
$1=="domain:"&&e=index($0,"@"){print substr($0,1,e-1);next}
1

but really depends how formatted the file is to get specific good answer. =\

Last edited by neutronscott; 06-15-2012 at 02:21 PM..
The Following User Says Thank You to neutronscott For This Useful Post:
dipanchandra (06-16-2012)
Sponsored Links
    #7  
Old 06-16-2012
Registered User
 
Join Date: Jul 2010
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
Hi neutronscott....

Seems like some issue

Quote:
$ awk -F: '$1=="Sample"{print $1,tolower($2);next}$1=="domain"&&e=index($2,"@"){print $1,substr($2,1,e-1);next}1' OFS=: smmple16.txt
awk: syntax error near line 1
awk: bailing out near line 1
Quote:
$ uname
SunOS
$ echo $SHELL
/usr/bin/ksh

Last edited by dipanchandra; 06-16-2012 at 08:49 AM..
Sponsored Links
Closed Thread

Tags
case, conversion, text processing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conversion from Upper Case to Lower Case Condition based tsbiju Shell Programming and Scripting 12 07-08-2011 02:53 PM
Extract selective block from XML file dips_ag Shell Programming and Scripting 6 01-04-2011 04:29 AM
To compare selective file in different folders gmahesh2k UNIX for Dummies Questions & Answers 0 05-15-2008 02:03 AM
Selective, recursive file diddling! dinalt UNIX for Dummies Questions & Answers 7 04-07-2005 06:31 PM
lower case to upper case string conversion in shell script dchalavadi UNIX for Dummies Questions & Answers 3 05-29-2002 12:07 AM



All times are GMT -4. The time now is 04:37 AM.