Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-15-2017, 10:59 AM
|
#1
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Rep: 
|
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:
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?
Last edited by freeroute; 09-15-2017 at 11:11 AM.
|
|
|
09-15-2017, 11:22 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
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.
|
|
1 members found this post helpful.
|
09-15-2017, 12:05 PM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,991
|
probably:
Code:
awk -F"'" '{print $1, $2}' awk_test.txt
|
|
1 members found this post helpful.
|
09-15-2017, 12:09 PM
|
#4
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Original Poster
Rep: 
|
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
|
?
|
|
|
09-15-2017, 12:16 PM
|
#5
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Original Poster
Rep: 
|
Quote:
Originally Posted by pan64
probably:
Code:
awk -F"'" '{print $1, $2}' awk_test.txt
|
Thanks, it is also working.
|
|
|
09-15-2017, 12:20 PM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,991
|
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
|
|
1 members found this post helpful.
|
09-15-2017, 12:49 PM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
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.
|
|
1 members found this post helpful.
|
09-15-2017, 01:27 PM
|
#8
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Original Poster
Rep: 
|
Okay. Now I understand 
Thank you.
|
|
|
All times are GMT -5. The time now is 09:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|