LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script to output certain lines from a file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-output-certain-lines-from-a-file-4175536197/)

Regnets1 03-09-2015 11:27 AM

bash script to output certain lines from a file
 
I am trying to write a script to create a variable (string) out of specific lines from another file. The string will be used as headings for lists in another script. My plan is to create a variable "myvar" with the output from an awk command run against a file called banners.txt then I can call $myvar whenever it is appropriate in the larger script. additionally I can add/modify the banner file instead of having to comb through a large script to add or change echo commands.
So, here is my proof of concept script:
Code:

#!/bin/bash

path=/home/rstenger/

TESTFILE=$(<banners.txt)
MYVAR=$( awk 'NR > 6 && NR < 10' "$TESTFILE" )
#
echo ""
echo ""
echo $MYVAR
#
for i in {1..5}; do echo ""; done
echo $TESTFILE

exit

which produces this output:
Code:

sl7jump1~#->./test1.sh
awk: cmd. line:2: fatal: cannot open file `******************************************************
*                1st banner has 3 line              *
******************************************************
******************************************************
*                2nd banner has 3 line              *
******************************************************
******************************************************
*                3rd banner has 3 line              *
******************************************************' for reading (File name too long)








3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 1st banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 2nd banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names

What I was looking for is:
Code:

******************************************************
*                3rd banner has 3 line              *
******************************************************

I am very puzzled as to why I get the contents of the file in the awk error message, and the contents of my home directory as the variable.

Thanks,
Robert

pan64 03-09-2015 11:29 AM

it would be probably better:
TESTFILE=banners.txt

Regnets1 03-09-2015 11:41 AM

pan64, thanks for the quick response. I tried your suggestion

Code:

#!/bin/bash

path=/home/rstenger/

TESTFILE=banners.txt
MYVAR=$( awk 'NR > 6 && NR < 10' "$TESTFILE" )
#
echo ""
echo ""
echo $MYVAR
#
for i in {1..5}; do echo ""; done
echo $TESTFILE

exit

Here is the output.
Code:

sl7jump1~#->./test1.sh


3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd banner has 3 line 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names 3rd_floor_lab_ex3200s.txt banner banners.txt ban_test.sh base_configs Images lab_cyclades.sh lab_devices.v3.sh Projects savvis.lab_device_list Savvis_Lab-Domain-DNS.txt test1.sh vrf_names





banners.txt

Thanks again for the help it is greatly appreciated.

Regnets1 03-13-2015 09:11 AM

I worked around the issue I was seeing above. Instead of trying to read the entire file into a variable, I created my individual banners directly from the file like this

Code:

BANNER1=$( awk 'NR < 4 ' banners.txt)
This works fine for my purposes.


All times are GMT -5. The time now is 01:16 AM.