![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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! |
|
#3
|
||||
|
||||
|
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
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 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 |
|
#4
|
|||
|
|||
|
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 ? |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
This works in ksh93
Code:
var=$(<file1)
var=${var//
/,}
print $var
Code:
1,2,3,4,5 |
|
#7
|
|||
|
|||
|
It works.
Thank you all for your answers. |
|||
| Google The UNIX and Linux Forums |