Reading content of a variable to create a new one?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading content of a variable to create a new one?
# 1  
Old 02-08-2010
Reading content of a variable to create a new one?

Hello.

I've written up a script, that populates a variable with a list of tapes returned from my library. For example:

701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3

So now, the variable "TAPELIST" contains those numbers, delimited by commas. I'd like to now have 2 other variables. The first containing the first 5 entries (701940L3,701941L3,701942L3,701943L3,701944L3) and the second containing the next 5...

I would guess it would be something like NEWLIST=$TAPELIST with some awks or something of the sort? and OTHERLIST=$TAPELIST next 5 entries...

Thanks for the help.

Stephan
# 2  
Old 02-08-2010
Hi, give this a try:
Code:
TAPELIST=701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3
LIST2=${TAPELIST#*,*,*,*,*,}
LIST1=${TAPELIST%,${LIST2}}

Code:
$ echo $LIST1
701940L3,701941L3,701942L3,701943L3,701944L3
$ echo $LIST2
701945L3,701946L3,701947L3,701948L3

# 3  
Old 02-08-2010
Code:
TAPELIST=701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3
FirstFive=`expr $TAPELIST : '\(\([^,]*,\)\{4\}[^,]*\).*'`
SecondFive=`expr $TAPELIST : '.*,\(\([^,]*,\)\{4\}.*\)'`

# 4  
Old 02-08-2010
Quote:
Originally Posted by Stephan
with some awks
Code:
[house@leonov] INPUT="701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3"; \
[............] LIST_1=$( echo $INPUT | awk -F ',' '{print $1, $2, $3 , $4, $5}' ); \
[............] LIST_2=$( echo $INPUT | awk -F ',' '{print $6, $7, $8 , $9, $10}' ); \
[............] echo "LIST_1=( $LIST_1), LIST_2=( $LIST_2 )"
LIST_1=( 701940L3 701941L3 701942L3 701943L3 701944L3), LIST_2=( 701945L3 701946L3 701947L3 701948L3  )

# 5  
Old 02-08-2010
Quote:
Originally Posted by Scrutinizer
Hi, give this a try:
Code:
TAPELIST=701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3
LIST2=${TAPELIST#*,*,*,*,*,}
LIST1=${TAPELIST%,${LIST2}}

Code:
$ echo $LIST1
701940L3,701941L3,701942L3,701943L3,701944L3
$ echo $LIST2
701945L3,701946L3,701947L3,701948L3

Thanks Scrutinizer. Seems to work, but grabs the first five (LIST2) and LIST1 then lists the rest of the tapes? not necessarily the next five...Appreciate the post. Thanks.

I got this to work pretty good though. Increased it to 7 "tapes" though to be a bit safer in my queries...

---------- Post updated at 01:17 PM ---------- Previous update was at 01:13 PM ----------

Quote:
Originally Posted by anbu23
Code:
TAPELIST=701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3
FirstFive=`expr $TAPELIST : '\(\([^,]*,\)\{4\}[^,]*\).*'`
SecondFive=`expr $TAPELIST : '.*,\(\([^,]*,\)\{4\}.*\)'`

Thanks for the response. Yours seems to grab the first five but the the last five and not the next five...This could work though this way (at least as to what i am trying to do...). How could the "SecondFive" be modified to grab the "next" five though?

Thanks.

---------- Post updated at 01:19 PM ---------- Previous update was at 01:17 PM ----------

Quote:
Originally Posted by dr.house
Code:
[house@leonov] INPUT="701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3"; \
[............] LIST_1=$( echo $INPUT | awk -F ',' '{print $1, $2, $3 , $4, $5}' ); \
[............] LIST_2=$( echo $INPUT | awk -F ',' '{print $6, $7, $8 , $9, $10}' ); \
[............] echo "LIST_1=( $LIST_1), LIST_2=( $LIST_2 )"
LIST_1=( 701940L3 701941L3 701942L3 701943L3 701944L3), LIST_2=( 701945L3 701946L3 701947L3 701948L3  )

This obviously works just as expected and it is what i was trying to figure out...But i need the LIST_1 to return the comma in between the tape list...Kinda like "701940L3,701941L3,701942L3,701943L3,701944L3". No commas at the beginning of the list and none at the end...

Thansk.

Last edited by Stephan; 02-08-2010 at 02:41 PM..
# 6  
Old 02-08-2010
Can't you just use cut?

Code:
List_1=$(echo $TAPELIST | cut -d, -f1-5)
List_2=$(echo $TAPELIST | cut -d, -f6-10)

# 7  
Old 02-08-2010
Quote:
Originally Posted by steadyonabix
Can't you just use cut?

Code:
List_1=$(echo $TAPELIST | cut -d, -f1-5)
List_2=$(echo $TAPELIST | cut -d, -f6-10)

Looks like it is working like a charm! Thanks. Simple enough.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Reading flat file content

is there any unix tools that can read the text files like through SQL queries? (ie in Hadoop, Impala DB support flat file query) (1 Reply)
Discussion started by: omykarshan
1 Replies

2. Solaris

Reading binary content

Dear Gurus I am stuck with the peice of work and do not know from where to start. I get a machine generated file which is binary file contain binary data, i want to read binary data as it is without converting into any other format. i want to read byte by byte. Please let me know what... (24 Replies)
Discussion started by: guddu_12
24 Replies

3. Shell Programming and Scripting

Trouble reading content of file from a variable

Hi , i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help. I dont want to use cat command to read. Can we do it with out using cat command. while read line do... (9 Replies)
Discussion started by: Ravindra Swan
9 Replies

4. Shell Programming and Scripting

Create Multiple files with content

I have a file details.csv and I need to create 5 files in same folder named as details1.csv,details2.csv,details3.csv,details4.csv,details5.csv along with contents of details.csv Thanks in Advance. (9 Replies)
Discussion started by: Prashanth B
9 Replies

5. Shell Programming and Scripting

Need to create concatenate the shell variable with file content

Hi Guys, I have a file. Each record needs to inserted into a table. The table also have other columns which needs to be inserted with Shell variables. The following is the file. Error code None. Error Code 1 The shell script is having these variables. Name=Magesh Dep=Coding ... (1 Reply)
Discussion started by: mac4rfree
1 Replies

6. Shell Programming and Scripting

Reading files under a folder and formatting content of each file

I have 'n' number of files in a folder .each file in the folder "myfolder" is having the content like. COLNAME ------------ AAAAAA BBBBBB CCCCCC DDDDDD ... ... ... ZZZZZZ 26 recrod(s) selected. My request is by reading each file in "myfolder" and format each file such a way... (18 Replies)
Discussion started by: rocking77
18 Replies

7. Shell Programming and Scripting

Problem in reading a file content

Hi, I am reading a file line by line using read line function of while loop. Each line contains 4 fields. I want to take these 4 values in 4 variables in each iteration so that i can use them in my script. The issue here is that my awk command is returning awkward results - Here is a sample line... (8 Replies)
Discussion started by: garman
8 Replies

8. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

9. UNIX for Dummies Questions & Answers

Reading input to create a variable in a script?

I would like to prompt for input and then use it as a variable in a script. Something like this. #!/bin/ksh echo "What is your name?: \c" read response echo "Your name is $reply" >file.txt done exit 0 What am I missing? Thanks, (7 Replies)
Discussion started by: darthur
7 Replies
Login or Register to Ask a Question