Selective case conversion of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Selective case conversion of a file
# 1  
Old 06-15-2012
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 Smilie
# 2  
Old 06-15-2012
One of your needed functions

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

# 3  
Old 06-15-2012
Hi joyeg ,

Thanks for the reply, but unfortunately it did not work Smilie

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 Smilie
# 4  
Old 06-15-2012
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

This User Gave Thanks to joeyg For This Post:
# 5  
Old 06-15-2012
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 03:04 PM.. Reason: quote tags to code tags
# 6  
Old 06-15-2012
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 03:21 PM..
This User Gave Thanks to neutronscott For This Post:
# 7  
Old 06-16-2012
Hi neutronscott....

Seems like some issue Smilie

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 09:49 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Backup script with selective file types only

i am looking for a backup script to be run in ssh which can move all *.php files only to a archive Eg. a folder has 5 sub folders with different file types, which can be mix of PDF, jpeg, gif etc etc. but the archive generated should only include the *.php files without breaking the directory... (3 Replies)
Discussion started by: netatma
3 Replies

2. Shell Programming and Scripting

Case conversion within a command

Hi All, I need the variable to be set in both uppercase and lowercase in a single command DATABASE=ORCL something like the below which is absolutly wrong find /ora/admin/11.1.0/diag/rdbms/{print tolower($0)}${DATABASE}/{print toupper($0)}${DATABASE} The o/p should be as below:... (4 Replies)
Discussion started by: jjoy
4 Replies

3. Shell Programming and Scripting

How to output selective delimited data to a new file?

I have the following file which at times can be quite large so can be difficult to read, I need a script that just extracts particular delimited data and outputs to alternate file $cat fix1.log FIX4.4|0=12|55=LIT.L|48=123456|32=5|52=20111107-10:52:22.128|38=100|10=200| ... (6 Replies)
Discussion started by: Buddyluv
6 Replies

4. Shell Programming and Scripting

capturing selective data from a vcd file

Hi, This is a vcd file.A vcd file may have 'n' modules. 1) I need to capture the data in bold,i.e. the module names (shown in bold) 2) Also i need to capture the data inside each individual module,say for tst_bench_top ,i need to capture data from line 4 to line 20 ... I just want one... (2 Replies)
Discussion started by: veerabahu
2 Replies

5. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

6. Shell Programming and Scripting

Copy selective lines from text file

Hello, I have a text file which I need to check for presence of certain tags, and then copy a subsequent portion of text into another file. The tag matching canbe done with Grep but I do not know how to copy selective lines from one file to another. Is it possible do that? I checked up some... (8 Replies)
Discussion started by: ajayram
8 Replies

7. Shell Programming and Scripting

Extract selective block from XML file

Hi, There's an xml file produced from a front-end tool as shown below: <INPUT DATABASE ="ORACLE" DBNAME ="UNIX" NAME ="FACT_TABLE" OWNERNAME ="DIPS"> <INPUTFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" NAME ="STORE_KEY" PICTURETEXT ="" PORTTYPE ="INPUT" PRECISION ="15" SCALE... (6 Replies)
Discussion started by: dips_ag
6 Replies

8. UNIX for Dummies Questions & Answers

To compare selective file in different folders

Hello, I am using dircmp -d <folde1> <Folder2> to compare the files from two different foldes, but this command compares for all the files. Is there any option to select only some files for comparision. For example in Folder1: file1.txt file2.txt file3.txt Folder2 file1.txt file2.txt... (0 Replies)
Discussion started by: gmahesh2k
0 Replies

9. UNIX for Dummies Questions & Answers

Selective, recursive file diddling!

Hello, I have been trying all sorts of combinations and been royally screwing up my filesystem in the process :rolleyes: I have a bunch of .wav files in a hierarchical album/artist etc folder structure and each has a duplicate .w4a file next to it. All I want to do is move all the .w4a... (7 Replies)
Discussion started by: dinalt
7 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question