ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
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?
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.