Read Embedded Newline characters with read (builtin) in KSH93


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read Embedded Newline characters with read (builtin) in KSH93
# 1  
Old 01-10-2011
Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys,

Happy New Year to you all!

I have a requirement to read an embedded new-line using KSH's read builtin.
Here is what I am trying to do:

Code:
run_sql "select guestid, address, email from guest" | while read id addr email
do
   ## Biz logic goes here
done

I can take care of any embedded spaces by escaping them with a '\' in the run_sql utility and read would parse the value correctly.
However, address field can have embedded new line characters. Is there an escape sequence I can use to encode any embedded newlines, so the read command would store the new-line character in 'addr' variable?
I have tried '\n', but read doesn't recognize that escape-sequence.

Is there an option in read to accept embedded newlines? Or is there another builtin that can be used for this purpose?

Any help would be appreciated!
# 2  
Old 01-10-2011
Asking a line oriented tool to not be line oriented is tough, but if you know it is continued, say because of an escape character, why not read again and concatenate?
# 3  
Old 01-10-2011
Quote:
Originally Posted by DGPickett
Asking a line oriented tool to not be line oriented is tough, but if you know it is continued, say because of an escape character, why not read again and concatenate?
I can write an abstraction layer (say, a function called readx) around read and use it instead of using read directly in my main loop.

However, I am writing a framework to integrate database access into the shell and want a minimum number of new functions/utilities to be introduced for my developers.

If there is a shell trick that can solve my problem then it would be great. Otherwise I will have to work around by creating custom function/builtin for the purpose.
# 4  
Old 01-10-2011
read -r in ksh93 will leave the \n as they are. You can then use echo (or printf) to translate them to the correct character:

Code:
$ echo 'Programmer\\ntest' | read -r line rest
$ set | grep line                             
line='Programmer\ntest'
$ line=$(echo $line)
$ set | grep line
line=$'Programmer\ntest'


Last edited by Chubler_XL; 01-10-2011 at 06:53 PM.. Reason: typos
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-11-2011
Quote:
Originally Posted by Chubler_XL
read -r in ksh93 will leave the \n as they are. You can then use echo (or printf) to translate them to the correct character:

Code:
$ echo 'Programmer\\ntest' | read -r line rest
$ set | grep line                             
line='Programmer\ntest'
$ line=$(echo $line)
$ set | grep line
line=$'Programmer\ntest'

Hmm... this can be done. But it requires a step of post-processing the value after read, which I was trying to avoid. Smilie
Seems like extra processing will be required to solve my problem. Smilie
# 6  
Old 01-11-2011
Suggest you write an sql program to output the fields to a file (not a pipe) in a more useable format. Maybe the whole process can be done in sql? Really depends what you are going to do with the data.
# 7  
Old 01-11-2011
Well, how does the shell know which line feeds are real?

You might write a standard (local shared), postprocessing, sql tool (wrapper) for all to use, and make it encode embedded linefeeds in a printf friendly form, while escaping any printf metacharacters in the flow. It seems unlikely people will enter '\n' or '%' in their text, but with escapes, it would stay '\n' and '%'! It can trim and divert headers, counts, dividers and messages at the same time. It will be like JDBC/ODBC or ESQL, but crude and more work. Why not step up to a RDBMS tool or existing scripting extension for your RDBMS, sybperl or such?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read in numbers from console won't stop at newline.

Hello, I have snippet code from Lippman's <<C++ primer>>. The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Programming

How to read extended ASCII characters from stdin?

Hi, I want to read extended ASCII characters from keyboard using c language on unix/linux. How to read extended characters from keyboard or by copy-paste in terminal irrespective of locale set in the system. I want to read the input characters from keyboard, store it in an array or some local... (3 Replies)
Discussion started by: sanzee007
3 Replies

3. UNIX for Dummies Questions & Answers

Characters in a single read

Hi, I have the file which has the data : accctaaccctaaccctaaccctaaccctaaccctaaccctaaccctaac cctaacccaaccctaaccctaaccctaaccctaaccctaaccctaacccc taaccctaaccctaaccctaaccctaacctaaccctaaccctaaccctaa ccctaaccctaaccctaaccctaaccctaacccctaaccctaaccctaaa... (24 Replies)
Discussion started by: rajivn786
24 Replies

4. Windows & DOS: Issues & Discussions

Read last 7 characters from line ?

I have a txt like this: 638.301 (0.00973985) @ 50+55 the thing I want to do is read the last seven characters from the end of the first line. how do I do this ? Thanks in advance and sorry for being DOS only here. (2 Replies)
Discussion started by: pasc
2 Replies

5. Shell Programming and Scripting

how to read multi line characters in a single column

Hi, I have file in which fields are pipe (|) separated and the record separator is new line (\n). But sometime I am getting a field value which is spread across multiple line. Basically I am getting a file from another system in the below format and needs to process the file and load data into a... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

6. Programming

How to make gl_get_line read unicode characters

Hi, My program uses gl_get_line from libtecla to get user input from terminal. It works fine as long as I enter English at the terminal prompt. However, if I enter other languages, such as Chinese characters, either by typing in or cut-and-paste, the input characters get cleared from terminal... (5 Replies)
Discussion started by: tdw
5 Replies

7. UNIX for Dummies Questions & Answers

ksh93 newline character problem

Hi , I am working on Linux, with ksh93 installed. The following statement echo \\n just prints \n on this shell. However on pdksh echo \\n gives a blank new line as a output. What is the correct way of printing a new line character on the ksh93? (4 Replies)
Discussion started by: neeto
4 Replies

8. Shell Programming and Scripting

Read text from a file between two characters..

I have a requirement where i have to read from a .sh file a text lying bet characters like 'SELECT' & ';'...Please help me out in this. I am new to shell scripting. (2 Replies)
Discussion started by: goutam_igate
2 Replies

9. Shell Programming and Scripting

Removing Embedded Newline from Delimited File

Hey there - a bit of background on what I'm trying to accomplish, first off. I am trying to load the data from a pipe delimited file into a database. The loading tool that I use cannot handle embedded newline characters within a field, so I need to scrub them out. Solutions that I have tried... (7 Replies)
Discussion started by: bbetteridge
7 Replies

10. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies
Login or Register to Ask a Question