1

I have a file with columns separated by a string. For example:

one<xx>two<xx>three<xx>four<xx>five<xx>six
one11<xx>two11<xx>three11<xx>four11<xx>five11<xx>six11

I want to extract the text of specific columns, for example, column 1, 3, and 5. So the output should be:

one<xx>three<xx>five
one11<xx>three11<xx>five11

How to do this? using commands like awk for example?

2 Answers 2

4

Set input field separator (FS) and output field separator (OFS) to <xx>:

awk 'BEGIN{FS=OFS="<xx>"} {print $1,$2,$5}' file

Output:

one<xx>two<xx>five
one11<xx>two11<xx>five11

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

0
$ perl -F'<xxx>' -pale ' $"="<xxx>";$_ = "@F[1,3,5]"' file

Set the field separator to the desired string, then collect the indices of the fields, join them with string in $" and store as current record. -p will autoprint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy