\n in ksh using echo & printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting \n in ksh using echo & printf
# 1  
Old 10-26-2010
[SOLVED] \n in ksh using echo & printf

Code:
#!/usr/bin/ksh

var1="Hi World"
var2="Morning"
var3=$(echo "$var1" \n "$var2")
echo $var3
var3=$(printf "$var1 \n $var2")
echo $var3

Output

Quote:
Hi World n Morning
Hi World Morning
Any way to get
Quote:
Hi World
Morning
in my $var3 ?

Last edited by dahlia84; 10-26-2010 at 09:00 AM.. Reason: solved
# 2  
Old 10-26-2010
What happens when you use:
Code:
echo "$var3"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-26-2010
How about:-
Code:
echo "${var1}\n${var2}"
var3=`echo "${var1}\n${var2}"`
echo "$var3"


This should preserve the special characters.



Robin
UK

Last edited by rbatte1; 10-27-2010 at 05:02 AM.. Reason: Adding code tags (as I have been chastised)
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 10-26-2010
Thanks guys!

I wonder why the same logic does not work here.

Code:
#tmp=$(echo "$tmp" "\n" $(echo $line|cut -d '|' -f 3))
tmp=$(printf "$tmp \n" $(echo $line|cut -d '|' -f 3))
echo "$tmp"

This is inside a loop and reading lines from a txt files whose data may be like this

mmddyyyy|N|Desc 1
mmddyyyy|Y|Desc 2
mmddyyyy|N|Desc 3
mmddyyyy|Y|Desc 4

I am trying store

Code:
Desc 1
Desc 2
Desc 3
Desc 4

in the tmp variable based on some condition.

As of now it stores as

Code:
\n  \n Desc 2 \n Desc 6

with writing \n as such.
# 5  
Old 10-26-2010
Um, I might be going off the point here, but are you not better using grep and cut (or awk if you are okay with that)

It's either a simple:-
Code:
cut -f3 -d"|" file

or you could expent that to be:-
Code:
grep "|Y|" file | cut -f4

Am I missing the point? You could then loop in the fashion:-

Code:
for tmp in `grep "|Y|" file | cut -f4`
do
   whatever
done


I hope that this helps


Robin

Last edited by rbatte1; 10-27-2010 at 05:00 AM.. Reason: Adding code tags (as I have been chastised)
# 6  
Old 10-26-2010
Try:
Code:
tmp=$( 
  while read line; do
    echo "$line"|cut -d '|' -f 3
  done < infile 
)
echo "$tmp"

or :
Code:
tmp=$(
  while read line; do
    echo "${line##*|}"
  done < infile
)
echo "$tmp"

or:
Code:
tmp=$(
  while IFS="|" read x x desc; do
    echo "$desc"
  done < infile
)
echo "$tmp"


Last edited by Scrutinizer; 10-26-2010 at 08:34 AM..
# 7  
Old 10-26-2010
I am using this... This did the trick.

Code:
tmp=$(
while IFS="|" read x x desc; do
  echo "$desc"
done < infile
)

rbatte1 - you may even call me dumb. I am a noob in awk and sed. I need to equip myself to write good scripts using them.

Thanks a lot for your help rbatte1 & Scrutinizer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Availability: echo vs. printf?

Hello, For some reason i dont remember, i currently believe (but beeing unsure) that printf is available on more diffrent systems (unix, bsd, linux, ??) than echo is. Could someone please enlighten me, whether this is true or not? Thank you PS: I just found pages about the diffrences of... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Grep behaves diffrent upon printf or echo output

Hello I want to check whether certain arguments were passed to the script, and when those are, not doing a log entry. If those arguments are not passed, always do a log entry (*new call*). What currently i have is this: echo "${@}"|grep -q \\- || \ tui-log -e "$LOG" "\r---- New call $$... (4 Replies)
Discussion started by: sea
4 Replies

3. Shell Programming and Scripting

Managing output... echo or printf?

Hello script guru's as i write more and more code i always block at managing output... either writing to standard out, writing to files via std out (log, temp file, etc). Don't get me wrong 99% of the time it DOES the job but maybe there is more efficient. I'm writing a small script to... (2 Replies)
Discussion started by: maverick72
2 Replies

4. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

5. Shell Programming and Scripting

Problem with printf in UNIX KSH shell

Hi ALL, I am using SunOS 5.9 and KSH(bin/ksh) The problem am facing is error message diaplyed on screen printf: 12099415.79 not completely converted printf: + expected numeric value printf: 11898578.29 not completely converted When i try printing with The output is... (6 Replies)
Discussion started by: selvankj
6 Replies

6. Shell Programming and Scripting

printf/echo in a second script

This may be little confusing. I have Script1, which pulls data from the system and creates another script(lets say script2). While I run script1 I need to add printf/echo statements for script2, so that when I run script2 I see those statement. eg: script1 765 printf " display frame-$1 timeoffset... (2 Replies)
Discussion started by: miltonrods
2 Replies

7. Shell Programming and Scripting

advanced echo/printf

I want to print a colored line using bash. I want to print: Smtp status where "Smtp status" will be in yellow and will be in green. Thanks. (2 Replies)
Discussion started by: proactiveaditya
2 Replies

8. UNIX for Advanced & Expert Users

echo in ksh sh & bash

Hello, I have lib file which contain a function that get text to print on screen by echo command. Several scripts are inculde this lib and use this function. Each one of them is written in different shell language (sh ksh & bash). This causing some issues when using backslash charater as... (4 Replies)
Discussion started by: Alalush
4 Replies

9. Shell Programming and Scripting

KSH printf: columns and colors

Ih all, I need to make a ksh script with colors, it is possible with printf to combine column and colors ? i seem not working, I think i dont doing the good thing: printf -n "%-15s %-20s %-20s\n" "\033 the position is ok Name______Age________Site ----________---_________---- Bob... (2 Replies)
Discussion started by: wolfhurt
2 Replies

10. Shell Programming and Scripting

printf command in ksh

Hi, I am very confused with my printf command. Somehow one variable can't line up with others... newstart2 ="Mon Nov 11 01 00:00:00 2002" printf "%-20s" $newstart2 Here is the output: Mon Nov 11 01 00:00:00 2002 It spread out to two lines.. Why? (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question