The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cat operation trichyselva UNIX for Dummies Questions & Answers 0 03-24-2008 06:55 AM
Help with arithmetic operation emjayshaikh Shell Programming and Scripting 3 09-24-2007 12:44 AM
RPC failure on yp operation Remi SUN Solaris 1 05-22-2007 11:15 AM
split operation scotty_123 Shell Programming and Scripting 1 04-05-2007 09:34 AM
multiple operation ajnabi Shell Programming and Scripting 2 09-01-2004 05:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-21-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
string operation

Hi all,

Here is my situation.

I have a text file TXT_FILE like this:
john 123456
jack 94589
kelvin 94595
mary 88585

I want to read the first word in each line ( the name ) and assign to a string variable ( EX_LIST ) in my script so that I can use later as this command
Code:
for i in $EX_LIST ; do ...

This is my complete script:
Code:
 #!/bin/sh

EX_LIST=' '
TXT_FILE=./my_file.txt

get_first()
{
        cat $TXT_FILE |while read line
        do
                first_word=`echo $line | awk '{print $1}'`
                echo This is first word: $first_word
                tmp_list=$EX_LIST
                echo This is temp list: $tmp_list
                EX_LIST=$tmp_list" "$first_word
                echo update new list: $EX_LIST
                echo ======***======
        done
        echo $EX_LIST
}

get_first

echo Printing out the result:
for nn in $EX_LIST ; do
        echo item: $nn
done
echo done!

And this is the result of my script:

Code:
This is first word: john
This is temp list:
update new list: john
======***======
This is first word: jack
This is temp list: john
update new list: john jack
======***======
This is first word: kelvin
This is temp list: john jack
update new list: john jack kelvin
======***======
This is first word: mary
This is temp list: john jack kelvin
update new list: john jack kelvin mary
======***======

Printing out the result:
done!

The problem as you can see is it does not echo anything of my for command. (Printing out the result:)
can anyone help to correct it ?
Thank you!
  #2 (permalink)  
Old 11-21-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
try this


Code:
while read line
do
        first_word=`echo $line | awk '{print $1}'`
        echo This is first word: $first_word
        tmp_list=$EX_LIST
        echo This is temp list: $tmp_list
        EX_LIST=$tmp_list" "$first_word
        echo update new list: $EX_LIST
        echo ======***======
done < $TXT_FILE

  #3 (permalink)  
Old 11-21-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,429
You can also do :

Code:
EX_LIST=`awk '{print $1}' $TEXT_FILE`


Jean-Pierre.
  #4 (permalink)  
Old 11-21-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
@ anbu23: It does not work, but thank you anyway for your kind of help.
@ Jean-Pierre: Just one line and great again! (you helped me twice). Thank you so much!
  #5 (permalink)  
Old 11-23-2006
mels mels is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 3
$EX_LIST is not global and only filled within the get_first function.
If you change the last part to:
EX_LIST2=`get_first`

echo Printing out the result:
for nn in $EX_LIST2 ; do
echo item: $nn
done
echo done!

Mels
  #6 (permalink)  
Old 11-23-2006
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Oh, i see.
Thank you mels!

PS: But i have declare EX_LIST=' ' right at the beginning of the script. It should be global, right ?
Please explain me!

Last edited by fongthai; 11-23-2006 at 10:41 PM..
  #7 (permalink)  
Old 11-24-2006
mels mels is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 3
Its shell and not c or other programming language.
If a shell is running a function in an other shell, everything defiend in the last shell is not available in the first shell.

Mels
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0