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
  • 319
  • 2
  • 13
Parker
  • 21
  • 1
  • 2
    [What are the shell's control and redirection operators?](https://unix.stackexchange.com/q/159513/22142) – don_crissti Jan 24 '18 at 18:40
  • @jpaugh, it's exactly the correct regex, see e.g. [here](https://www.regular-expressions.info/anchors.html) – ilkkachu Jan 24 '18 at 18:47
  • 2
    I don't know what I was thinking! I was thinking of the control character. – jpaugh Jan 24 '18 at 18:49
  • +1 for the honesty that it's homework. My inner middle-schooler would give an additional +1 for the new file's required name. – zr00 Jan 29 '18 at 16:05

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
  • 319
  • 2
  • 13
francois P
  • 1,219
  • 11
  • 27
0

To copy the Words beginning with c to new file

command

sed -n '/^c/p' inputfile >outputfile
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14
-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.