LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-09-2007, 03:08 PM   #1
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Rep: Reputation: 0
Cool Need to make multiple lines into one with no extra spacing with shell script.


I'm creating a database and can't find the following answer:

I'm copying and pasting addresses from an unstructured text file. The addresses are on two different lines. I paste the two lines into the shell program that asks multiple questions about a topic which builds a database. When I paste the address:

123 Fake Ave.
Eugene, OR 97402



The shell reads the first line of the address and then puts the second line in the next question (KEYW).

echo "What is the address?"
read ADDRESS

echo "What are the keywords?"
read KEYW

I want both lines in the address to become one and have the script read it on one line.

I also have the same problem when I try and add a description about the listing. Many of the descriptions I am copying and pasting from the unstructured text file have extra spacing and are on multiple lines. I don't know how to make the description on one line with no extra spaces. For instance..the description

The food is wonderful even though
I dont like italian food very much. The



needs to be all on one line with no space between "wonderful" and "even"

I'm sorry to ask such a newb question. I'm trying REALLY hard to learn shell scripting with awk, but I can't seem to figure very basic things out. Thanks so much!!!!!!!!!!!!!



 
Old 05-09-2007, 04:50 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
Quote:
Originally Posted by sunksullen
I want both lines in the address to become one and have the script read it on one line.
Have you tried piping all input through “tr -d '\n'” or similar?
Quote:
Originally Posted by sunksullen
For instance..the description

The food is wonderful even though
I dont like italian food very much. The



needs to be all on one line with no space between "wonderful" and "even"
This one doesn’t show up on the forum correctly (since multiple spaces are collapsed in html). I think you entered something like this:
Code:
The food is wonderful          even though
I dont like italian food very much.  The
In this case, pipe the input through “tr -s ' '”

For more info, man 1 tr
 
Old 05-09-2007, 05:56 PM   #3
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
Wow okay that works. Thanks. My only issue now is that when I do "tr -d '\n'" it does make the address on one line but they are right next to each other...

4064 Marshall AveEugene, OR 97402

Hopefully I'll figure it out. any suggestions? Thanks!


Quote:
Originally Posted by osor
Have you tried piping all input through “tr -d '\n'” or similar?
This one doesn’t show up on the forum correctly (since multiple spaces are collapsed in html). I think you entered something like this:
Code:
The food is wonderful          even though
I dont like italian food very much.  The
In this case, pipe the input through “tr -s ' '”

For more info, man 1 tr
 
Old 05-09-2007, 06:15 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
I’m sorry, I was being dumb. Of course you need to replace newlines by spaces (not just delete them!). Use this instead:
Code:
tr '\n' ' '
 
Old 05-10-2007, 02:46 PM   #5
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
Another problem....

I can get it to work only if I place the address or description in a text file. I NEED to be able to paste the description and address from a text file into to the terminal.

The problem is that the READ command is only reading one line at a time. I need it to be able to read multiple lines at once. Any ideas? Other than just editing the text file so that the address's and descriptions are on one line.

I need to find a way for READ to accept multiple lines when I paste it into the terminal, and then it needs to be sent through TR to make it one line. Thanks so much for help.


Quote:
Originally Posted by osor
I’m sorry, I was being dumb. Of course you need to replace newlines by spaces (not just delete them!). Use this instead:
Code:
tr '\n' ' '
 
Old 05-10-2007, 02:53 PM   #6
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
I just read about read has a -d option which causes read to continue to the first character of DELIM is read. I cant seem to figure out what the DELIM chracter is. Not seeing anything on google either.
 
Old 05-10-2007, 03:27 PM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
Code:
$ read -d "#" -p "What is the address [press # to continue]?" ADDRESS
What is the address [press # to continue]?123 Fake Ave.
Eugene, OR 97402
#$ echo "$ADDRESS"
123 Fake Ave.
Eugene, OR 97402
 
Old 05-10-2007, 03:29 PM   #8
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
okay sorry to keep posting, but I found out that "read -n 700" works perfectly...I'm pretty sure I can figure out now how to send the variable through tr...I'm just a little confused on how to combine the two different tr options together. I'll play around and see what happens.
 
Old 05-10-2007, 03:33 PM   #9
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
Oh thats even better!! Do you know how I can then send it through the two different tr commands: “tr -s ' '” and "tr '\n' ' '" ?

Quote:
Originally Posted by radoulov
Code:
$ read -d "#" -p "What is the address [press # to continue]?" ADDRESS
What is the address [press # to continue]?123 Fake Ave.
Eugene, OR 97402
#$ echo "$ADDRESS"
123 Fake Ave.
Eugene, OR 97402
 
Old 05-10-2007, 03:42 PM   #10
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
Nevermind. I figured out how to put it through tr.

read -d "#" -p "What is the address [press # to continue]?" ADDRESS
echo "$ADDRESS" > catburgler
tr '\n' ' ' < catburgler > catburger
tr -s ' ' < catburger > final


I'm SURE this isn't the best way of doing it though. I would much, much, much, rather not have to save the results in other files.
 
Old 05-10-2007, 05:20 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
Quote:
Originally Posted by sunksullen
echo "$ADDRESS" > catburgler
tr '\n' ' ' < catburgler > catburger
tr -s ' ' < catburger > final
How about:
Code:
echo "$ADDRESS" | tr '\n' ' ' | tr -s ' ' > final
P.S., What’s a catburger?
 
  


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
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 09:34 AM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
shell script for expanding lines in a file b123coder Programming 1 05-25-2006 04:51 AM
Script: splitting lines in multiple files and joining them timmay9162 Programming 28 04-14-2006 08:52 AM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM

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

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