LinuxQuestions.org
Help answer threads with 0 replies.
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 10-06-2010, 09:50 AM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Rep: Reputation: 35
Keeping a constant space between 2 words in a file


Hi all,

I have a text file that gets produced at the end of the script being run.

For this example the text file will produce the following:

Quote:
THE COW
THE DOG
THE CAT
THE HORSE
In the script I am using either echo or printf to print on the screen each line and then it is doing a test and produces a good or bad result.

Another example:

Quote:
THE COW -- IS HOME
THE DOG -- IS HOME
Each whitespace between the -- are 5 spaces. How can I get them to be in-line and formatted when it gets to more words, when THE HORSE arrives.

Example:

Quote:
THE COW -- IS HOME
THE DOG -- IS HOME
THE CAT -- IS HOME
THE HORSE -- IS HOME <-- This has only 3 whitespaces but is still formatted.
I hope you know what I mean. I just dont want the report the script produces to turn out all messy and horrible.

Many thanks for any help.
 
Old 10-06-2010, 09:50 AM   #2
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
Damn just seen that this has not kept the format I typed the examples in

Anyhow I hope you know what I mean. If not I will re-explain for you.

Regards
 
Old 10-06-2010, 09:54 AM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I, for one, do not understand.

I don't understand what is the "formatting" you're speaking of. Can you perhaps show us the output looking like you don't want it to, and then to compare, also show us the same output, only formatted like you DO want it to look?

Using the code tags as you have, should preserve any whitespace you insert. So, show us what exactly you want in the output, compared to what you don't want.

Thanks!
 
Old 10-06-2010, 10:17 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Change quote for code tags will keep the spacing.
 
Old 10-06-2010, 10:18 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by grail View Post
Change quote for code tags will keep the spacing.


Nice catch!

*opens eyes a bit wider*
 
Old 10-06-2010, 10:28 AM   #6
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
Hi all,

OK I have replaced with code tags now:

Example list:
Code:
THE COW
THE DOG
THE CAT
THE HORSE

In the script I am using either echo or printf to print on the screen each line and then it is doing a test and produces a good or bad result.

Another example:

Code:
THE COW     --     IS HOME
THE DOG     --     IS HOME
Each whitespace between the -- are 5 spaces. How can I get "IS HOME" to stay in line with the above line even though more/extra characters are present in the text file?

Completed Example but not in line:

Code:
THE COW     --     IS HOME
THE DOG     --     IS HOME
THE CAT     --     IS HOME
THE HORSE     --     IS HOME
As you can see "THE HORSE" has an extra 2 characters, HOR[SE]

But I want the "IS HOME" to stay in line with the column.

How I want it:

Code:
THE COW     --     IS HOME
THE DOG     --     IS HOME
THE CAT     --     IS HOME
THE HORSE   --     IS HOME
Many thanks for any help.
 
Old 10-06-2010, 10:35 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{printf "%-10s     %s     IS HOME\n" ,$0,"--" }' file
 
Old 10-06-2010, 11:07 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
printf formatting.

Here's a simple loop demonstrating the use of some printf formatting:
Code:
location="IS HOME"; sep="--"

for i in "THE COW" "THE DOG" "THE BOAT" "THE AARDVARK"; do
    printf "%-14s %s %s\n" "$i" "$sep" "$location"
done
So, the printf statement expects 3 arguments:
%-14s and %s and %s followed by the newline \n.

The formatting results in the following:
%-14s = makes a padded string 14 chars long, left justified.
%s = makes a string exactly as is given to printf.

Output:
Code:
root@reactor: for i in "THE COW" "THE DOG" "THE BOAT" "THE AARDVARK"; do     printf "%-14s %s %s\n" "$i" "$sep" "$location"; done
THE COW        -- IS HOME
THE DOG        -- IS HOME
THE BOAT       -- IS HOME
THE AARDVARK   -- IS HOME
root@reactor:
You can play around with the formatting codes to shift the padding left or right (left or right justify). And you should be able to easily enough figure out at least one way to move the "IS HOME" further to the right if you want.
There are man pages for printf, but they don't go into great detail about all the formatting codes you can possibly use. Search for a good online resource for this if you need more.

Good luck!
 
Old 10-06-2010, 01:23 PM   #9
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
Everyone thank you very much. You have solved my problem and taught me something new in the process as it should.

Keep up the good work!!!

Thanks again!
 
  


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
Removing white spaces between words and joining the words in a given format Priyabio Linux - General 4 08-20-2009 08:42 AM
Keeping DATE setting constant in FreeBSD 7.0 paragkalra *BSD 5 07-30-2008 12:02 PM
Replacing words in a text file Raghavan_sat Programming 3 05-27-2008 04:11 PM
how to find the head file where a Constant was defined greghua Programming 1 08-12-2005 04:50 PM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 08:48 PM


All times are GMT -5. The time now is 12:00 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