string operation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string operation
# 1  
Old 11-21-2006
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  
Old 11-21-2006
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  
Old 11-21-2006
You can also do :
Code:
EX_LIST=`awk '{print $1}' $TEXT_FILE`


Jean-Pierre.
# 4  
Old 11-21-2006
@ 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! Smilie
# 5  
Old 11-23-2006
$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  
Old 11-23-2006
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  
Old 11-24-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String operation in csh AIX 4.3.2.0

Hi to everybody i stuck on a simple thing i had a string and i want cut it , i try already few thing with the cut command but does not the way it should. The script is in csh and running on AIX 4.3.2.0 here are few samples how the string can look like FT71;1;1;1;;;1;31.01.2017... (9 Replies)
Discussion started by: Nadielosabra
9 Replies

2. Shell Programming and Scripting

String operation in csh shell

Hi, to everybody i have a string which Looks like this FT47;3;1;1;;;1;09.02.2017 21:21:19;2;2 and i would like to change to value on one Position only e.g. the values on Position 6 should change to 1 nevertheyless which values was there before AIX 4.3.2.0 and csh i try... (10 Replies)
Discussion started by: Nadielosabra
10 Replies

3. Shell Programming and Scripting

Complex string operation (awk, sed, other?)

I have a file that contains RewriteRules for 200 countries (2 examples for 1 country below): RewriteRule ^/at(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=de_AT #& RewriteRule ^/at_english(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=en_AT I have... (5 Replies)
Discussion started by: usshadowop
5 Replies

4. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

5. Shell Programming and Scripting

sed OR-Operation

Hello, I'm trying to get the configuration-IP-Addresses from Cisco-configurations on the Routers they are defined as a Loopback0-interface like this: interface Loopback0 ip address 172.23.19.249 255.255.255.255 On the Switches they are defined as a VLAN 80 interface like this ... (4 Replies)
Discussion started by: Sally[-_-]
4 Replies

6. Shell Programming and Scripting

String Operation

/home8/mc09ats/UnixCw/file4 this is the path...i have 2 variables filename and filepath...i want filename=file4 filepath=/home8/mc09ats/UnixCw i.e. i think i have to find last occurence of "/" in string and the string after "/" want to take in some variable and string before last "/"... (4 Replies)
Discussion started by: AbhijitIT
4 Replies

7. Shell Programming and Scripting

Help - Search for string, then do string operation on line

Hi, I wish to find all lines that contain a specific search word, and then do few string operations on that line. The idea is to "fix" the file which has been moved from windows to unix. Using unix - Sun Solaris Test input ("t2.sas") statement1 statement2 libname yahoo ... (6 Replies)
Discussion started by: deepaksinbox
6 Replies

8. UNIX for Dummies Questions & Answers

string operation

I am trying to ask for full name in a script, Then echo back to the user with the surname only, omitting the first name. Thanks (2 Replies)
Discussion started by: orjnet
2 Replies

9. Shell Programming and Scripting

string operation

i am new user of unix.i have a question.My script is- export STR_ALFA=`head -2 "${FILE_PATH}"|tail -1|cut -d"," -f1` "${TEST_HOME}"/function/chk_alfa.ksh STR_ALFA now i want to check STR_ALFA: 1)whether is alphabetic 2)whether is numeric 3)whether is alphanumeric... (1 Reply)
Discussion started by: arghya_owen
1 Replies

10. Shell Programming and Scripting

string operation

i am new user of unix.i have a question.My script is- export STR_ALFA=`head -2 "${FILE_PATH}"|tail -1|cut -d"," -f1` "${TEST_HOME}"/function/chk_alfa.ksh STR_ALFA now i want to check STR_ALFA: 1)whether is alphabetic 2)whether is numeric 3)whether is alphanumeric... (1 Reply)
Discussion started by: arghya_owen
1 Replies
Login or Register to Ask a Question