-1

So far I have:

cat FileName | tr -s '[a-z]' '[A-Z]'

but I cannot figure out how to remove a string, in my case between 4 and 8 characters long. I think I need to use sed but I only see ways of removing particular strings, or the beginning or the end of a string - not strings between X and Y length.

Does anyone know how to accomplish this?

1 Answers1

1

You indeed can use sed:

$ cat test.txt 
aaa
bbbb
ccccc
dd
$ sed '/^.\{4\}$/d' test.txt 
aaa
ccccc
dd

^ means the start of string, $ means the end of the string. .\{4\} means a character (.) repeated four times (\{4\}). The d at the end deletes lines if the regexp is matched.

Use sed -i '/^.\{4\}$/d' test.txt to apply the changes to file instead of echoing it to stdout.

For a range from 4 to 8 characters, use:

$ sed '/^.\{4,8\}$/d' test.txt 
aaa
dd

To capitalize it:

 $ sed '/^.\{4,8\}$/d; s/./\u&/g' test.txt 
AAA
DD

Resources:
[1] My brain
[2] Capitalize strings in sed or awk

Bayou
  • 3,293
  • 1
  • 9
  • 22