0

I'm working on a bash script to backup MySQL. I need to read from a file a series of strings and pass them to a variable in my script. Example:

Something like this will be in the file (file.txt)

database1 table1
database1 table4
database2
database3 table2

My script needs to read the file and put these strings in a variable like:

#! bin/bash
LIST="database1.table1|database1.table4|database2|database3.table2"

Edit. I changed my mind, now I need this output:

database1.table1.*|database1.table4.*|database2*.*|database3.table2.*
2
  • 2
    And what have you tried so far? Commented Apr 30, 2014 at 14:18
  • 1
    You need to fix the shebang too, it'd be #!/bin/bash Commented Apr 30, 2014 at 15:49

4 Answers 4

6

You could use tr to replace the newlines and spaces:

LIST=$(tr ' \n' '.|' < file.txt)

Since the last line of the input file is likely to contain a newline by itself, you'd need to get rid of it:

LIST=$(tr ' ' '.' < file.txt | paste -sd'|')
Sign up to request clarification or add additional context in comments.

Comments

5

Using awk:

s=$(awk '{$1=$1}1' OFS='.' ORS='|' file)
LIST="${s%|}"

echo "$LIST"
database1.table1|database1.table4|database2|database3.table2

4 Comments

This is great!!. I forgot to mention one thing. I need to also add some wildcards to the output string. The conditions are: if we read database2 the output should be database2*.* and if we read a database and a table the output should be database1.table1.* The final result I'm looking for is: database1.table1.*|database1.table4.*|database2*.*|database3.table2.* Please help :)
That actually changes the very nature of the problem. If you open a new question linking to this one I will make an attempt to solve it.
May I know what's wrong with the answer since you unaccepted it.
3

bash (version 4 I believe)

mapfile -t lines < file.txt           # read lines of the file into an array
lines=("${lines[@]// /.}")            # replace all spaces with dots 
str=$(IFS='|'; echo "${lines[*]}")    # join the array with pipe
echo "$str"
database1.table1|database1.table4|database2|database3.table2

mapfile -t lines < file.txt
for ((i=0; i<${#lines[@]}; i++)); do
    [[ ${lines[i]} == *" "* ]] && lines[i]+=" *" || lines[i]+="* *"
done
str=$(IFS='|'; echo "${lines[*]// /.}") 
echo "$str"
database1.table1.*|database1.table4.*|database2*.*|database3.table2.*

2 Comments

You don't need the auxiliary variable since the purpose is only to print to stdout. In a subshell (IFS='|'; echo "${lines[*]}") is enough. Actually, you don't even need the auxiliary lines either: mapfile -t lines < files.txt; (IFS='|'; echo "${lines[*]// /.}")
How could I get this output instead database1.table1.*|database1.table4.*|database2*.*|database3.table2.* - Please notice the .* vs *.* - In other words: If there is only database with no table i need to add *.* at the end and when there is a database.table i need to add .*
2

You can just replace the new lines with a charater that you need using sed, if it doesn't occur in the data.

For example

FOO=$(sed '{:q;N;y/ /./;s/\n/|/g;t q}' /home/user/file.txt)

1 Comment

How could I get this output instead database1.table1.*|database1.table4.*|database2*.*|database3.table2.* - Please notice the .* vs *.* - In other words: If there is only database with no table i need to add *.* at the end and when there is a database.table i need to add .*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.