LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 09-15-2017, 11:59 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
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?

Last edited by freeroute; 09-15-2017 at 12:11 PM.
 
Old 09-15-2017, 12:22 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,430
Blog Entries: 3

Rep: Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206
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.
Old 09-15-2017, 01:05 PM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,579

Rep: Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342
probably:
Code:
awk -F"'" '{print $1, $2}' awk_test.txt
 
1 members found this post helpful.
Old 09-15-2017, 01:09 PM   #4
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
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
?
 
Old 09-15-2017, 01:16 PM   #5
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
probably:
Code:
awk -F"'" '{print $1, $2}' awk_test.txt
Thanks, it is also working.
 
Old 09-15-2017, 01:20 PM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,579

Rep: Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342Reputation: 4342
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.
Old 09-15-2017, 01:49 PM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,430
Blog Entries: 3

Rep: Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206
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.
Old 09-15-2017, 02:27 PM   #8
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Okay. Now I understand
Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command to replace special character in special line rubylu Linux - Newbie 3 04-09-2015 02:45 PM
Replace 2nd occurance of a special character after nth occurance of a delimiter from dhiru_b25@rediffmail.com Programming 6 11-02-2013 12:27 AM
[SOLVED] awk, print special character, how? johnpaulodonnell Linux - Newbie 6 04-26-2012 01:48 PM
[SOLVED] m4 macro processor, use any character as delimiter becky2 Programming 2 03-12-2012 07:25 PM
change for-loop delimiter character? galle Programming 6 08-19-2009 11:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration