Replace field in the delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace field in the delimited file
# 8  
Old 02-28-2018
Code:
$ awk 'FNR == NR{v[$1]=$2; next} FNR==1 {n=$2=v[$3]+1} FNR==2 || $3=="All_Total" {$2=n}1' FS=: file1 FS=\| OFS=\| file2

Code:
Sale|11|London|3|28022018
Approval|11
Agent1|12|abc|abc|
Agent2|12|abc|abc|
Agent3|12|abc|abc|
Final|11|All_Total

This User Gave Thanks to Aia For This Post:
# 9  
Old 03-01-2018
Hello Aia,RudiC,

Yes this code works find to update file2. Hope can I update the file1 with the increase count ?

E.g. Since we have updated file2 to
Code:
Sale|11|London|3|28022018
 Approval|11
 Agent1|12|abc|abc|
 Agent2|12|abc|abc|
 Agent3|12|abc|abc|
 Final|11|All_Total

File1 should be increment to 11 as well.

e.g.
Code:
London:11

Can somebody suggest quick way?
# 10  
Old 03-02-2018
Try
Code:
awk '
NR == FNR       {TR[$1] = $2
                 SQ[NR] = $1
                 next
                }
FNR == 1        {CITY = $3
                 for (n=split(PLINES, TMP); n; n--) LINES[TMP[n]]
                 TR[CITY]++
                 while (SQ[++n]) print SQ[n] ":" TR[SQ[n]] > "TEMPFILE"
                }

$1 == "Final" ||
FNR in LINES    {$2 = TR[CITY]
                }
1
' FS=: file1 FS=\| OFS=\| PLINES="1|2" file2

and then rename TEMPFILE to file1.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 03-03-2018
Can the above code from RudiC be modified to cater to below file2 as well?

file1:
Code:
LONDON:5500
NEWYORK:4500
AMSTERDAM:3500

file2:
Code:
"MKTG",LONDON,"EXPENSE",20160606,20160606,5000
"EMPLOYEE1",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE2",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE3",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE4",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE5",XXXX,"XXX","XXX"," ,"D","",1000
"FINAL",4,TOTAL_EXPENSE,YYYYYYYYY

I want to do the same thing.
1. Read "LONDON" from file 2 i.e. second field. The field separator is comman (,) instead of pipe.
2.Read file1 to find the count for LONDON i.e. 5500.
3. Update 6th field in the file2 i.e. 5000 to 5501
4. Update the file1 with 5501 (i.e. increament the counter by one)

Contrary to pervious file which has pipe delimiter and also i needed to update three rows.. here i need to update only single row i.e. the first row in the file.

Thanks
# 12  
Old 03-03-2018
You've changed your requirements several times and not have shown us that you have made any attempt at all to modify code that you've been given to meet your newly added requirements.

Why don't you try making some of these changes on your own, show us what you have tried, and let us know if you get stuck getting the output you want.

We want to help you learn how to do things like this on your own. If you're given code that you don't understand, ask the person who gave it to you to explain how it works. We are not here to act as your unpaid programming staff.
# 13  
Old 03-03-2018
Not at all Don. Here is the code... i am trying to execute. The requirement is same just different fields and row numbers.

Getting the city : LONDON

Code:
var1=`awk 'FNR=NR {print $2}' FS=, file2 | head -1`

Getting: the counter
Code:
var2=`grep $var1 file1 | awk '{print $2}' FS=:`

increment counter y 1
Code:
var3 = $((var2+1))

So now.. i have the inceased counter, and also the city for which I need to replace the counter.

I can replace the increased counter file 1 by using below:

Code:
awk -F\| '{gsub($var1':'$var2,$var1':'$var3,$1);}1' OFS=\: file1



But how can I replace the value in file2? Now give that RudiC solution is more efficient and neat.... i chose to use that. But If I know how to replace in file1 (field 6).. i can use mine too as I have got most of the value in variables now.

# 14  
Old 03-03-2018
Wouldn't it have been nice to see your attempt with awk?

Howsoever, try
Code:
awk '
NR == FNR       {TR[$1] = $2
                 SQ[NR] = $1
                 next
                }
FNR == 1        {CITY = $2
                 $6 = ++TR[CITY]
                 while (SQ[++n]) print SQ[n] ":" TR[SQ[n]] > "TEMPFILE"
                }
1
' FS=: file1 FS=, OFS=, file2
"MKTG",LONDON,"EXPENSE",20160606,20160606,5501
"EMPLOYEE1",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE2",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE3",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE4",XXXX,"XXX","XXX"," ,"D","",1000
"EMPLOYEE5",XXXX,"XXX","XXX"," ,"D","",1000
"FINAL",4,TOTAL_EXPENSE,YYYYYYYYY
cat TEMPFILE 
LONDON:5501
NEWYORK:4500
AMSTERDAM:3500

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove Last field from a delimited file

Hi, I have a '~' delimited file and i want to remove the last field using awk. Please find the sample records below: 1428128~1~0~1100426~003~50220~005~14~0~194801~11~0~3~14~0~50419052335~0~0820652001~2~00653862 ~0~1~0~00126~1~20000110~20110423~R~ ~0~Z~1662.94~ ~002041~0045~Z~... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

2. UNIX for Dummies Questions & Answers

add new 'date field' in a pipe delimited file

i need to add a new field in a pipe delimited line. the field will be the current date today. aa|a|s|w|1 as|oiy|oiy|oiy|2 given that all lines are uniformed in the number of fields i want it to look like this:\ aa|a|s|w|1|20120126 as|oiy|oiy|oiy|2|20120126 please help :) (3 Replies)
Discussion started by: kokoro
3 Replies

3. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

4. Shell Programming and Scripting

Pad zeroes first field in a Delimited file

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file. awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3 BEFORE "KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT... (3 Replies)
Discussion started by: johnhips
3 Replies

5. Shell Programming and Scripting

sed command to remove the first field from a '|' delimited file

Hi I have a file with fields delimited by |. I need to remove the first field from the file. I tried cut but it just extracts that field. sample.output abc|100|name1 cde|200|name2 efg|300|name3 Output should be sample.output 100|name1 200|name2 300|name3 thanks Var (6 Replies)
Discussion started by: var285
6 Replies

6. Shell Programming and Scripting

replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously. XX YY 03 variable text here XX YY 03 more variable text here XX YY 03 even more variable text here really long setence XX YY 03 variable numbers also appear 03 11. 123 456 XX YY 03 the occasional comma,... (4 Replies)
Discussion started by: ajp7701
4 Replies

7. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

8. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

9. Shell Programming and Scripting

Count field frequency in a '|' delimited file

I have a large file with fields delimited by '|', and I want to run some analysis on it. What I want to do is count how many times each field is populated, or list the frequency of population for each field. I am in a Sun OS environment. Thanks, - CB (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

10. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies
Login or Register to Ask a Question