how to get first word in unix using grep

May 11, 2011 at 21:01:18
Specs: Windows 7
i need to get first word from the first line in a file
which contain multiple lines

See More: how to get first word in unix using grep

May 12, 2011 at 01:49:02
1. Is it a text file?
2. Is the word at the beginning of the line?

Report •

May 12, 2011 at 04:06:44
the file is a txt file and i need to ge the first word from the first line
for eg
hello world
god is great

i want hello
by using
1)grep cmd individualy
2)cut cmd individually
3)combining head and awk commands

Report •

May 15, 2011 at 16:44:34
Why do you want to use grep? The grep command is a regular expression search tool. Even using the ^ in your search pattern, you'll still need another filter since grep prints the lines that contain the pattern.



awk -v Field=$WordNumber '{if (NF>=Field) {print $Field} } ' "$File"

With an input file that contained the lines:

Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
`'Tis some visitor,' I muttered, `tapping at my chamber door -
Only this, and nothing more.'

This script will output:


Report •

Related Solutions

May 15, 2011 at 20:13:35
fine got it thanks

Report •

Ask Question