How to skip lines which don't begin with a number

Hi,

I have a file:
file.txt

1 word
2 word
word
word
3 word
4 word

and I would like to create a set:
set number = `cut -d" " -f1 ${1}` #${1} is the text file

but it should only contain the lines which begin with numbers,
and another set which contains the lines which begin with words.

How can I do that?

Thanks,
Shira. :slight_smile:

awk '$1 ~ /^[0-9]+$/ {print >> "num"; next} {print >> "alpha"}' "$1"

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

Regards,
alister

This might also work for you:

set number = `cut -d" " -f1 ${1} | grep "[0-9]"`
set word = `cut -d" " -f1 ${1} | grep -v "[0-9]"`
set number = `awk '/^[0-9]/ {print $1}' ${1}`
set word = `awk '/^[^0-9]/ {print $1}' ${1}`

As you are apparently still learning, my best advice would be to stop using csh! It's truly dreadful.

The consensus here is that there are better ones!

Hi,

thank you all!

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\)

Thanks,
Shira.

Hi.

The solution as given works fine - if run in isolation. You are obviously doing something else, somewhere that is breaking it.

Please post your script from the point where "number" and "word" are declared up to the point where those values are echo'd.

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?

Thanks,
Shira. :slight_smile:

The only thing that is STRANGE, is that you said in your other post that it was strange, right after you fixed that too!

Standard-issue fodder for CSH users:
Csh Programming Considered Harmful
Top Ten Reasons not to use the C shell

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.

try this ,

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$