3

I have a text file with the following information:

cat test.txt
a,e,c,d,e,f,g,h
d,A,e,f,g,h

I wish to iterate through each line and then for each line print the index of all the characters different from e. So the ideal output would be either with a tab seperator or comma seperator

1 3 4 6 7 8
1 2 4 5 6

or

1,3,4,6,7,8
1,2,4,5,6

I have managed to iterate through each line and print the index, but the results are printed to the same line and not seperated.

while read line;do echo "$line" | awk -F, -v ORS=' ' '{for(i=1;i<=NF;i++) if($i!="e") {print i}}' ;done<test.txt

With the result being

1 3 4 6 7 8 1 2 4 5 6

If I do it only using awk awk -F, -v ORS=' ' '{for(i=1;i<=NF;i++) if($i!="e") {print i}}'

I get the same output.

Could anyone help me with this specific issue with seperating the lines?

7
  • Thank you for showing your efforts in your question(keep it up). Could you please also mention what is the logic of getting your expected output? That will make your question more clear, thank you. Commented Apr 16, 2021 at 12:38
  • i have two text files and i need to extract specific columns for each line in the second file using the index of the columns for each line not matching a pattern for my first file. So this is to make a clear example of the issue. Does this make it more clear? Commented Apr 16, 2021 at 12:41
  • 3
    There's no need for the while loop. awk will iterate for you. Commented Apr 16, 2021 at 12:41
  • Could you please elaborate? how do i get awk to print it for each line? Commented Apr 16, 2021 at 12:42
  • The simplest way with your current logic is to add echo "" after awk command. Commented Apr 16, 2021 at 12:43

1 Answer 1

4

If you don't mind some trailing whitespace, you can just do:

while read line;do echo "$line" | awk -F, '{for(i=1;i<=NF;i++) if($i!="e") {printf i " "}; print ""}' ;done<test.txt

but it would be more typical to omit the while loop and do:

awk -F, '{for(i=1;i<=NF;i++) if($i!="e") {printf i " "}; print ""}' <test.txt

You can avoid the trailing whitespace with the slightly cryptic:

awk -F, '{m=0; for(i=1;i<=NF;i++) if($i!="e") {printf "%c%d", m++ ? " " : "", i }; print ""}' <test.txt
4
  • 1
    The input redirection is not really needed, right? You can just pass the file name as the last argument for awk.
    – accdias
    Commented Apr 16, 2021 at 13:01
  • 3
    @accdias correct. The pro to using input redirection is that if you're also redirecting output to a file, e.g. awk 'script' <input >output then if the input can't be opened the output won't be created/emptied. The con is that you do not have the name of the input file in the FILENAME variable. IMHO the pro isn't generally worth the con for 1 input file and you can't use it in the common case where you have multiple input files so it's not worth using unless you have a very specific need to do so such as you absolutely must not zap the output file if the single input file can't be opened.
    – Ed Morton
    Commented Apr 16, 2021 at 13:31
  • 1
    @accdias You are correct, there is no need for the redirection and I probably should not have retained it. That was just a cut-n-paste from the original while loop. Commented Apr 16, 2021 at 13:44
  • FWIW I'd use awk 'BEGIN{FS=OFS=","} {out=""; for (i=1; i<=NF; i++) if ($i!="e") out=(out ? out OFS : "") i; print out}' file for something like this but there's lots of options!
    – Ed Morton
    Commented Apr 16, 2021 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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