- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to set "||" as your field separator in awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2008 10:51 PM
тАО07-28-2008 10:51 PM
How to set "||" as your field separator in awk
I've created a file called test.txt and the content of it is as the following:
aaa||bbb
Then I run
#awk -F "||" '{print $1}' test.txt
and it output "aaa||bbb"
So I think it seems like "||" doesn't work
How can I use "||" as field separator?
Tks in advance
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2008 10:57 PM
тАО07-28-2008 10:57 PM
Re: How to set "||" as your field separator in awk
use it like this:
awk -F "[||]" '{print $1}' test.txt
source:
http://www.gnu.org/manual/gawk/html_node/Command-Line-Field-Separator.html
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2008 11:48 PM
тАО07-28-2008 11:48 PM
Re: How to set "||" as your field separator in awk
$ echo "hi||guy||sam" | awk -F"[|][|]" '{print $1, $3}'
hi sam
$ echo "hi||guy||sam" | awk -F"\\\|\\\|" '{print $1, $3}'
hi sam
$ echo "hi||guy||sam" | awk -F'\\|\\|' '{print $1, $3}'
hi sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2008 11:56 PM
тАО07-28-2008 11:56 PM
Re: How to set "||" as your field separator in awk
This won't happen on HP-UX's awk. My GNU awk will do that. HP-UX awk gives:
awk: There is a regular expression error.
Unknown error
The input line number is 1.
The source line number is 1.
As documented, FS is an Extended Regular Expression where "|" is special.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2008 04:08 AM
тАО07-29-2008 04:08 AM
Re: How to set "||" as your field separator in awk
Hence, the moral of this story is to escape characters with special meaning. The same applies if you used 'split':
# echo 'aaa||bbb'|awk '{split($0,a,/\|\|/);print a[1]}'
aaa
# echo 'aaa||||bbb'|awk '{split($0,a,/\|\|/);print a[1],a[3]}'
aaa bbb
...notice the four pipe symbols in the last example. We want to take them two-at-a-time as dictated by our regular expression. The second field is thus an empty one.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2008 12:34 AM
тАО08-01-2008 12:34 AM