How in BASH in a variable to bring the entire contents of a text file so that you can then work with this variable through substring?

For example, I have a text.txt file with lines in it:

<hr> Hello im text now Im text, you know it Or not? I am Text Hi, how are you Text. How do you feel? 

and so on ... I need, if in the line there is the word "text" to find out which account this character is, and to select a certain number of characters from it and write it to another file.

  • one
    The easiest way to VAR=$(cat text.txt) - KoVadim
  • Thanks a lot) - Ki Mi
  • @ kimi This is an assignment from some course or work? Barakin gave you a tip that it is better for the shell (bash in your case) to not do this; there are a lot of right tools for this. - Hellseher

2 answers 2

I need, if the line has the word "text" to find out which account this character is, and to select a certain number of characters from it and write it to another file.

that is, in fact, you need to get a few characters from the lines after the text .

means of the shell (although the program bash , at least any other), of course, can be done, but it will turn out quite cumbersome.

It is better to use programs that are more suitable for manipulating text.

for example, the sed program:

 $ sed -rn 's/.*text(.{4}).*/\1/p' файл 

for your example, the command will print 4 characters (after the text ) from each line where text is found:

  now , yo 

save the stdout program to another file:

 $ программа опции и аргументы > другой.файл 

    VVV = `cat ...`
    Only you need not this. Here you need line-by-line reading in a loop.

    • If it is not difficult, I will be very grateful for your help. You can write an example of how to achieve this through the cycle. I am new to Bash, and so far I don’t understand much .. - Ki im
    • The line is read command read. I haven't finished reading it yet. I also do not know what kind of bash;) specie - Smolyanoye Chuchelko