2

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.

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

6 Comments

@TomFenech. I was just to post a comment about FS/OFS my self. You should not change a code for a user, but post a comment, and the user could then change it if i like to do it.
@Jotne for a major edit I agree, although in this case the changes are largely cosmetic. You or the OP are welcome to roll back the edit if you disagree with it.
@Jotne it is completely fine to edit someone's answer if it improves it like now. See stackoverflow.com/help/editing
@TomFenech I do agree with you in some case, but posting a comment is better, since then the poster may learn some and he/she may remove some bad habits.
No problem. @Jotne I appreciate that it's a grey area. I tried to leave a helpful edit message for that reason :)
|
1

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

Comments

0
sed 's/^\([0-9]*|\)\{3\}\)20141030/\120141029/' YourFile

Using sed (posix version so us --posix on GNU sed especially with the | inside)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.