Simple cat and echo question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple cat and echo question
# 1  
Old 09-20-2012
Simple cat and echo question

Apologies, probably a really simple problem:

I've got a text file (nh.txt) with this in it:

Code:
    user1 email1 email2
    user2 email1 email2
    etc

With the following basic script:

Code:
    for tline in $(cat nh.txt)
    do
        echo "**********"
        echo $tline
    done
 
I get this output:
 
        **********
         user1

        **********
         email1
 
        **********
         email2
 
        **********
         user2
 
        **********
         email1
 
        **********
         email2

etc.

ie. the 'cat' command seems to be supplying one field at a time, rather than the whole line - is that right?

What do I do to get the whole line at one go?

Thanks.
# 2  
Old 09-20-2012
That is a dangerous use of backticks and useless use of cat and discovered for yourself one of the reasons why Smilie You are correct, it splits on whitespace, not lines. (You can change this by altering the special IFS variable, but read is preferred over backticks for this anyway.)

If you want to read lines, you can do this.

Code:
while read LINE
do
        echo "got line $LINE"
done < inputfile

This is also more efficient than cat in backticks.

It can even split tokens for you:

Code:
while read USERNAME EMAIL1 EMAIL2
do
        echo "user $USERNAME"
        echo "Email1 $EMAIL1"
        echo "Email2 $EMAIL2"
done < inputfile

read also obeys IFS, so if your file was separated by commas instead of spaces, the loop would still work with a tiny change:

Code:
while IFS="," read USERNAME EMAIL1 EMAIL2
do
        echo "user $USERNAME"
        echo "Email1 $EMAIL1"
        echo "Email2 $EMAIL2"
done < inputfile

# 3  
Old 09-20-2012
Code:
while read tline
do
 echo "**********"
 echo "$tline"
done < nh.txt

EDIT: Corona688, you are scorchingly fast...!!!
# 4  
Old 09-20-2012
Thanks guys

('dangerous use of backticks' - like the description Smilie)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo with cat command

Hi, I need to send an email in the below format: Hi All, body description on 12-Dec-2014 <a html table, which is there in Result.txt file> I am using the below command { echo "Hi All," echo "body description on $var_date " } | ( cat Result.txt ) | /usr/sbin/sendmail -oi -t ... (1 Reply)
Discussion started by: Prasannag87
1 Replies

2. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

3. Shell Programming and Scripting

need simple echo question

Hi I want to use echo command as below echo 'dir=' $1 ' dir|file|home' i need output like below : echo 'dir=' $1 ' dir|file|home' pp13dff Output dir=pp13dff dir|file|home (4 Replies)
Discussion started by: asavaliya
4 Replies

4. Shell Programming and Scripting

Simple echo problem

Hey all! I'm in an intro to UNIX class at university, and we've just began writing scripts. Naturally I can't get it to do what I want. Basic script as follows: COMPARE1=`ls|wc -l` tar czf archive.tgz ~/path/to/file COMPARE2=`tar tvzf archive.tgz|wc -l` if then ... (7 Replies)
Discussion started by: nickzourdos
7 Replies

5. Shell Programming and Scripting

"Simple" echo/reading variable question...

Hello, I have a simple(I think) question! Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is: 1)I have an awk script that prints something, such as: awk '{print $2}' a > x so x might hold the value of say '10' 2)Now, I just want to check to see if... (4 Replies)
Discussion started by: astropi
4 Replies

6. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

7. OS X (Apple)

Question about cat and echo

Hello, I am trying to send text to a USB to serial adaptor and then to an external speech synthesizer. I tried using the cat and echo commands with no luck. I have gotten some audio output from my synthesizer using Kermit a terminal emulator, so I am pretty sure my synthesizer and my USB to serial... (1 Reply)
Discussion started by: jamesapp
1 Replies

8. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

9. Shell Programming and Scripting

Problem with IF - CAT - GREP in simple shell script

Hi all, Here is my requirement I have to search 'ORA' word in out.log file,if it is present then i need to send that file (out.log) content to some mail id.If 'ORA' word is not in that file then i need to send 'load succesful' message to some mail id. The below the shell script is not... (5 Replies)
Discussion started by: mak_boop
5 Replies

10. UNIX for Dummies Questions & Answers

Simple 'echo' question

Hi, I would like to output the identical line to 2 text files, ie output='blah' echo $output > test1.txt echo $output > test2.txt Is there a way I could do that output with ONE command, ie output='blah' echo $output > test1.txt & test2.txt (I know that doesn't work) Thanks for any... (1 Reply)
Discussion started by: msb65
1 Replies
Login or Register to Ask a Question