LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-26-2011, 10:49 AM   #1
CodyK479
LQ Newbie
 
Registered: Feb 2011
Posts: 18

Rep: Reputation: Disabled
Bash newbie - blank string help


Using awk I pull the first field of a random line from my datafile.

myvar1=`awk -F"\t" 'NR=='$randline' {printf "%s\n", $1}' myfile

This works fine. The problem is there will be empty lines at the end of the file. Rather than using awk
to filter out blank lines I would like to figure this out first.

So I test $myvar1 for a blank string after setting $randline to one that I know is blank:

test -z "$myvar1" && echo "true" || echo "false"

But, this returns "false"? So the string is not zero length. Why? It's a tab-separated file. Is awk storing the tab with the $1 field or something.

This is where I get headache. I try to echo my variable to see what it looks like.

echo "$myvar1"
outputs: nothing
echo "My variable is [$myvar1]"
outputs: [y variable is [

Why is the closing bracket at the beginning? What character could be stored in $myvar1 that would do such a thing and how did it get there?
 
Old 03-26-2011, 10:54 AM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
to filter out empty lines, use NF

Code:
awk -F"\t" 'NR=='$randline' && NF {printf "%s\n", $1}' myfile
 
Old 03-26-2011, 11:41 AM   #3
CodyK479
LQ Newbie
 
Registered: Feb 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you. But I would still like to understand why my strings are behaving this way.
Why wouldn't my variable be a zero-length string if it was pulled from a blank line?

Besides, I'm not sure that will work since $randline might be a blank line. Wouldn't I be filtering out the line I told Awk to print?

If it helps any, If the following is correct, then awk does not see any blank lines.
The following output 134, when there are really 132 non-blank lines. Well, they certainly seem blank 2 blank lines to me.

awk 'NF!=0 {print}' myfile | wc -l
 
Old 03-26-2011, 11:52 AM   #4
CodyK479
LQ Newbie
 
Registered: Feb 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Actually, doing this outputs 132. So my two blank lines are considered to have 1 field each. Is that normal for a blank line? If so I don't like it. And is still doesn't explain the behavior when I try to echo it.

awk 'NF!=1 {print}' myfile | wc -l
 
Old 03-26-2011, 12:16 PM   #5
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
My stab in the dark here, without the original file, is that it is written and saved on a Windows machine and the unusual result of placing the square bracket at
the front is a direct result. Try running dos2unix or some such to make sure the file is clean and try your scripts again.
 
Old 03-26-2011, 12:58 PM   #6
CodyK479
LQ Newbie
 
Registered: Feb 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
My stab in the dark here, without the original file, is that it is written and saved on a Windows machine
That's it. Thanks. It is created in Windows. I was afraid of that. It is uppose to be a standard ASCII text file.

Are there no bash native commands that could work?
 
Old 03-27-2011, 06:55 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It probably is a standard ascii file. It's just that DOS/Windows and Unix use different line ending characters. Unix text files use the ascii LineFeed only, while Dos files use CarriageReturn+LineFeed. So you have to remove that extra CR before most *nix tools will work reliably.

There are also other line ending systems, such as the old Apple type, which is CR only.

http://en.wikipedia.org/wiki/Newline

Last edited by David the H.; 03-27-2011 at 06:57 AM. Reason: got it backwards
 
Old 03-27-2011, 07:18 AM   #8
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Quote:
Originally Posted by CodyK479 View Post
Are there no bash native commands that could work?
Yes, the problem is that you're not seeing the non-printing characters.
Run "cat -vt myfile" and you'll see that there are ctrl-M characters on
the blank lines.

As mentioned this is normal for a DOS file, and running dos2unix on the
file will fix it. If you don't want to touch the original file, you
can pipe the output through "sed 's/^M$//'" to your command. The ^M is
created by typing ctrl-V and ctrl-M. It is not a caret and a capital m.
 
  


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
how to read a blank line ie a string of length 0. ishandutta2007 Programming 3 04-20-2010 05:13 AM
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 08:55 AM
selectively remove blank spaces from string Cpt_Cell Linux - Newbie 2 10-08-2009 02:41 AM
Bash String help Fredstar Programming 1 09-03-2008 12:58 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM

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

All times are GMT -5. The time now is 03:15 PM.

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