Content of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Content of variable
# 1  
Old 08-22-2008
Content of variable

I have a variable that contains filenames like this:

variable="file_1.extension<blank>file_2.extension<blank>file_3.extension<blank>file_4.extension and so on"

How can I make filenames to be separated by newline: (I tried Sed but it didn't worked well)

file_1.extension
file_2.extension
file_3.extension
file_4.extension

I don't want another temporary file and want to do this with the variable:

while read filename; do

some stuff

done<< END
$variable
END

Thank you for suggestions!
# 2  
Old 08-22-2008
echo $variable | tr " " "\012"

or

for f in $variable; do echo $f; done
# 3  
Old 08-22-2008
Code:
# cat file1.txt
file_1.extension file_2.extension file_3.extension file_4.extension


# sed 's/ /\n/g' file1.txt
file_1.extension
file_2.extension
file_3.extension
file_4.extension

# 4  
Old 08-22-2008
thanks, works like a charm Smilie
# 5  
Old 08-22-2008
Quote:
Originally Posted by Ikon
Code:
# cat file1.txt
file_1.extension file_2.extension file_3.extension file_4.extension


# sed 's/ /\n/g' file1.txt
file_1.extension
file_2.extension
file_3.extension
file_4.extension

I had the very same sed.. so there has to be another problem :-(
# 6  
Old 08-22-2008
echo $variable | tr ' ' '\n'
# 7  
Old 08-23-2008
Why not simplify the problem and just use a while loop instead of a here-document i.e.
Code:
variable="file1 file2 file3"

for filename in $variable
do
   .......
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacement of variable by their content in a file

Dear all, I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request. For example: $ echo $SHELL /bin/bash $ cat dae2.txt DELETE FROM ... (11 Replies)
Discussion started by: dae
11 Replies

2. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

3. Shell Programming and Scripting

Adding Content to Variable (bash)

is this possible? its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number. For instance: VAR=Tommy for all in $(blah blah) do ... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Loop over variable content

Hello all, I do have a variable containing one line like this: Waiting for job XXXXXX to start I needed to get the 'XXXXXX' literal, so I did the following: job_interno=`echo $log_exec | sed 's/.*Waiting for job \(*\).*/\1/' ` #other stuff Now, my variable is have more... (5 Replies)
Discussion started by: manolain
5 Replies

5. Shell Programming and Scripting

How to put content of file into a variable?

For example, I have a simple text file note: this a note a simple note a very very simple notewhen I use this command, temp=$(cat "note.txt")then I echo temp, the result is in one line. echo $temp note: this a note a simple note a very very simple noteMy variable doesn't have newline. How... (7 Replies)
Discussion started by: 14th
7 Replies

6. Shell Programming and Scripting

how to get the specified content of the text into a variable.

I am having one string like ./usr1/Server/temp/app.env ./usr1/Server/temp/upp/app.env ./usr1/Server/ORIG_temp/app.env ./usr1/Server/ORIG_temp/upp/app.env ./usr1/Server/work_temp_40/app.env ./usr1/Server/work_temp_40/upp/app.env ./usr1/fd/app.env ./usr1/PurgeArchive/app.env ./usr1/bm/bin/app.env... (6 Replies)
Discussion started by: dineshmurs
6 Replies

7. Shell Programming and Scripting

addressing variable content...

I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 But how can I... (3 Replies)
Discussion started by: sobolev
3 Replies

8. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies

9. Shell Programming and Scripting

Content of Content of a variable!

I got a sample BASH script like this : $ cat test MYVAR=$1 DUMMY1="This is tricky" DUMMY2=24 echo $ $ ./test DUMMY1 ./test: line 5: This is tricky: syntax error in expression (error token is "is tricky") **I was expecting the output as "This is tricky", ah! but no luck **But... (2 Replies)
Discussion started by: jaduks
2 Replies

10. Shell Programming and Scripting

How to replace a variable content

Hi, variable1="This is a car" Now I want to replace the content of variable1, "car" to "dog". Is there any simple command I can use. Thanks. Joseph (4 Replies)
Discussion started by: josephwong
4 Replies
Login or Register to Ask a Question