سؤال

I have been reading tutorials and I need to use cat and paste functions (for making a kind of array, table) the thing is that all tutorials use these commands on files, and reading files from the hard drive are making my task very very slow, so I wanted to know how to use them with variables, I tried and got erroneous results, so maybe you can help me with the sintaxis using that.

basically I want to make a table in a variable like this:

00001 Tacos
00023 pizza
00076 burger
00103 chopsuey
00167 burrito
01034 Tamales

And I'm getting every element after executing a program and taking specific data from the output, so I'm getting:

  1. 00001
  2. Tacos
  3. 00023
  4. pizza ....

You dont have to do the program, just wanted to be sure that cat and paste are the ones to use here and their syntax, if they aren't I accept any suggestion.

Sorry, I maybe did not explain myself, sorry about that, I have a and b, both variables, a is 00001 and b y is Tacos, then i want them to merge together and store them in a variable, then do the same again but to put them in a new line. Sorry for the misundertanding.

at the end i want a variable with this in it:

00001 Tacos
00023 pizza
00076 burger
00103 chopsuey
00167 burrito
01034 Tamales

Thanks!

هل كانت مفيدة؟

المحلول

If you have a tool only working on files as input (e. g. diff or paste), you can use the <(…) notation to create a fake file whose contents is created by a shell command:

cat <(echo "hello world")

This will print hello world. The fake file lacks some of the abilities of on-disk files; it cannot be seeked for instance. So programs which want to seek a specific position in the file, for instance to read the file twice, will fail on this. But for your case, it should suffice and you can use stuff like this:

paste <(echo "$a") <(echo "$b")

For your case more concrete:

cat input.txt | {
  x=''
  y=''
  while read a
  do
    read b
    x=$(echo "$x"; echo "$a")
    y=$(echo "$y"; echo "$b")
  done
  paste <(echo "$x") <(echo "$y")
}

(I'm assuming the input to be this here:)

00001
Tacos
00023
pizza
00076
burger
00103
chopsuey
00167
burrito
01034
Tamales
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top