0

I need to grep value of ErrCode, ErrAttkey and ErrDesc from the below Input file. and need to display as below in another file

How can i do this using shell script?

Required output

ErrCode|ErrAtkey|ErrDesc
003010|A3|The Unique Record IDalreadyExists 
008024|A8|Prepaid / Postpaid not specified

Input File

<TariffRecords><Tariff><UniqueID>TT07PMST0088</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMST0086</UniqueID><SubStat>Success</SubStat><ErrCode>000000</ErrCode><ErrAttKey></ErrAttKey><ErrDesc>SUCCESS</ErrDesc></Tariff><Tariff><UniqueID>TT07PMCM0048</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMCM0049</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMPV0188</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMTP0060</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMVS0072</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMPO0073</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMPO0073</UniqueID><SubStat>Failure</SubStat><ErrCode>008024</ErrCode><ErrAttKey>A8</ErrAttKey><ErrDesc>Prepaid' / Postpaid not 'specified</ErrDesc></Tariff><Tariff><UniqueID>TT07PMSK0005</UniqueID><SubStat>Failure</SubStat><ErrCode>003010</ErrCode><ErrAttKey>A3</ErrAttKey><ErrDesc>The' Unique Record ID already 'Exists</ErrDesc></Tariff><Tariff><UniqueID>TT07PMSK0005</UniqueID><SubStat>Failure</SubStat><ErrCode>005020</ErrCode><ErrAttKey>A5</ErrAttKey><ErrDesc>Invalid' LSA 'Name</ErrDesc></Tariff><Tariff><UniqueID>TT07PMSK0005</UniqueID><SubStat>Failure</SubStat><ErrCode>008024</ErrCode><ErrAttKey>A8</ErrAttKey><ErrDesc>Prepaid' / Postpaid not 'specified</ErrDesc></Tariff><Tariff><UniqueID>TT07PMSK0005</UniqueID><SubStat>Failure</SubStat><ErrCode>015038</ErrCode><ErrAttKey>A15</ErrAttKey><ErrDesc>Regular' / Promotional is 'compulsory</ErrDesc></Tariff><Tariff><UniqueID>TT07PMSK0005</UniqueID><SubStat>Failure</SubStat><ErrCode>018048</ErrCode><ErrAttKey>A18</ErrAttKey><ErrDesc>Special' Eligibility Conditions cannot be left blank. If no conditions, please enter '`NIL`</ErrDesc></Tariff><Tariff><UniqueID>TT07PMTP0080</UniqueID><SubStat>Success</SubStat><ErrCode>000000</ErrCode><ErrAttKey></ErrAttKey><ErrDesc>SUCCESS</ErrDesc></Tariff></TariffRecords>
5
  • I believe your Input sample is not on single line, could you please correct it in your post once, if this is the case. Commented Jun 23, 2018 at 12:56
  • also what will happen to other values or it is only a sample of output? Commented Jun 23, 2018 at 13:28
  • Please avoid "Give me the codez: questions that have been asked and answered so many times you have to make an effort to avoid finding an answer. Also see How much research effort is expected of Stack Overflow users? Commented Jun 23, 2018 at 15:54
  • 1
    It is very discouraging that you always down vote all the answers to be honest :( there are some people who have to start some time learning coding and look forward to us to help or guide them, so we are trying to do here. I would like to request you to think from their perspective also. Commented Jun 30, 2018 at 6:01
  • 1
    Agreed so I upvoted them all again to compensate. Commented Jul 1, 2018 at 13:10

2 Answers 2

1

EDIT: As per OP all results should be shown even they are coming multiple times in Input_file so in that case following may help.

awk '{gsub(/></,">"RS"<")} 1' Input_file | 
awk -F"[><]" -v time="$(date +%r)"  -v date="$(date +%d/%m/%Y)" '
/ErrCode/||/ErrAttKey/||/ErrDesc/{
  val=val?val OFS $3:$3
}
/<\/Tariff>/{
  print val,date,time,FILENAME;
  val=""
}' OFS="|" 

I am surprised that you are saying that all lines are actually a single line. So in case you want to change them into multiple lines(which actually should be the case then do following in single awk).

awk '{gsub(/></,">"RS"<")} 1' Input_file > temp_file && mv temp_file Input_file
awk -F"[><]" '/ErrCode/{value=$3;a[value]++}  a[value]==1 && NF>3 &&(/ErrCode/||/ErrAttKey/||/ErrDesc/){val=val?val OFS $3:$3} /<\/Tariff>/{if(val && val ~ /^[0-9]/){print val};val=""}'  Input_file

In case you don't want to change your Input_file into multiple lines pattern then run these 2 commands with pipe as follows.

awk '{gsub(/></,">"RS"<")} 1' Input_file | 
awk -F"[><]" '
/ErrCode/{
  value=$3;
  a[value]++
}
a[value]==1 && NF>3 && (/ErrCode/||/ErrAttKey/||/ErrDesc/){
  val=val?val OFS $3:$3
}
/<\/Tariff>/{
  if(val && val ~ /^[0-9]/){
    print val};
  val=""
}'

NOTE: 2 points to be noted here, 1st: If anywhere tag's ErrCode value is null or not starting from digits then that tag's values will not be printed. 2nd point is it will not print any duplicate of values of ErrCode tag.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @RavinderSingh13... I have used your code, but it is providing errcode only once though in file same error code is present multiple times.
Hi @RavinderSingh13..could you please also add system date and time in fourth column in output file as well and input filename in fifth column
@as7951, not sure where is system date and time in there? Also try to encourage people by up-vote them(whoever trying to help you), please keep your requirements same don't change or add things to them repeatedly, see my EDIT code and let me know then?
@RavinderSingh13..have checked your code..getting all values but without any delimiter and also request if you can append system date and time in fourth column and filename in fifth column in input file( i know date and time is not present in input file so want the date and time at which script is executed)...will do up-vote..thank you
Yes it helped me..just one more thing..would be great if in fifth column can get the input filename as well..trying with FILENAME...but not helping
|
1

Assuming the content of your xml is in a file file.txt, the following will work :

echo "ErrCode|ErrAtkey|ErrDesc" && cat file.txt | sed 's/<Tariff>/\n/g' | sed 's/.*<ErrCode>//g;s/<.*<ErrAttKey>/|/g;s/<.*<ErrDesc>/|/g;s/<.*//g' | grep -v '^$'

3 Comments

@as7951, is your Input_file it really having only 1 line all the things?
@RavinderSingh13 - yes one line all the things
@as7951, not sure but this answer is not giving me answer what you have posted to get, not at all.

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.