Reputation: 151
Consider the sample below:
BRANCH|TXN_ID|CUSTOMER|PROCESS_DATE|VALUE_DATE|AMOUNT|UPLOAD_DATE|NARRATIVE
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
1|23234|12343|20141030|20141030|2000|20141030|TEST
If I use sed 's/20141030/20141029/g'
, it will replace all instances of 20141030
, including UPLOAD_DATE, which is not what I want.
From the net, the awk examples am getting only replace one instance of the string. I need to replace all instances in one go
So my question is, how can I replace the contents of column 4 and 5 (process date and value date) using a unix script while maintaining the format of the file? The result will be written into a new file.
Upvotes: 2
Views: 349
Reputation:
nu11p01n73Rs answer is definitely the best solution for this specific problem but I though i would add a more general approach that could work for lots of fields.
Just add all the fields you want to change to the start of the split statement.
awk -F"|" -vOFS="|" '{split("4 5",A," ");for(i in A)sub(20141030,"20141029",$A[i])}1' file
Actually this would be more efficient.
awk 'BEGIN{FS=OFS="|";split("4 5",A," ")}{for(i in A)sub(20141030,"20141029",$A[i])}1' file
Upvotes: 1
Reputation: 26667
Using awk
awk 'BEGIN{FS=OFS="|"}{sub(20141030,"20141029",$4); sub(20141030,"20141029",$5); print}' inputFile
Gives output as
BRANCH|TXN_ID|CUSTOMER|PROCESS_DATE|VALUE_DATE|AMOUNT|UPLOAD_DATE|NARRATIVE
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
1|23234|12343|20141029|20141029|2000|20141030|TEST
Upvotes: 4
Reputation: 10039
sed 's/^\([0-9]*|\)\{3\}\)20141030/\120141029/' YourFile
Using sed (posix version so us --posix
on GNU sed especially with the |
inside)
Upvotes: 0