2

I have a homework assignment where I have a file that is a large list of words. I have to copy into a new file all the words starting with c and name it cwords.

I can see a list of the words by doing cat words | grep ^c and I can copy the entire list to a file by doing cp words cwords but what do I type to get just the words starting with c copied over?

jpaugh
  • 329
Parker
  • 21

3 Answers3

0

here is the > or 1> redirection of stdout to a file your are looking for -i option set on grep means ignore case you don't need cat file | grep ... just use grep

there are few different type of redirection to learn to use.

grep yourpattern inputfile > outputfile 

 

-bash-4.4$ cat > cword\? # to have a random file list as input
fdsf
fdsfsd
cdsfdsf
csrezr
rezr
ret

 

-bash-4.4$ grep -i "^c" cword\? > cwords 
-bash-4.4$ cat cwords
cdsfdsf
csrezr
-bash-4.4$ rm cword*
-bash-4.4$ 
jpaugh
  • 329
francois P
  • 1,219
0

To copy the Words beginning with c to new file

command

sed -n '/^c/p' inputfile >outputfile
-1
grep -E '\bc' inputfile > outputfile

I should elaborate -E, --extended-regexp \b is word boundries c is the character you want to match so you're searching through inputfile for all words that start with c you then use > to put the output in outputfile. In your case, you wold name it cwords.