The first "$1" is internal to awk and refers to the first field in the line. The second instance of "$1" is unrelated and is the shell positional parameter containing your file's name.
num=name of output file with numeric first fields
alpha=name of output file with all other lines
2pugs, your message really helped me.
The only thing is that you need to switch between the pipelines.
It should be:
set number = `grep "[0-9]" ${1}|cut -d" " -f1`
set word = `grep -v "[0-9]" ${1} | cut -d" " -f1`
But now I have a different problem.
When I try to do:
echo "${number[$i]} ${word[$i]}"
the number is printed okay, but the string returns 0 instead of the word.
(2pugs, it's the same problem as before, but now I can't avoid it by entering the parameter from the standard input, because now this word is from a file. For everyone else who doesn't know what I'm talking about: http://www.unix.com/shell-programming-scripting/127462-how-compare-parameter-string.html\)
well,
I use word and number in the main script.
in the main script I have an if->while loop, and then I send ${word[$i]} and ${number[$i]} to the subscript.
like that:
if () then
bla bla
while ()
bla bla
subscript ${word[$i]} ${number[$i]}
end
endif
inside the subscript I receive them as
set word = ${1}
set number = ${2}
In the subscript I tried to echo them and they echoed fine.
But inside the subscript there's an IF part.
If I print
echo "word: ${word}"
it works fine.
But if I print
echo "${number} ${word}"
it prints the right number and 0 instead of the word.
---------- Post updated at 04:12 AM ---------- Previous update was at 03:49 AM ----------
OK, I solved it, but it's so STRANGE!!!
what I did was like this:
in the subscript in the IF part, right above the line that prints I reentered the string into word:
if () then
set word = ${1}
echo "${number} ${word}"
endif
and it works.
But the thing is that I didn't touch the word parameter at all before the echo part.
Can someone explain why this happens?
I didn't fix that problem then, I just did some trick.
And in this case, I couldn't do that trick.
You probably missed what I wrote - I wrote that this time the parameter comes from a file and not from the standard input so I can't use that solution here.
Are you by any chance using functions in your script? The only way I can think of an explanation is maybe the positional parameters are changing if you call it through a function. If not then I agree.. it's odd.
gaurav@localhost:~/tem$ awk '/^[0-9]/{num[i++]=$0}!/^[0-9]/{no_num[j++]=$0}END{for(k=0;k<i;k++)print num[k];print "\n";for(k=0;k<j;k++)print no_num[k]}' file.txt
1 word
2 word
3 word
4 word
word
word
gaurav@localhost:~/tem$