I have a file.txt
fhadja
ksjfskdasd
adasda
sada
s
adasaaa
I need to extract only the words that are 6 character length from there.
EXAMPLE of what i need to obtain as a result:
fhadja
adasda
Thank you.
I have a file.txt
fhadja
ksjfskdasd
adasda
sada
s
adasaaa
I need to extract only the words that are 6 character length from there.
EXAMPLE of what i need to obtain as a result:
fhadja
adasda
Thank you.
You can use:
grep -E '^.{6}$' file
fhadja
adasda
Or using awk:
awk 'length($0)==6' file
fhadja
adasda
Or using sed
:
sed -rn '/^.{6}$/p' file
fhadja
adasda
grep -E '^f.{5}$' file
–
Swann Try this with GNU grep:
grep -E '^.{6}$' file
Output:
fhadja adasda
Just for fun: Another solution in bash
while read -r line
do
if [ ${#line} -eq 6 ]
then
echo $line
fi
done < file.txt
you get,
fhadja adasda
If your file can contain spaces, make sure you're matching non-blank characters:
grep -oE '\<[[:graph:]]{6}\>' << END
fhadja
ksjfskdasd
adasda
sada s 123456
s
2 4 6
END
fhadja
adasda
123456
© 2022 - 2024 — McMap. All rights reserved.