LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk - special character as delimiter (https://www.linuxquestions.org/questions/linux-newbie-8/awk-special-character-as-delimiter-4175613862/)

freeroute 09-15-2017 10:59 AM

awk - special character as delimiter
 
My file awk_test.txt contains:
Quote:

fred'eva'steve'
tom'java'mark
eve mark john
I would like to say to awk: the delimiter is "'" (not the default "space")

My command was:
Code:

awk 'BEGIN {FS ="'"} {print $1, $2}' awk_test.txt
Got a promt:
Code:

>
Why did not work?

This is working (field separator in this case is "space":
Code:

awk 'BEGIN {FS =" "} {print $1, $2}' awk_test.txt
fred'eva'steve'
tom'java'mark
eve mark

Could you help, please?

Turbocapitalist 09-15-2017 11:22 AM

There might be several ways. One is to let the shell handle the escape:

Code:

awk 'BEGIN{ FS="['\'' ]"; OFS="\t"; } { print $1, $2, $3; }'
Note carefully what is inside and outside of which kinds of quotes there. The \' ends up being processed by the shell and not by awk.

pan64 09-15-2017 12:05 PM

probably:
Code:

awk -F"'" '{print $1, $2}' awk_test.txt

freeroute 09-15-2017 12:09 PM

Okay. It works. Thank you very much for your answer.

I know that in the character range I must use escape character at some special character: -, ^, }, ].
I tried to use only escape like:
Quote:

awk 'BEGIN{ FS="[\' ]"; OFS="\t"; } { print $1, $2, $3; }' awk_test.txt
. but it was not enough.
So why must use
Quote:

awk 'BEGIN{ FS="['\'' ]"; OFS="\t"; } { print $1, $2, $3; }' awk_test.txt
?

freeroute 09-15-2017 12:16 PM

Quote:

Originally Posted by pan64 (Post 5759172)
probably:
Code:

awk -F"'" '{print $1, $2}' awk_test.txt

Thanks, it is also working.

pan64 09-15-2017 12:20 PM

remember, between ' ': 'protected text' the protected text will not be evaluated by the shell, and it must not contain ', because that is the delimiter itself ( = beginning and the end)
Therefore you need to do the following:
Code:

awk                  # the command itself
'BEGIN{ FS="['      # first protected string
\'                  # a single '
' ]"; OFS="\t"; } { print $1, $2, $3; }'  # second protected string
filename


Turbocapitalist 09-15-2017 12:49 PM

Yes, anything between two single quotes is protected and won't be processed. It will be taken literally instead:

Code:

$ a='\"'
$ echo $a
\"

$ a="\""
$ echo $a
"

In the first example the backslash remains literally a backslash.

In the second example it is processed.

freeroute 09-15-2017 01:27 PM

Okay. Now I understand :)
Thank you.


All times are GMT -5. The time now is 09:53 AM.