0

My file looks looks like this:

1000074493 1D # # # # #
1000098165 1D # # # # #
1000105360 1D # # # # #
1000115763 1D 2D # # # #
1000345208 1D # # # # #
1000470774 1D 2D # 4D # #
1000487544 # # 3D # 5D #
1000499657 1D # # # # #
1000531456 1D # # # # #
1000561333 # # # # 5D #

I want to loop per record through fields 2:NF print if $NF != # and stop reading the line but continue in next line.

In other words, find the first field after the first which isn't #, then print only the first field and that field, and skip to the next line.

So the expected result would be:

1000074493 1D
1000098165 1D
1000105360 1D
1000115763 1D
1000345208 1D
1000470774 1D
1000487544 3D
1000499657 1D
1000531456 1D
1000561333 5D

My code is:

awk '{for(i=2; i<=NF; i++) {if($i != "#" ) print $1,$i }}' $FILE

which gives me:

1000074493 1D
1000098165 1D
1000105360 1D
1000115763 1D
1000115763 1D
1000345208 1D
1000470774 1D
1000470774 2D
1000470774 4D
1000487544 3D 
1000487544 5D
1000499657 1D
1000531456 1D
1000561333 5D

What do I need to change?

Braiam
  • 1
  • 11
  • 47
  • 78
  • see: [awk get the nextline](https://stackoverflow.com/questions/44575730/awk-get-the-nextline), or in the docs: https://www.gnu.org/software/gawk/manual/gawk.html#Next-Statement – Luuk Sep 25 '21 at 09:10

4 Answers4

2

Like your original question articulation should already have suggested, the keyword you are looking for is break.

awk '{for(i=2; i<=NF; i++) if($i != "#" ) { print $1,$i; break }}' "$FILE"

Demo: https://ideone.com/hWRM9K

As an aside, avoid useless uses of cat and use lower case for your private variables, and quote file name variables.

tripleee
  • 175,061
  • 34
  • 275
  • 318
2

With awk you can do this:

awk '{gsub(/#/,""); print $1,$2}'  file
1000074493 1D
1000098165 1D
1000105360 1D
1000115763 1D
1000345208 1D
1000470774 1D
1000487544 3D
1000499657 1D
1000531456 1D
1000561333 5D
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Carlos Pascual
  • 1,106
  • 1
  • 5
  • 8
0

the following applied to your file gave me your expected result

awk '{i=2; while ($i == "#") i++; print $1 " " $i}' $FILE
rossifr
  • 96
  • 1
  • 3
-1

Found out finally by myself:

awk '{for(i=2; i<=NF; i++) {if($i != "#" )  print $1,$i }}' $FILE|awk '$1 != p {print $1,$2}{p=$1}'

if anyone knows how to combine both awk statements in one, it would be appreciated!