Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert a list from one row to many rows

Status
Not open for further replies.

alhassani54

Programmer
Aug 1, 2002
27
GB
The script below will take the value from tarsds and write it to a file by converting the list from one row list contains many cloumns to many rows contain one column.

'tarsds' could be = null, could be = one value or could be = more than one value.
Is there a way to simplify the script?

Thank you

tarsds="tarsds200.Z"
cnt=1
otarsds=""
while [[ $cnt != 0 ]]
do
ntarsds=`echo $tarsds | cut -d" " -f$cnt-$cnt`
if [[ $ntarsds = $otarsds ]]; then ntarsds=""; fi
if [[ $ntarsds != "" ]]; then
((cnt=cnt+1))
nntarsds=`echo $ntarsds | cut -d"." -f1-1 >>ntarsds1`
otarsds=$ntarsds
else
cnt=0
fi
done
# cat ntarsds1
Tarsds200


or

tarsds="tarsds200.Z tarsds100.Z tarsds400.Z"
cnt=1
otarsds=""
while [[ $cnt != 0 ]]
do
ntarsds=`echo $tarsds | cut -d" " -f$cnt-$cnt`
if [[ $ntarsds = $otarsds ]]; then ntarsds=""; fi
if [[ $ntarsds != "" ]]; then
((cnt=cnt+1))
nntarsds=`echo $ntarsds | cut -d"." -f1-1 >>ntarsds1`
otarsds=$ntarsds
else
cnt=0
fi
done
# cat ntarsds1
tarsds200
tarsds100
tarsds400

 
Hi,
Try this
Code:
LIST=$(cat tards)
for FILE in $LIST
do
    echo $(basename $FILE .Z) >> ntarsds1
done
 
If you are using a ksh or bash type shell, this function can be used instead of the external basename command:

Code:
# Using ksh pattern-matching operators, this function
# replaces the unix basename command.  If a 2nd argument
# exists, strip off the extension.
function basename {
   typeset v x

   v=${1##*/}
   x=${2#.} # get rid of the '.'
   v=${v%.$x}
   echo $v
}
 
bn=$(basename $var)
echo $bn # displays: ttest.c

bn=$(basename $var .c)
echo $bn # displays: ttest

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top