Max
Max

Reputation: 107

Reset variable new line awk

I really didn't want to have to waste people's time and ask this, but after 2 hours and running out of my own time I've decided to ask..

Basically I have a file in the format:

word: other words; more words here; last word
word: more words here; last word
(etc) x 100

Where the occurrence of other words/more words can be zero or more.

My aim is to count the amount of times ';' occurs per line, then print it at the beginning of the line.

My logic was, use awk for a regex of ';' and increase a variable each time it occurs, then print the variable.

Currently I have: (btw how do I put my code in like a shaded box...?)

awk ' /;/ {n=n+1} {print n " " $0}' a.txt

However I become undone in that my 'n' is not reset when awk reads in the nextline. How do I reset n, when awk moves onto the next line? I tried setting v=NR and if v!=NR then n = 0, but I couldn't make that work.

I also have one final question, how do I set a variable to be equal to a regex ?

Thanks heaps,

Upvotes: 1

Views: 2112

Answers (1)

paxdiablo
paxdiablo

Reputation: 882206

Far better to waste one minute of my life than a hundred and twenty of yours :-)

You can just use the field separator to do this:

pax> echo 'word: other words; more words here; last word
word: more words here; last word' | awk 'BEGIN {FS=";"}{print NF"> "$0}'

3> word: other words; more words here; last word
2> word: more words here; last word

Basically, set the field separator FS to a semicolon so that the number of fields NF is set as you need. Then print out the NF variable along with your entire line $0.


And you can get code blocks by prefixing each line with four spaces. When you're asking a question, there's an orange question mark at the top right of the box - click on that and some menu-driven formatting help appears.


As to your last question, augmented with your (paraphrased) comment:

I also have one final question, how do I set a variable to be equal to a regex? I wanted to set a variable equal to all the text before the ':' (so the regex was to isolate 'word').

For that, you can just grab a copy of the first field and perform a substitution on it. I've multi-lined the awk commands since they're getting more complex and this makes them easier to read, but you can put it all on one line if you wish:

pax> echo 'xyzzy: other words; more words here; last word
plugh: more words here; last word' | awk '
    BEGIN {FS=";"}
    {   xx = $1;
        gsub (/:.*/, "", xx);
        print NF" ("xx") > "$0
    }'
3 (xyzzy) > word1: other words; more words here; last word
2 (plugh) > word2: more words here; last word

gsub (/:.*/, "", xx) will simply replace the regex :.* (colon followed by anything) with nothing for the variable xx.

Upvotes: 3

Related Questions