How to use sed or grep to find values in any column of a row? [closed]

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file with 8 columns and I want to find and show any row that have 2 values in any order but only from an specific column.
It's like I want to print the row that contains 16 and 19 from the 3rd column to the end.
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
And the return should be:
02 03 04 16 19 28 29 49 - r1
03 04 16 17 18 19 27 43 - r4
awk sed gawk
closed as unclear what you're asking by G-Man, tink, Stephen Harris, Thomas, RalfFriedl Dec 2 at 10:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
I have a file with 8 columns and I want to find and show any row that have 2 values in any order but only from an specific column.
It's like I want to print the row that contains 16 and 19 from the 3rd column to the end.
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
And the return should be:
02 03 04 16 19 28 29 49 - r1
03 04 16 17 18 19 27 43 - r4
awk sed gawk
closed as unclear what you're asking by G-Man, tink, Stephen Harris, Thomas, RalfFriedl Dec 2 at 10:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file with 8 columns and I want to find and show any row that have 2 values in any order but only from an specific column.
It's like I want to print the row that contains 16 and 19 from the 3rd column to the end.
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
And the return should be:
02 03 04 16 19 28 29 49 - r1
03 04 16 17 18 19 27 43 - r4
awk sed gawk
I have a file with 8 columns and I want to find and show any row that have 2 values in any order but only from an specific column.
It's like I want to print the row that contains 16 and 19 from the 3rd column to the end.
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
And the return should be:
02 03 04 16 19 28 29 49 - r1
03 04 16 17 18 19 27 43 - r4
awk sed gawk
awk sed gawk
edited Dec 2 at 12:03
asked Dec 2 at 2:59
Hector
113
113
closed as unclear what you're asking by G-Man, tink, Stephen Harris, Thomas, RalfFriedl Dec 2 at 10:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by G-Man, tink, Stephen Harris, Thomas, RalfFriedl Dec 2 at 10:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58
add a comment |
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
-2
down vote
ITried for content 16 and it worked fine. Please find the below command and let us know for any input and suggestions
filename
[root@praveen_linux_example ~]# cat filename
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
command
for z in 16 ; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' filename| grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' filename | awk 'for(j=1;j<maxcolumnnumbercount;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
praveen_linux_example ~]# for z in 16; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' o.txt | grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' o.txt | awk 'for(j=1;j<21;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
Content exsists 16 in line number 2 and in column 4
Content exsists 16 in line number 3 and in column 2
Content exsists 16 in line number 4 and in column 1
Content exsists 16 in line number 5 and in column 3
2
In lieu of using 2sed, 2awk, and 1grepinvocations in 2forloops, consider doing it ALL in one script (e.g.awk, orperl). Plus, the result doesn't look like the desired output by far.
– RudiC
Dec 2 at 12:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-2
down vote
ITried for content 16 and it worked fine. Please find the below command and let us know for any input and suggestions
filename
[root@praveen_linux_example ~]# cat filename
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
command
for z in 16 ; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' filename| grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' filename | awk 'for(j=1;j<maxcolumnnumbercount;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
praveen_linux_example ~]# for z in 16; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' o.txt | grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' o.txt | awk 'for(j=1;j<21;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
Content exsists 16 in line number 2 and in column 4
Content exsists 16 in line number 3 and in column 2
Content exsists 16 in line number 4 and in column 1
Content exsists 16 in line number 5 and in column 3
2
In lieu of using 2sed, 2awk, and 1grepinvocations in 2forloops, consider doing it ALL in one script (e.g.awk, orperl). Plus, the result doesn't look like the desired output by far.
– RudiC
Dec 2 at 12:44
add a comment |
up vote
-2
down vote
ITried for content 16 and it worked fine. Please find the below command and let us know for any input and suggestions
filename
[root@praveen_linux_example ~]# cat filename
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
command
for z in 16 ; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' filename| grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' filename | awk 'for(j=1;j<maxcolumnnumbercount;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
praveen_linux_example ~]# for z in 16; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' o.txt | grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' o.txt | awk 'for(j=1;j<21;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
Content exsists 16 in line number 2 and in column 4
Content exsists 16 in line number 3 and in column 2
Content exsists 16 in line number 4 and in column 1
Content exsists 16 in line number 5 and in column 3
2
In lieu of using 2sed, 2awk, and 1grepinvocations in 2forloops, consider doing it ALL in one script (e.g.awk, orperl). Plus, the result doesn't look like the desired output by far.
– RudiC
Dec 2 at 12:44
add a comment |
up vote
-2
down vote
up vote
-2
down vote
ITried for content 16 and it worked fine. Please find the below command and let us know for any input and suggestions
filename
[root@praveen_linux_example ~]# cat filename
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
command
for z in 16 ; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' filename| grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' filename | awk 'for(j=1;j<maxcolumnnumbercount;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
praveen_linux_example ~]# for z in 16; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' o.txt | grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' o.txt | awk 'for(j=1;j<21;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
Content exsists 16 in line number 2 and in column 4
Content exsists 16 in line number 3 and in column 2
Content exsists 16 in line number 4 and in column 1
Content exsists 16 in line number 5 and in column 3
ITried for content 16 and it worked fine. Please find the below command and let us know for any input and suggestions
filename
[root@praveen_linux_example ~]# cat filename
c1 c2 c3 c4 c5 c6 c7 c8
02 03 04 16 19 28 29 49 - r1
01 16 19 22 44 47 49 50 - r2
16 17 19 32 47 50 54 56 - r3
03 04 16 17 18 19 27 43 - r4
command
for z in 16 ; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' filename| grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' filename | awk 'for(j=1;j<maxcolumnnumbercount;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
praveen_linux_example ~]# for z in 16; do for ((i=1;i<maxlinenumbercount;i++)); do sed -n ''$i'p' o.txt | grep -i "$z">/dev/null ; if [[ $? == 0 ]];then echo "Content exsists $z in line number $i and in column"; fi;awk -v i="$i" -v z="$z" 'NR==iprint $0' o.txt | awk 'for(j=1;j<21;j++)if($j ~ /16/)print j';done;done | sed "N;s/n/ /g"
Content exsists 16 in line number 2 and in column 4
Content exsists 16 in line number 3 and in column 2
Content exsists 16 in line number 4 and in column 1
Content exsists 16 in line number 5 and in column 3
answered Dec 2 at 9:50
Praveen Kumar BS
1,162138
1,162138
2
In lieu of using 2sed, 2awk, and 1grepinvocations in 2forloops, consider doing it ALL in one script (e.g.awk, orperl). Plus, the result doesn't look like the desired output by far.
– RudiC
Dec 2 at 12:44
add a comment |
2
In lieu of using 2sed, 2awk, and 1grepinvocations in 2forloops, consider doing it ALL in one script (e.g.awk, orperl). Plus, the result doesn't look like the desired output by far.
– RudiC
Dec 2 at 12:44
2
2
In lieu of using 2
sed, 2 awk, and 1 grep invocations in 2 for loops, consider doing it ALL in one script (e.g. awk, or perl). Plus, the result doesn't look like the desired output by far.– RudiC
Dec 2 at 12:44
In lieu of using 2
sed, 2 awk, and 1 grep invocations in 2 for loops, consider doing it ALL in one script (e.g. awk, or perl). Plus, the result doesn't look like the desired output by far.– RudiC
Dec 2 at 12:44
add a comment |
@tink His output does match what is wanted. It contains the rows with 16 in the 3rd and 4th column.
– Nasir Riley
Dec 2 at 4:31
@tink The desired output is rows 2 and 5. In row 2, 16 is in column 4, not 2. In row 5, 16 is in column 3. Row 3 is not in the desired output so that doesn't matter.
– Nasir Riley
Dec 2 at 5:53
Darn - right you are. Deleting stupid comments. '
– tink
Dec 2 at 5:55
Nice, thanks for your help. Could anyone take down the “on hold” status?
– Hector
Dec 2 at 11:58