LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2012, 08:59 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
awk delimeter | problem.


I have such data file. | 's ascii is 0x7C.
$cat mydatafile
0001|NameOne|60|70
I have to get the NameOne field. I use
Code:
while read line
do
	nameField=`echo $line| awk -F"|" '{print $2}'`;
	mathscore=`echo $line| awk -F"|" '{print $3}'`;
done < mydatafile
I thought it's easy. But I encounter UNICODE problem.
when the name(UNICODE) field have 0x7C, it goes wrong.
Is there any solution for this? I hope I make the problem clear. Thanks in advance!

Last edited by fantasy1215; 03-14-2012 at 10:00 PM.
 
Old 03-14-2012, 09:57 PM   #2
adixon
Member
 
Registered: Oct 2010
Posts: 34

Rep: Reputation: 3
I don't think the problem is with unicode, your code is working for me - added some output to see

Code:
#bin/sh
while read line
do
	nameField=`echo $line| awk -F"|" '{print $2}'`;
	mathscore=`echo $line| awk -F"|" '{print $3}'`;
	echo nameField is `echo $nameField`
done < test_in.txt
I get
Quote:
nameField is NameOne
If you want though, can always do
Code:
cat test_in.txt | tr -s '|' ' '
now the | chars are normal whitespace..
 
Old 03-15-2012, 02:52 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about just let bash do it:
Code:
#!/bin/bash

while IFS="|" read -r _ nameField mathscore _
do
    echo $nameField
    echo $mathscore
done<mydatafile
 
  


Reply



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
Select all fields till the last delimeter RanjanR Programming 4 01-06-2012 11:09 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
split string on file delimeter Jeroen1000 Programming 7 10-05-2009 08:35 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
cut with multiple character delimeter krock923 Programming 9 12-21-2004 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 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
Open Source Consulting | Domain Registration