combine variables in one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting combine variables in one
# 1  
Old 07-28-2009
combine variables in one

Within one of my scripts, I have variables extracted from parameters list and want to combine some of them in one (lpath). I tried using awk command..... How do I get it? Thank you.


Code:
 
#!/bin/bash
# ## Read paramlist.txt 
file='/name1/name2/paramlist.txt'
while read line
do
#
key= echo $line | awk -F\| '{ print $1 }'
host= echo $line | awk -F\| '{ print $2 }'
user= echo $line | awk -F\| '{ print $3 }'
ftppass= echo $line | awk -F\| '{ print $4 }'
lpath= echo $line | awk -F\| '{ print $5 $6 $7 $8 $9 }'
echo "$lpath"
fpath= echo $line | awk -F\| '{ print $10 $11 $12 $13 $14}'
email1= echo $line | awk -F\| '{ print $15 }'
email2= echo $line | awk -F\| '{ print $16 }'
 
file_ctr=`egrep -cv '#|^$' $lpath` 
echo "$file_ctr"
#
done </name1/name2/paramlist.txt


Here is the parameters file:

Code:
 
key1|IP1|user1|pass1|lpath1|lpath2|lpath3|lpath4|lpath5|lcount|rpath1|rpath2|rpath3|rpath4|rpath5|email1 |email2
key2|2IP2|user2|pass2|lpath1|lpath2|lpath3|lpath4|lpath5|lcount|rpath1|rpath2|rpath3|rpath4|rpath5|email1 |email2
Key3|2IP3|user3|pass3|lpath1|lpath2|lpath3|lpath4|lpath5|lcount|rpath1|rpath2|rpath3|rpath4|rpath5|email1 |email2

# 2  
Old 07-28-2009
to store some command o/p in a variable you have to put that command between backsticks "`"
for example
Code:
key1=`echo $line | awk -F\| '{ print $1 }'`

# 3  
Old 07-30-2009
or "take value of subshell output". Easier to read as using old bsh style ` `. My opinion.
Code:
key=$( echo $line | awk -F\| '{ print $1 }'  )

# 4  
Old 07-30-2009
Hammer & Screwdriver Other ways to approach the matter

Why?
Code:
key= echo $line | awk -F\| '{ print $1 }'
host= echo $line | awk -F\| '{ print $2 }'
user= echo $line | awk -F\| '{ print $3 }'
ftppass= echo $line | awk -F\| '{ print $4 }'
lpath= echo $line | awk -F\| '{ print $5 $6 $7 $8 $9 }'
echo "$lpath"

another approach
Code:
lpath=`echo $line | cut -d"|" -f5-9`

or, on some flavors of unix
Code:
lpath=$(echo $line | cut -d"|" -f5-9)

# 5  
Old 07-30-2009
Great ! Thank you all
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. UNIX for Dummies Questions & Answers

Combine two lists of variables

Thanks in advance for any advice and help. I have two lists of variables that I want to put into nested for loops. for x in 1 2 3 do for y in a b c do The output I want is: filepath/1/ command modified by a filepath/2/ command modified by b filepath/3/ command modified by c To... (10 Replies)
Discussion started by: wheyhamp
10 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

5. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

6. Shell Programming and Scripting

guide needed to combine 2 variables

readNOW=$(date +"%Y%m%d%H%M") read readTerminal #input B terminalc=$readTerminal echo eqpt_list_QC_$terminalcT_$readNOW how to get display result it: eqpt_list_QC_BT_201209121530 (2 Replies)
Discussion started by: ment0smintz
2 Replies

7. Shell Programming and Scripting

combine

Dear all i am having text file like xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|>< 5|6|>< jjj|kkk|>< 8|9>< i want to join two lines which are having ' >< ' by taking only two lines at a stretch ...using awk command the result output should be xxx|yyy|1|2| zzz|rrr|3|4| www|xxx|5|6|... (2 Replies)
Discussion started by: suryanarayana
2 Replies

8. Shell Programming and Scripting

combine

Hi I am having text file like this 001|ramu|hno221|>< sheshadripuram|delhi|560061>< 002|krishna|hno225|>< newdelhimain|delhi|560061>< i want to combine every two lines as single...line... i.e 001|ramu|hno221|sheshadripuram|delhi|560061 can u pls help me (3 Replies)
Discussion started by: suryanarayana
3 Replies

9. Shell Programming and Scripting

awk : combine 3 variables into 1

Within one of my awk scripts, I have three variables extracted and calculated on. When done, I simply want to combine the three. The following works, but looks weird. My script reads a field that has text and numbers, knowing the last four comprise MMYY (month and year) # YY are last two... (2 Replies)
Discussion started by: joeyg
2 Replies

10. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies
Login or Register to Ask a Question