![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|