How to replace value for a given condition in specific column of file
8,683
You can do it with awk like
awk -F\| 'BEGIN {OFS=FS} $6 == "A" {$7 = "0800000"} $6 == "I" {$7 = "0758000"}; 1' file1.txt
This will have awk
split fields based on |
, then set the output field separator to also be |
when we write the lines back out. Then if the sixth field, $6
, is A
replace the seventh field with a particular value, and a different value if it's an I. Then print the line out at the end, with our changes if we made any.
Related videos on Youtube
Author by
Miguel Angel
Updated on September 18, 2022Comments
-
Miguel Angel over 1 year
I have file separetd by pipe
|
and I want that when column 6 contain letterI
print0758000
in column 7 and when the column 6 contain letterA
print0800000
in column 7, I can not find how to do it!!!Example:
Original file
cat file1.txt Z89|EEE333333|100001|JANMC84|19990101|I|1800040 Z89|EEE444444|200001|JANMC84|19990101|I|1800040 Z89|EEE222222|300001|JANMC84|19990101|A|1800040 Z89|EEE555555|700001|JANMC84|19990101|A|1800040
The result should be:
Z89|EEE333333|100001|JANMC84|19990101|I|0758000 Z89|EEE444444|200001|JANMC84|19990101|I|0758000 Z89|EEE222222|300001|JANMC84|19990101|A|0800000 Z89|EEE555555|700001|JANMC84|19990101|A|0800000