Skip to content
Advertisement

Linux read file line by line, print regex and print everything between two patterns

I made this script:

for i in  `cat PDV_List`
do echo $i
ssh $i /tmp/checkutilisateur.sh
done > hc_connesse
awk '/^+-------------+/{PTN=1} /^(/{PTN=0} PTN' hc_connesse
echo "       "

Output of hc_connesse is:

SE401 
INGRES TERMINAL MONITOR Copyright 2011 Actian Corporation
Ingres Linux Version II 10.1.0 (a64.lnx/126)GPL login 
Fri May 22 13:47:36 2020 
Enter g to execute commands, "help helpg" for help, q to quit

continue
* Executing . . .


+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 
|num          |nom                           |mdp   |numpro|numcai|numcle       |coffre|consig|datemodif                |matcv |cre_hot                  |deb_ctrl                 |fin_ctrl   |top_vi|
+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 
| some data here
+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 
(12 rows) continue
*  Your SQL statement(s) have been committed.

Ingres Version II 10.1.0 (a64.lnx/126)GPL logout 
Fri May 22 13:47:36 2020

I’m not able to print SE401 from hc_connesse.

The query is made on 45 different server and without putting the SE04 before the table will be impossible to understand which data are from where.

Tried another For cycle inserting another awk before the other.

Tried putting the two awk on one line.

Most probably i wrote something wrong because any change i make to the script, console is like awaiting input.

Do you have any idea on how to print the SE04?

Advertisement

Answer

It is very hard to guess the relationship between the body of your question and its title. If I understand you correctly, you want to print the first line (SE followed by digits) AND the output of the database query.

Try:

for i in  `cat PDV_List`
do echo $i
  ssh $i /tmp/checkutilisateur.sh
done | tee hc_connesse | awk '/^SE/ || /^+/ || /^|/'
echo "       "

What awk does here is to print every line starting with SE, + or |. If you want something else, please edit your question to make it understandable.

It is probable that you don’t want to keep a copy of hc_connesse. In that case, just take away the string tee hc_connesse | from the command above.

Output:

SE401 
+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 
|num          |nom                           |mdp   |numpro|numcai|numcle       |coffre|consig|datemodif                |matcv |cre_hot                  |deb_ctrl                 |fin_ctrl   |top_vi|
+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 
| some data here
+-------------+------------------------------+------+------+------+-------------+------+------+-------------------------+------+-------------------------+-------------------------+-------------------------+------+ 

Advertisement