Reputation: 724
Grep pattern and select portion of a line after a matching patterns 41572: 90000: and 90002:
input
hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
Count ch : 41572:47149-47999/2(14485-14910) 41584:47149-47999/2(14911-15449) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418)
drx : 12345
here the code used
awk '
{
flag=""
for(i=1;i<=NF;i++){
if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
flag=1
printf("%s%s",$i,i==NF?ORS:OFS)
}
}
}
!flag
' Input_file
with the code above from Mr. RavinderSingh13, I got the following output
hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
41572:47149-47999/2(14485-14910) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418)
drx : 12345
I need the following output desired
hyt : generation
str : 122344
stks : 9000233
dhy : 9000aaaa
sjyt : hist : hhh9000kkk
Count ch : 41572:47149-47999/2(14485-14910) 90000:47919-47999/2(15447-15477) 90002:47919-47999/2(15478-15418)
drx : 12345
Thanks in advance
Upvotes: 0
Views: 55
Reputation: 133438
EDIT: Adding solution as per OP's new question.
awk '{flag="";for(i=1;i<=NF;i++){if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){flag=1;printf("%s%s",$i,i==NF?ORS:OFS)}}} !flag'
OR
awk '
{
flag=""
for(i=1;i<=NF;i++){
if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
flag=1
printf("%s%s",$i,i==NF?ORS:OFS)
}
}
}
!flag
' Input_file
Could you please try following(though not fully clear going by as per shown sample output only).
awk 'NF>1{for(i=1;i<=NF;i++){if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){printf("%s%s",$i,i==NF?ORS:OFS)}};next} 1' Input_file
Adding a non-one liner form of solution too now.
awk '
NF>1{
for(i=1;i<=NF;i++){
if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){
printf("%s%s",$i,i==NF?ORS:OFS)
}
}
next
}
1
' Input_file
Explanation: Adding explanation for above code too here.
awk '
NF>1{ ##Checking if NF is greater than 1.
for(i=1;i<=NF;i++){ ##Using for loop to go through from value 1 to till value of NF.
if($i ~ /41572/ || $i ~ /90000/ || $i ~ /90002/){ ##Checking if value of fields is either 41572 OR 90000 OR 90002 then do following.
printf("%s%s",$i,i==NF?ORS:OFS) ##Print the field value in case above condition is TRUE with NEW line if i==NF or space if not.
}
}
next ##Next will skip all further statements from here.
}
1 ##1 will print all edited/non-edited lines here.
' Input_file ##Mentioning Input_file name here.
Upvotes: 1