Reputation: 317
How to work with lines that have only 4 words in them with SED?
That’s what I managed to do, but it’s not working:
sed -e '/[ ]*[^ ]+[ ]*[^ ]+[ ]*[^ ]+[ ]*[^ ]+[ ]*/!d' -e 'other commands...' fileName
Upvotes: 0
Views: 77
Reputation: 10324
This should be portable:
sed -n '
/^[[:blank:]]*\([[:alpha:]]\{1,\}[[:blank:]]\{1,\}\)\{3\}[[:alpha:]]\{1,\}[[:blank:]]*$/ {
# capture the 1st word of every 4 word line and print it 3 times
s/^[[:blank:]]*\([[:alpha:]]\{1,\}\).*/\1 \1 \1/
p
}
' > temp-file
Upvotes: 1
Reputation: 21965
AWK
may be the easier tool for your task. Just check the number of fields in a line is equal to four using the awk built-in variable NF
.
awk 'NF==4' filename
would be a good starting point. if you wish to write the changes to file, you can use the inplace edit option of the GNU AWK
like below
gawk -i inplace 'NF==4' filename
Upvotes: 0