The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I delete a data string from a .dat file in unix supergirl3954 UNIX for Dummies Questions & Answers 4 03-04-2008 04:56 AM
How to copy data into a single file plus title c0384 Shell Programming and Scripting 4 05-15-2007 06:15 AM
FILE to String data types cb.mark High Level Programming 4 12-01-2006 07:21 PM
how to find out if i have single string between tags umen Shell Programming and Scripting 4 07-14-2006 11:30 AM
concatenating static string to records in data file gillbates Shell Programming and Scripting 5 06-22-2006 03:22 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-13-2008
Registered User
 

Join Date: Jun 2008
Posts: 22
get the data in a file into single string

Hello everyone !

I need to get the data in a file into a string.
file1
1
2
3
4
5

I need to write a script where the file1 info is stored in a string (say variable s). So the output should be
1,2,3,4,5 or (1,2,3,4,5)
If i say
echo $s or print $s
output should be
1,2,3,4,5
or
(1,2,3,4,5)

Is this possible ? let me know what changes I need to do in the following script

while read file1
do
var=`echo $file1`
temp=`echo $temp","$var"`
echo $temp
done<file1
Reply With Quote
Forum Sponsor
  #2  
Old 06-13-2008
Registered User
 

Join Date: Jun 2008
Posts: 22
Got it!!

temp=`cat file1.dat | sed -n 1p`
while read value
do
var=`echo $value`
if [[ $var -ne "" && $var != $temp ]]
then
temp=`echo $temp,$var`
fi
done<file1.dat
echo $temp


Let me know if there is an efficient way to do this!
Reply With Quote
  #3  
Old 06-14-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

I think you should be pleased that you created a solution. There are many ways to approach problems in *nix. For long files, while-read loops can be very time-consuming. You can often use utilities to process an entire file. Here is one example with your situation:
Code:
#!/bin/bash -

# @(#) s2       Demonstrate collapse of all lines in file to a single line.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) tr sed
echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

temp=$( tr '\n' ',' <$FILE )

echo
echo " Contents of string after tr:"
echo $temp

temp1=${temp/%,/}
echo
echo " Contents of string after clean-up:"
echo $temp1

temp2=$( echo $temp | sed 's/,$//' )
echo
echo " Contents of cleaned string, sed alternative:"
echo $temp2

exit 0
Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
tr (coreutils) 5.2.1
GNU sed version 4.1.2

 Input file data1:
1
2
3
4
5

 Contents of string after tr:
1,2,3,4,5,

 Contents of string after clean-up:
1,2,3,4,5

 Contents of cleaned string, sed alternative:
1,2,3,4,5
The tr translates characters in an entire file. The subsequent special assignment with the variable is peculiar to some shells, so I provided a alternative sed step to remove the final ",".

See man pages for details.

For the size of problems such as this is likely to be, most solutions are probably the same in efficiency. However, it is useful to know about other methods in general ... cheers, drl
Reply With Quote
  #4  
Old 06-14-2008
Registered User
 

Join Date: Jun 2008
Posts: 22
thanks for the solution. my problem is solved. your method looks efficient.

------------------------------------------------------------
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) tr sed
echo
--------------------------------------------------------------
But the above did not give any results except :
(Versions displayed with local utility "version")

Is this just printing version i am using ?
Reply With Quote
  #5  
Old 06-14-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

The command version is something that I wrote to document versions of commands that I use in scripts. I don't expect that you would have it, which is why the first part of the statement causes the remainder of the statement to be skipped. It works on my systems, but nothing is produced when version is not present in your path ... cheers, drl
Reply With Quote
  #6  
Old 06-15-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
This works in ksh93
Code:
var=$(<file1)
var=${var//
/,}

print $var
and outputs
Code:
1,2,3,4,5
Reply With Quote
  #7  
Old 06-15-2008
Registered User
 

Join Date: Jun 2008
Posts: 22
It works.

Thank you all for your answers.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0