LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2011, 08:50 AM   #1
diamonty
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
How do I add tabs to multiple lines of text?


Hi
I have a very simple script to see if NTP is installed:

NTP=$(rpm -qa | grep ntp)
echo "$NTP"

The output is:
ntp-4.2.4p8-2.el6.i686
fontpackages-filesystem-1.41-1.1.el6.noarch
ntpdate-4.2.4p8-2.el6.i686

How can I get this output to tab all 3 lines so that it looks like this:
ntp-4.2.4p8-2.el6.i686
fontpackages-filesystem-1.41-1.1.el6.noarch
ntpdate-4.2.4p8-2.el6.i686

I have tried playing around with the /t option but it only tabs the 1st line, the same with using /t in the printf command, would anyone have done something like this before?
Thanks
 
Old 11-25-2011, 08:52 AM   #2
diamonty
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
The text under:
How can I get this output to tab all 3 lines so that it looks like this:

should have come out indented, ie with a tab, but the space must have been automatically removed...
 
Old 11-25-2011, 09:00 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Hi and welcome to LQ.

To correctly display your code preserving any formatting please wrap it in the CODE tags.

It'll make it more readable.
 
Old 11-25-2011, 10:38 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
First, the direct solution:

Code:
echo "$NTP" | sed 's/^/\t/'
Or an in-bash solution using parameter substitution with $'' (ansi-c style quoting):

Code:
echo $'\t'"${NTP//$'\n'/$'\n\t'}"
You might possibly need to enable shopt -s extquote first.

Or with printf:
Code:
IFS=$'\n'
printf "\t%s\n" $NTP
You have to set IFS to newline first, and then NOT quote the variable, so that the input is split by lines before printf reads it.

It might be more convenient later on if you stored the output in an array instead, rather than a scalar variable.

Code:
mapfile -t NTP < <( rpm -qa | grep ntp )  # mapfile was added in bash 4.0
printf "\t%s\n" "${NTP[@]}"

Last edited by David the H.; 11-25-2011 at 10:45 AM. Reason: IFS must be set before the printf line executes
 
1 members found this post helpful.
Old 11-25-2011, 05:40 PM   #5
diamonty
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks Sycamorex
I'll use tags the next time...
 
Old 11-25-2011, 05:43 PM   #6
diamonty
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up

David the H

The solution: echo "$NTP" | sed 's/^/\t/' is exactly what I need, thanks very much for your help and the alternative solutions,

Regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Add comma to end of lines in text file Johng Programming 9 08-21-2010 05:15 AM
match text over multiple lines Python donnied Programming 5 05-24-2009 10:18 AM
shelll script to replace and add lines in text cmontr Programming 11 02-29-2008 08:27 AM
Add multiple lines to sites-available automatic sinister1 Linux - Server 1 01-09-2008 06:46 AM
echo multiple lines of text twistedpair Linux - Software 9 08-08-2007 07:07 PM


All times are GMT -5. The time now is 06:36 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration