LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2015, 01:44 PM   #1
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Rep: Reputation: Disabled
SED returns contents of directory when searching a file


I am writing a bash script with several menus. Instead of bloating my script with a bunch of echo commands to print banners above the different menus, I created a file which contains all my banners. I am trying to use sed (tried awk first but experiencing the same issue) to grab specific lines of the file and place them in a variable in the script. Then I call the variable once for my banner. What is happening is the correct lines from the file along with the contents of my directory are being placed in the variable.
Here is how I am assigning the lines of the file to a variable:
Code:
BANNER1=$(sed -n 1,3p banners.txt)
BANNER2=$(sed -n 4,6p banners.txt)
BANNER3=$(sed -n 7,9p banners.txt)
BANNER4=$(sed -n 10,12p banners.txt)
BANNER5=$(sed -n 13,15p banners.txt)
BANNER6=$(sed -n 16,18p banners.txt)
BANNER7=$(sed -n 19,21p banners.txt)

Contents of the file:
>cat -n banners.txt
     1  ******************************************************
     2  *                1st Floor Lab Device                *
     3  ******************************************************
     4  ******************************************************
     5  *                3rd Floor Lab Devices               *
     6  ****************************************************** 
     7  ******************************************************
     8  *                DCK Lab Devices List                *
     9  ******************************************************
    10  ******************************************************
    11  *                All Lab Devices List                *
    12  ******************************************************
    13  ******************************************************
    14  *                Lab Terminal Servers                *
    15  ******************************************************
    16  ******************************************************
    17  *                    Main - Menu                     *
    18  ******************************************************
    19  ******************************************************
    20  *               Classic Device List                  *
    21  ******************************************************

and output

****************************************************** banners.txt History.log lab_devices.v3.sh labhostlist.txt lab_ips_3-4-15.txt old-lab_devices.v2.sh Lab Terminal Servers banners.txt History.log lab_devices.v3.sh labhostlist.txt lab_ips_3-4-15.txt old-lab_devices.v2.sh ******************************************************
What should have been displayed is

******************************************************
* 3rd Floor Lab Devices *
******************************************************

I am using an echo command to output the variable
[code]
echo $BANNER3

[\code]

I get the same results using AWK in this manner:
Code:
BANNER1=$( awk 'NR < 4 ' banners.txt)             
BANNER2=$( awk 'NR > 3 && NR < 7 ' banners.txt )  
BANNER3=$( awk 'NR > 6 && NR < 10 ' banners.txt ) 
BANNER4=$( awk 'NR > 9 && NR < 13 ' banners.txt ) 
BANNER5=$( awk 'NR > 12 && NR < 16 ' banners.txt )
BANNER6=$( awk 'NR > 15 && NR < 19 ' banners.txt )
BANNER7=$( awk 'NR > 18 ' banners.txt )
I have tried several variations with spaces and single/double quotes but nothing seems to help. I just can not figure out what i am missing.
 
Old 03-16-2015, 01:53 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You need to use quotes in your echo command so that the newlines will be preserved (not converted into spaces) and to keep the shell from expanding the "*" wildcards.
Code:
echo "$Banner3"
I know you said you tried quotes, but without seeing exactly what you tried, all I can suggest is what should work.

Last edited by rknichols; 03-16-2015 at 01:55 PM.
 
Old 03-16-2015, 02:14 PM   #3
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
rknichols, Thank YOU! That was exactly what I was missing! I spent a lot of time making changes to the sed command and none on how I was outputing the variable. The double quotes fixed it right up!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed -- replacing a string in a file with the contents of another file? CGP314 Linux - Newbie 10 06-17-2010 04:29 PM
cat FILE|sort +1 -n Returns ... +1: No such file or directory musician Linux - Newbie 1 07-23-2009 01:28 PM
searching contents of .class file within jar (for strings) wakeboarder3780 Linux - Newbie 4 02-17-2009 01:49 PM
Inserting file contents into Sed Command ewingtux Programming 4 11-19-2007 06:59 PM
List the contents of a directory to a file MasterC Linux - General 19 11-26-2002 10:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration