Score:1

Awk command with word as delimitor

de flag

How to I use awk to cut based on a word that keeps everything after it : Example:

File.txt > 
XXXXWORDNNNNNN
XXXWORDNNNNNNN
XXWORDNNNNNNNN
XWORDNNNNNNNNN
WORDNNNNNNNNNN

output desired:

File.txt >
NNNNNN
NNNNNNN
NNNNNNNN
NNNNNNNNN
NNNNNNNNNN
Score:2
cn flag
cat File.txt | awk -F"WORD" '{ print $2 }'

The -F parameter usually searches for a single character as a delimiter, but since you wanted to use a string of letters as your delimiter, it must be put in double quotes.

The $2 means all the text following "WORD" ... unless "WORD" reoccurs in the line of text. If that happens then you'll only see the text in between the first and second occurrence of "WORD".

I hope this helps.

hr flag
+1 although quotes are not actually required just because the delimiter is multi-character - they're only necessary if it contains characters that are special to the shell (which `WORD` doesn't). The more significant difference is that a multi-character FS value is interpreted as a *regular expression* (although again `WORD` doesn't contain any regex special characters). Also you have a [uuoc](https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat). So it could be simplified to `awk -F WORD '{print $2}' File.txt`
mondotofu avatar
cn flag
Thanks @steeldriver. I hadn't known there was a term for uuoc. I sometimes use cat when I am trying out an idea for readability sake or transmitting stdout through a sequence of pipes. I could have used *<File.txt awk -F WORD '{print $2}'* but I have clobbered too many files this way.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.