0

I have a population file, looks like this:

Pop_ID
good
HA_27
HA_14
HA_1
HA_20
HA_17
HA_26
HA_22
HA_7
HA_7
HA_16
HA_14
HA_2
HA_1
HA_11
HA_3
HA_7
HA_25
HA_23

I want to print the line number of those lines that has a specific character. For instance, "HA_1" where lines 5 and 15 have this character.

5
15
2

With awk.

awk '/\<HA_1\>/{print NR}' infile

With sed.

sed -n '/\<HA_1\>/=' infile
|improve this answer|||||
  • Thanks so much @afshin, one last question. I have another file and I want to extract only those column which their column number matches to the line number of HA_1 that I produced from your file above! How can I do that! Sorry I am R-expert very beginner in Unix! – Anna1364 Oct 6 '17 at 18:18
  • Thanks and you are welcome! Sounds it's easy job, do you have question for that? can you post the link to that? also don't forget to mark one of the answer which helped you and mainly solved your question : ) – αғsнιη Oct 6 '17 at 18:25
  • Note that not all awk or sed implementations support vi's \< \> operators. Those are not POSIX. Even on Solaris (the system from the company co-founded by the author of vi), \< is supported in sed but not awk. – Stéphane Chazelas Nov 8 '17 at 3:15
2

Using sed:

$ sed -n '/^HA_1$/=' file
5
15

The sed expression /^HA_1$/= will apply the = command to all lines matching the regular expression ^HA_1$. The = command will output the current line number, and the regular expression matches any line whose sole content is HA_1.

|improve this answer|||||
1

grep + cut approach:

grep -wn 'HA_1' file | cut -d':' -f1

The output:

5
15
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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