Replacing fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing fields
# 1  
Old 07-24-2010
Replacing fields

Hi!

I have a file somefile.txt:
Code:
12, 1, a, b, c, d, e, f
12, 1, a, b, c, d, e, f
17, 51, a, b, c, d, e, f
...

I've made this script to read two fields from a line and output a third:
Code:
cat  somefile.txt | awk -F, '{if ($1 == "12" && $2== "1")
	print "19";
else if ($1 == "17" && $2== "51")
	print "23";
else if ($1 == "71" && $2== "52")
	print "24";
else if ($1 == "12" && $2== "114")
	print "22";
else if ($1 == "71" && $2== "111")
	print "16";
else if ($1 == "17" && $2== "108")
	print "26"; 
else if ($1 == "71" && $2== "112")
	print "18";
else if ($1 == "17" && $2== "117")
	print "29";
else if ($1 == "31" && $2== "1")
	print "35";
else if ($1 == "19" && $2== "107")
	print "79";
else if ($1 == "13" && $2== "0")
	print "39";
else print "99";}'

However what I need to do is replace a field's #4 value with the result of this check for every line in a different file (including all tho other fields as well).

Anyone knows how can I do this?

Thanks.
# 2  
Old 07-24-2010
If I understand the question you want to replace the 4th field if the first 2 field match the patterns, right?
Code:
awk -F, '
$1 == "12" && $2== "1" {$4="19"}
$1 == "17" && $2== "51" {$4="23"}
.
.
$1 == "13" && $2== "0" {$4="39"}
{print}
' OFS="," somefile.txt > newfile

# 3  
Old 07-24-2010
That worked very well, however I need that if none of the checks are met, $4="99". Can this be done?
# 4  
Old 07-24-2010
Something like this?
Code:
awk -F, '
$1 == "12" && $2== "1" {$4="19"; f=1}
$1 == "17" && $2== "51" {$4="23"; f=1}
.
.
$1 == "13" && $2== "0"{$4="39"; f=1}
!f{$4="99"}
{print}
' OFS="," somefile.txt > newfile

# 5  
Old 07-24-2010
It seems to be a very clever solution, however it did not work. $4 does not change its value to "99"...
# 6  
Old 07-24-2010
Sorry, I forgot to reset the flag:
Code:
awk -F, '
$1 == "12" && $2== "1" {$4="19"; f=1}
$1 == "17" && $2== "51" {$4="23"; f=1}
.
.
$1 == "13" && $2== "0" {$4="39"; f=1}
!f{$4="99"}
{print; f=0}
' OFS="," somefile.txt > newfile

# 7  
Old 07-24-2010
Now it works like a charm. Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

3. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

4. Shell Programming and Scripting

Swapping/replacing fields

Hallo Team, I would like to replace filed 4 and 7 with filed 39 how can i achieve this ? -bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u +27113996891,+27113996891,196.35.130.52,828854047,+27873500077 +27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690... (2 Replies)
Discussion started by: kekanap
2 Replies

5. Shell Programming and Scripting

Replacing certain fields from certain rows

Hi all, say for example i have the next input file 30 Au 7.500000 7.500000 5.774000 Au 7.500000 8.995000 8.363000 Au 7.500000 6.005000 8.363000 Au 20.633000 7.500000 9.226000 Au 20.632000 6.005000 6.637000 Au 20.632000 ... (4 Replies)
Discussion started by: ezitoc
4 Replies

6. Shell Programming and Scripting

replacing a quote in some lines with multiple quote fields

i want to replace mistaken quotes in line starting with tag 300 and relocate the quote in the correct position so the input is 223;25 224;20100428064823;1;0;0;0;0;0;0;0;8;1;3;9697;18744;;;;;;;;;;;; 300;X;Event:... (3 Replies)
Discussion started by: wradwan
3 Replies

7. Shell Programming and Scripting

Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id. file 1: 123|myadr@abc.com 456|myadr2@abc.com 789|myadr3@abc.com file 2: 456|adr456@xyz.com Where the record appears in the second... (3 Replies)
Discussion started by: tltroy
3 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

10. Shell Programming and Scripting

Specifying and replacing fields with awk

#cat BATCH007.TXT 01,661060052,061000104,081118,0915,07,80,1,2/ 99,,,2/ I have this file called BATCH007.TXT. I am trying to change fields 2 and 3 on line 2 to have zeroes. Like this: 01,661060052,061000104,081118,0915,07,80,1,2/ 99,0,0,2/ I can use these commands to print identify the... (2 Replies)
Discussion started by: ddurden7
2 Replies
Login or Register to Ask a Question