Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 585
  • Last Modified:

Awk (or Sed) to show current field number from the Field Seperator

How can I awk (or sed) something to do a search and print out the Field NUMBER (position in the line) when it finds a match?

Example File has 3 lines:

This is - a test file.
This is a - test file.
This is a test - file.

Objective:
Find the positions of the "-"

Desired Outcome:
3
4
5

I can grep, awk -F-, and sed to find them (and / or do other things), but thought awk would have had a variable like FS or something that might help out.

Thoughts?
Thank you,
Gene
0
genehackman
Asked:
genehackman
  • 3
1 Solution
 
rockiroadsCommented:
try grep -n

-n prints line number in file
0
 
rockiroadsCommented:
sorry my mistake, just reread your example, ignore what I wrote
0
 
rockiroadsCommented:
have a look at using index in AWK, I think that might be it
hopefully some other expert can help ya cos Im now off home
have a good evening, sorry about my cockup
0
 
ahoffmannCommented:
awk '{for(n=1;n<=NF;n++){if($n=="-")print n}}' file
0
 
genehackmanAuthor Commented:
That worked perfectly !
Thank you,
Gene
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now