Guys i have one file and i want the o/p from that file like below
This is my sample file and i want below things from it.
Name:
Sate just below the name:
And retention Level
Name: MEANS_SLP_DAILY
State: inactive
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Name: MEANS_SLP_WEEKLY
State: inactive
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Name: SLP_Irv_Arlington_Test
State: active
Retention Level: 1 (2 weeks)
State: active
Retention Level: 1 (2 weeks)
State: active
Retention Level: 1 (2 weeks)
State: active
I have tried this code for the above file but here i am not getting retention level. I also want to grep retention level. Please suggest
cat /tmp/1 | grep 'Name' -A1
Name: MEANS_SLP_DAILY
State: inactive
--
Name: MEANS_SLP_WEEKLY
State: inactive
--
Name: SLP_Irv_Arlington_Test
State: active
I want my final o/p look like below
Name: MEANS_SLP_DAILY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Name: MEANS_SLP_Weekly State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Hello nirjhar17,
Please use code tags in spite of icode tags for Input_file.
Could you please try following and let me know if this helps you.
awk '($0 ~ /^Name:/ && NR !=1){print A;A="";} {A=A?A OFS $0:$0}' OFS="\t" Input_file
Output will be as follows.
Name: MEANS_SLP_DAILY State: inactive Retention Level: 3 (1 month) State: active Retention Level: 3 (1 month) State: active Retention Level: 3 (1 month) State: active
Name: MEANS_SLP_WEEKLY State: inactive Retention Level: 3 (1 month) State: active Retention Level: 3 (1 month) State: active Retention Level: 3 (1 month) State: active
Thanks,
R. Singh
Thanks for your help! I am trying on Cygwin and i am not getting any output from the code provided. Also, if you look in your output you are also receiving "State" after Retention Level, But my requirement is i want to take "State" only after the Name.
E.g
Name :- MEANS_SLP_DAILY
State :- inactive " i want this one"
Retention Level: 3 (1 month)
State: active "no need"
nirjhar17:
Thanks for your help! I am trying on Cygwin and i am not getting any output from the code provided. Also, if you look in your output you are also receiving "State" after Retention Level, But my requirement is i want to take "State" only after the Name.
E.g
Name :- MEANS_SLP_DAILY
State :- inactive " i want this one"
Retention Level: 3 (1 month)
State: active "no need"
Hello nirjhar17,
I don't have cygwin o.s with me so couldn't test it, that is why it is recomended to let us know all the terms and condition into your very first post along with your O.S name in which you are expecting the code to work. Could you please try following and let me know how it goes then.
awk '($0 ~ /^Name:/ && NR !=1){print A;A="";} {if((q ~ /^Name:/ && $0 ~ /^State:/) || ($0 !~ /^State:/)){A=A?A OFS $0:$0}};{q=$0}' OFS="\t" Input_file
Output will be as follows.
Name: MEANS_SLP_DAILY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Name: MEANS_SLP_WEEKLY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Thanks,
R. Singh
I have tried in linux too, but this code is not giving me any output.
[root@ip-172-31-33-211 ec2-user]# cat /tmp/1 | awk '($0 ~ /^Name:/ && NR !=1){print A;A="";} {if((q ~ /^Name:/ && $0 ~ /^State:/) || ($0 !~ /State:/)){A=A?A OFS $0:$0}};{q=$0}' OFS="\t"
[root@ip-172-31-33-211 ec2-user]#
Hello nirjhar17,
You need not to use cat alnog with awk , awk could read the Input_file itself. It is working for me fine in BASH, following is the Input_file.
cat Input_file
Name: MEANS_SLP_DAILY
State: inactive
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Name: MEANS_SLP_WEEKLY
State: inactive
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Retention Level: 3 (1 month)
State: active
Name: SLP_Irv_Arlington_Test
State: active
Retention Level: 1 (2 weeks)
State: active
Retention Level: 1 (2 weeks)
State: active
Retention Level: 1 (2 weeks)
State: active
Then following is the code for same.
awk '($0 ~ /^Name:/ && NR !=1){print A;A="";} {if((q ~ /^Name:/ && $0 ~ /^State:/) || ($0 !~ /State:/)){A=A?A OFS $0:$0}};{q=$0}' OFS="\t" Input_file
Output will be as follows.
Name: MEANS_SLP_DAILY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Name: MEANS_SLP_WEEKLY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Thanks,
R. Singh
RudiC
July 30, 2016, 5:25am
7
Your spec is far from clear; this is based on what I read from it (print the "state" line immediately AFTER "Name" line only, then print "Retention" lines up to next "Name" line. Not sure if you want "inactive" states only?):
awk '
{sub (/^ */, _)
}
/Name/ {printf "%s%s", TRS, $0
TRS = RS
NM = 1
next
}
NM {printf "\t%s", $0
NM = 0
next
}
/Ret/ {printf "\t%s", $0
}
END {printf RS
}
' file
Name: MEANS_SLP_DAILY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Name: MEANS_SLP_WEEKLY State: inactive Retention Level: 3 (1 month) Retention Level: 3 (1 month) Retention Level: 3 (1 month)
Name: SLP_Irv_Arlington_Test State: active Retention Level: 1 (2 weeks) Retention Level: 1 (2 weeks) Retention Level: 1 (2 weeks)