Hello, I am writing a script in ksh to output a data file that is pipe delimited (not my choice). In any case, it has me scratching my head since the output is not as expected.
The output ignores "$ssid|2012|$acc" for some reason. I can print them separately if I include some echoes before that line but that line only prints the right half starting from "|${acc_f[$i]}...". Does anyone have any ideas what is going on?
Thanks,
Ben
Moderator's Comments:
Please view this code tag video for how to use code tags when posting code and data.
First off, it would have been nice to tell the interested audience which shell you are using. I take it, from the "read -u" command, which is not there in bash, that you are using a ksh (probably ksh93), but instead of letting me hone my investigative talents it is generally more fruitful to describe your environment (this includes the OS you are on - crystal balls are in short supply nowadays).
And while i am at it: giving the thread a meaningful title would also have helped. Something like "display variables after reading from file" would probably have gotten more answers in shorter time.
Quote:
Originally Posted by benalt
You should probably use "print" instead of "echo" - "print" is a builtin and offers more possibilities. But this only as an aside.
I suppose it is because "|" is a special character to the shell. Try the following, which is a bit safer:
Firstly, thanks for the response. While I did mention I was using ksh I should have mentioned that I am using both ksh93 and cygwin. I am assuming there is something not right with this combination. When I substituted your print line, the output I got inexplicably had the fields in the wrong order: |N|Y|N|Y|N|2012|901
I.e. it wrote the right half of the print first and skipped the ${ssid} completely before printing the left side. I just rewrote it in awk in my frustration and it worked fine.
In any case, I appreciate your help and scripting advice.
Thanks,
Ben
As a quick update to this problem. I ran my ksh93 script at home on CentOS and had the same puzzling results. I did a od -cx on the files after a quick look at the results, what stood out was the \r\n in the files. I did a dos2unix on the files and the script now works. I presume this is the result of working with Windows based cygwin. The results were still quite unexpected. I thought I might update this info here as it I guess pays to check what is really contained in your data files.
Thanks to the carriage returns, it only looked like it wasn't being printed. When the shell read the lines into the variables, using the \n as the delimiter, the string assigned to the variables ended in \r. So, when you were echoing them, the embedded \r in the string was causing the teriminal's cursor to return to column1 and overwrite what had been there.
Once you've been bitten by this, you tend to remember it.
Thank you for reporting back with your findings. At some point in the future, someone may find it useful.
Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
Thank you for posting the solution. It is a common problem which appears daily on unix.com. Mainstream unix uses line-feed as a record terminator in text files whereas MSDOS (and thence Microsoft Windows) uses the two characters carriage-return line-feed.
This is a long-standing design issue with Microsoft Operating Systems. It does mean that in early MSDOS you could output a text file to a printer port (with no proper printer driver) and it would print ok.
If you want to quickly check a file, this command will make line terminators visible :
Last edited by methyl; 07-15-2012 at 08:39 PM..
Reason: addenda
If you want to quickly check a file, this command will make line terminators visible :
Absolutely right.
Another way is in vi to enter ":set list" in command-mode. This makes all non-printable characters (line feeds, tabs, etc.) visible. Switch off with ":set nolist".
Thanks go to the threads O/P for writing a follow-up and bothering to share his solution. Thanks also to alister for the explanation.
I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Hi Guys,
I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Hi Team -
I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n?
%~n is a DOS variable that dispayed the script name.
For instance (in DOS):
REM... (11 Replies)
Hello guys.
i wan to write an script to Customize this output:
-------------------+--------------------+---------+---------
ias-component | process-type | pid | status
-------------------+--------------------+---------+---------
DSA | DSA | ... (10 Replies)
Hi Guys..
Need your help to format the output of my shell script.
I am using spool command to take out put in csv file.
below is my code. (for example)
col USERNAME for a15
col EMAIL for a30
col FULL_NAME for a20
col LAST_LOGIN for a40
col DATE_CREATED for a40
SPOOL 120.csv... (3 Replies)
Hi All,
Below is the shell script for which desired output is required:
Shell script:
#!/bin/bash
. /oracle/TEST/db/tech_st/11.1.0/TEST_<hostname>.env
sqlplus /nolog << EOF
connect / as sysdba
spool /home/oracle/db_output.log
@lockwait.sql
prompt Database Locks
... (1 Reply)
I have a TCL script that logs into a switch using expect.I send a command "show port-security address" and it returns a table having a large number of rows.I need to capture this output(the table) and store it in a .txt file.
I have done this:
match_max 5000
set expect_out(buffer) {}
set... (0 Replies)
Hi guys,
I have a simple question, I want to store the output of the following command:
As you can see it is running all the time, and we get a new line every 3sec.
I just want to store these new lines into a single variable, so I can use it into a script.
To clear the screen, and... (4 Replies)
I'm trying to automate numerous grep searches via shell scripting. However, the output of echo is ONE line whereas grepping from the command line would give line breaks as seen in the file. For example:
test.txt:
12345 - hi
12345 - how
54321 - are
12345 - you
grep output would be:... (4 Replies)