4

I have a file MDD.TXT with following records. file : MDD.TXT

0|ABC|055309|20120929053309|11965150   
0|ABC|061434|20120929063434|11155537  
0|ABC|070228|20120929073228|11965150  
0|ABC|082213|20120929583213|11965150  
0|ABC|083950|20120929983950|11965150  
0|ABC|084112|20120929083112|11965150

Now I want to replace the second column with "XYZ" and want to add two column filename and "success" in all record. the desired output should be following:

0|XYZ|055309|20120929053309|11965150|MDD.TXT|SUCCESS
0|XYZ|061434|20120929063434|11155537|MDD.TXT|SUCCESS
0|XYZ|070228|20120929073228|11965150|MDD.TXT|SUCCESS
0|XYZ|082213|20120929583213|11965150|MDD.TXT|SUCCESS
0|XYZ|083950|20120929983950|11965150|MDD.TXT|SUCCESS
0|XYZ|084112|20120929083112|11965150|MDD.TXT|SUCCESS
4

You can try following awk:

awk 'BEGIN { FS = OFS = "|" } { $2 = "XYZ"; $6 = ARGV[1]; $7 = "SUCCESS";} 1' MDD.TXT

or:

awk 'BEGIN { FS = OFS = "|" } { $2 = "XYZ"; print $0 OFS ARGV[1] OFS "SUCCESS"; }' MDD.TXT
| improve this answer | |
6

You can use sed as well

$ sed -e 's/\bABC\b/XYZ/g' -e 's/$/|MDD.TXT|SUCCESS/g' MDD.TXT

To make the modification to the file directly

$ sed -i -e 's/\bABC\b/XYZ/g' -e 's/$/|MDD.TXT|SUCCESS/g' MDD.TXT

To keep the original file and redirect the wanted output to other

$ sed -e 's/\bABC\b/XYZ/g' -e 's/$/|MDD.TXT|SUCCESS/g' MDD.TXT >> MDD_MODIFFIED.TXT
| improve this answer | |
  • Why are you escaping pipes when you are using BRE ? – 123 Sep 23 '15 at 15:24
  • @User112638726 Thanks for the observation – tachomi Sep 23 '15 at 15:27
4

With awk:

awk 'BEGIN{OFS=FS="|"} {$2="XYZ"; $0=$0"|"FILENAME"|SUCCESS"}1' mdd.txt

Where:

  • The BEGIN{} is executed only at the beginning, not for every line.
  • OFS=FS="|" sets the input and output field separator to |.
  • $2="XYZ" sets the second field to XYZ
  • $0=... appends to the whole line the FILENAME (from awks internal variables) and SUCCESS as a string.
| improve this answer | |
2

This is the same idea as the existing awk answers but a little more concise:

awk -F'|' -vOFS="|" '{$2="XYZ"; print $0,"MDD.TXT","SUCCESS"}' MDD.TXT

To make it add the file name automatically (for any file name), use:

awk -F'|' -vOFS="|" '{$2="XYZ"; print $0,ARGV[1],"SUCCESS"}' MDD.TXT

The -F sets the input record separator and the -vOFS sets the OFS variable, the output record separator. Then, we set the second field to XYZ and print the line ($0), the file name (ARGV[1]) and SUCCESS.

Note that in your example, you have trailing whitespace on each line, so running any of the proposed solutions will result in:

$ awk -F'|' -vOFS="|" '{$2="XYZ"; print $0,ARGV[1],"SUCCESS"}' MDD.TXT 
0|XYZ|055309|20120929053309|11965150   |MDD.TXT|SUCCESS
0|XYZ|061434|20120929063434|11155537  |MDD.TXT|SUCCESS
0|XYZ|070228|20120929073228|11965150  |MDD.TXT|SUCCESS
0|XYZ|082213|20120929583213|11965150  |MDD.TXT|SUCCESS
0|XYZ|083950|20120929983950|11965150  |MDD.TXT|SUCCESS
0|XYZ|084112|20120929083112|11965150|MDD.TXT|SUCCESS

To avoid that, you could set the input field separator to | or space:

$ awk -F' |' -vOFS="|" '{$2="XYZ"; print $0,ARGV[1],"SUCCESS"}' MDD.TXT 
0|ABC|055309|20120929053309|11965150|XYZ|||MDD.TXT|SUCCESS
0|ABC|061434|20120929063434|11155537|XYZ||MDD.TXT|SUCCESS
0|ABC|070228|20120929073228|11965150|XYZ||MDD.TXT|SUCCESS
0|ABC|082213|20120929583213|11965150|XYZ||MDD.TXT|SUCCESS
0|ABC|083950|20120929983950|11965150|XYZ||MDD.TXT|SUCCESS
0|ABC|084112|20120929083112|11965150|XYZ|MDD.TXT|SUCCESS

Or, you could just remove the trailing whitespace before parsing the file:

sed 's/\s*$//' MDD.TXT | 
    awk -F' |' -vOFS="|" '{$2="XYZ"; print $0,"MDD.TXT|SUCCESS"}'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.