get the data in a file into single string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers get the data in a file into single string
# 1  
Old 06-13-2008
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
# 2  
Old 06-13-2008
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  
Old 06-14-2008
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
# 4  
Old 06-14-2008
thanks for the solution. my problem is solved. your method looks efficient.Smilie

------------------------------------------------------------
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  
Old 06-14-2008
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  
Old 06-15-2008
This works in ksh93
Code:
var=$(<file1)
var=${var//
/,}

print $var

and outputs
Code:
1,2,3,4,5

# 7  
Old 06-15-2008
It works.

Thank you all for your answers.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

2. Shell Programming and Scripting

Execute sequential files and store data in single file

1)In a particualr path i have a set of inputfiles like path:/defaultmis/MonthlyLoads/INFA_EXPORT_022013/map* example: 1)map_de 2)map_cod 3)map_feg ........and so on in above path there wil be nearly 15 to 20 files starting with map and in other path i have another file input file... (4 Replies)
Discussion started by: katakamvivek
4 Replies

3. Shell Programming and Scripting

Merge the data from two servers into a single file

Hi All, Need your inputs for the below. I have 2 different servers 611 & 610, where i would be running two scripts. And would would be running one script from 611 at every 4 hours to merge the data from the 2 servers into 2 files and send a mail. so below is the code snippet for 611: ... (3 Replies)
Discussion started by: ss_ss
3 Replies

4. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

5. Shell Programming and Scripting

Excel file attachment showing data in single column

Hi All, found similar posts, but not exatcly what i wanted. I have an text file like below and am trying to send mail as an excel file but when i get the excel file as the attachment, all the data is coming in the first column. I need below data in 4 columns. Unix file Name,ID,Trade,Date... (3 Replies)
Discussion started by: robinbannis
3 Replies

6. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

Data after a string in a file

Hi, I have a file with data as follows: 324dsadfasfgf23<xmlsssssssssssssssssssssssssssssssssssssss<fdf>dfsdf> i need all the data followed by the string <xml? Is it possible to retrieve it? (1 Reply)
Discussion started by: Vijay06
1 Replies

9. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies

10. Shell Programming and Scripting

How to copy data into a single file plus title

Hi Can anyone help me with the task below? Example: The contents in fileA.txt are: HELLO HOW DO U DO? The contents in fileA.txt are: HI I AM FINE. how to combine the data in 2 files into one with the format below? Case A-fileA.txt HELLO HOW DO U DO? Case B-fileB.txt (4 Replies)
Discussion started by: c0384
4 Replies
Login or Register to Ask a Question