Substituting Characters using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substituting Characters using SED
# 1  
Old 04-08-2009
Substituting Characters using SED

Can SED be used to substitute a character (y) with a character (Y) in a specified field?

File has 12000 : delimeted rows as;

HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:Firstname.MI.Lastname@mil:SGT

I wish to replace the capital letters in the 3rd field of the second row with lowercase letters.

Thanks,
JB
# 2  
Old 04-08-2009
Try this:

Code:
awk -F: 'NR==2{$3=tolower($3)}1' OFS=":" file

Regards
# 3  
Old 04-08-2009
Code:
/usr/bin/nawk -F: 'NR==2{$3=tolower($3)}1' OFS=":" file > file2

works like a champ.

I was using
Code:
sed -f scriptfile filetomod > outputfile

to run multiple edits in a single pass and was just wondering if there was an easy way to do the above with sed.

Thanks for you solution. It is a lot simpler than I can come up with and I can always make a second pass with my scriptfile to make the other mods.

JB
# 4  
Old 04-09-2009
I spoke too soon Franklin. Your solution did work like a champ for record #2. I am trying to create a loop using your logic to assure that all records have lowercase in field 3.

Code:
#!/bin/bash
>uncap.txt
i=0
while read line
       do nawk -F: 'NR=="$i"{$3=tolower($3)}1' OFS=":" >> uncap.txt
       let i=$i+1
done < CAP.txt

This deletes the first record, and does not set filed #3 tolower in any record.
Thanks for the help,
JB
# 5  
Old 04-09-2009
In your first post you wished to replace the capital letters in the 3rd field of the second row with lowercase letters if I don't understand the question.
To replace the 3th field in all rows you can use this:

Code:
awk -F: '{$3=tolower($3)}1' OFS=":" CAP.txt > uncap.txt

Regards
# 6  
Old 04-09-2009
My example was not clear enough. I have 12000 rows in my user accounts, and I wish to replace any capitals in the 3rd field of any row with lowercase.

The 3rd field of the output file will be used as a join field with a second file.

Agan, I thank you for you help.

JB
# 7  
Old 04-09-2009
Quote:
Originally Posted by altamaha
My example was not clear enough. I have 12000 rows in my user accounts, and I wish to replace any capitals in the 3rd field of any row with lowercase.

The 3rd field of the output file will be used as a join field with a second file.

Agan, I thank you for you help.

JB
This is the output I get, am I missing something?

Code:
$ cat file
HHC 1 BDE:Lastname, Firstname MI:FIRSTNAME.MI.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:FIRSTNAME.MI.Lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:FIRSTNAME.MI.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:FIRSTNAME.MI.Lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:FIRSTNAME.MI.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:FIRSTNAME.MI.Lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:FIRSTNAME.MI.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:FIRSTNAME.MI.Lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:FIRSTNAME.MI.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:FIRSTNAME.MI.Lastname@mil:SGT
$
$ awk -F: '{$3=tolower($3)}1' OFS=":" file
HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
HHC 2 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT
$

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

getting error while substituting variable using sed..

I am using script for substitute one variable with another variable like below... below code works fine... sed 's/'$sub_fun'/'$To_sub'/g' But when i run the same code from the script getting below errors.. sed: -e expression #1, char 7: unterminated `s' command please help....... (2 Replies)
Discussion started by: pamu
2 Replies

3. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

5. Shell Programming and Scripting

Remove the Characters '[' and ']' with Sed

Hi, I am new to Sed and would like to know if it is possible to remove the characters . I have a couple of files with a keyword and would like to remove the substring. I am Using sed s/// but Its not working Thanks for your Support Andrew Borg (2 Replies)
Discussion started by: andrewborg
2 Replies

6. Shell Programming and Scripting

Substituting a shell variable in sed

Hi guys, I'm trying to figure out how to use a shell variable inside my sed command. I just want to remove a certain part of a path. I've tried three different combinations and none of them work. Here are the three combinations: echo $file | sed 's/'$test'//' echo $file | sed "s/$test//"... (7 Replies)
Discussion started by: chu816
7 Replies

7. Shell Programming and Scripting

issue with substituting using sed

have a fileA containing about 260 lines wherein i have to match 2 lines fileA blah blah OF 90 DAYS DOCS PERIOD 12/06/0" Pairs_Id 52006 Amount1 -300000.0 Amount2 15091500.10 Codifiers_Id 0 OriginalId 0 EOT --blah blah blah TBL Tradt_IN CardRate 0.0 hashAmount -15091500.0... (2 Replies)
Discussion started by: sunnyboy
2 Replies

8. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (1 Reply)
Discussion started by: Shilpi
1 Replies

9. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (2 Replies)
Discussion started by: Shilpi
2 Replies

10. Shell Programming and Scripting

Substituting with value of variable in Sed

Hi, I have a program in which i have to substitute a TAG in a file with the value of a variable. Code Snippet: ---------------- myvar=1234 sed 's/_TAG_/$myvar/' infile outfile When I run this command, the _TAG_ in the "infile" is substituted with "$myvar" but NOT the value "1234"... (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question