-1

I want to print uvuveve at finish each line, for each output into while statement, as below:

var1;var2;uvuveve
var1;var2;uvuveve
var1;var2;uvuveve
var1;var2;uvuveve
var1;var2;uvuveve

This is my code:

var1="somedata..."
var2="anotherdata..."
while read -u3 w1; read -u4 w2; do
echo "$w1;$w2" >> $file
done 3<<< "$var1" 4<<<"$var2"

var1, var2 print multiple occurrences of a file, so there are many outputs by these variables. I tried to add uvuveve word like this way:

var1="somedata..."
var2="anotherdata..."
string="uvuveve"
while read -u3 w1; read -u4 w2; read -u5 w3; do
echo "$w1;$w2;$w3" >> $file
done 3<<< "$var1" 4<<<"$var2" 5<<<"$string"

Essentially, I need a print of the word in each line, for each occurrence founded.

Adding Details:

Var1 & var2 retrieve lines of occurrences that a file contains, and then put into variables

Literally the lines are:

var1=$(grep -A12 -B12 "$tofind" $findlogs | grep Date | cut -c 51-65 | sed -e 's! !/!g')
var2=$(grep -A12 -B12 "$tofind" $findlogs | grep Date | cut -c 67-71 | sed -e 's/://g')
  • 2
    Does replacing echo "$w1;$w2;$w3" with echo "$w1;$w2;$string" solve the problem? If not, please provide enough input data (and corresponding desired output) so that we can understand what you are actually trying to do. See How to create a Minimal, Complete, and Verifiable example. – John1024 Mar 28 at 20:02
  • Adding the $w3 varaible (string) only add one time the string. – Mareyes Mar 28 at 20:04
  • Yes, that is why I suggested that you change the code as per my comment. – John1024 Mar 28 at 20:05
  • 1
    Does the data in your two variables var1 and var2 come from a file? Could you give a real example of input and output? – Kusalananda Mar 28 at 20:09
  • @Kusalananda here goes. – Mareyes Mar 28 at 20:17
1

Since you just have plain text in variables, redirecting them into a while-read loop seems ridiculously over-complicated.

What's wrong with

var1="somedata..."
var2="anotherdata..."
string="uvuveve"

echo "$var1;$var2;$string"
# or
printf '%s;%s;%s\n' "$var1" "$var2" "$string"

If you actually have files, then you do need a loop. If we have

$ cat file1
a
b
c
$ cat file2
1
2
3

then

string="uvuveve"
while IFS= read -r -u3 a; read -r -u4 b; do 
    printf '%s;%s;%s\n' "$a" "$b" "$string"
done 3<file1 4<file2
a;1;uvuveve
b;2;uvuveve
c;3;uvuveve

Going back to your edit: use Process Substitutions

# don't repeat yourself
dates=$(grep -A12 -B12 "$tofind" $findlogs | grep Date)
string="uvuveve"
while IFS= read -r -u3 a; read -r -u4 b; do 
    printf '%s;%s;%s\n' "$a" "$b" "$string"
done 3< <(echo "$dates" | cut -c 51-65 | sed -e 's! !/!g') \
     4< <(echo "$dates" | cut -c 67-71 | sed -e 's/://g')
1

Does this do what you want?

#!/usr/bin/env bash
IFS=$'\n'; var1=( $( ... your command here ... ) );
IFS=$'\n'; var2=( $( ... your command here ... ) );

for i in ${!var1[@]}; do
  echo ${arr1[$i]};${arr2[$i]};uvuveve
done

It assumes that var1 and var2 have the same amount of lines.

Basically:

  1. We turn your command into a variable that is an array. source
  2. Then we iterate the array (source) adding the variables together and your extra string.
  • I have other script with arrays, and your solution was useful. Thanks, Kyle! – Mareyes Mar 29 at 16:17
-1

Tried with below command

$ j="praveen"
$ k="ajay"
$ m="abhi"

$ awk -v j="$j" -v k="$k" -v m="$m" '{print j"\n"k"\n"m;exit}'
praveen
ajay
abhi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.