LinuxQuestions.org
Help answer threads with 0 replies.
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 01-18-2011, 03:18 PM   #1
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Rep: Reputation: 0
Question Bash concatenating string to variable


I have a program that loops over each word in a sentence. I need to append a constant to the beginning and end of each word. It works up until the last word on the line. I am stumped. Thanks for any suggestions

Code:
  for w in $(echo $i | tr "," "\n")
        do    
         echo $w  
         w='a'$w'a'
         echo  $w
        done
Output:
2011-18-01
a2011-18-01a
40
a40a
A
aAa
DWELLING
aDWELLING
 
Old 01-18-2011, 03:34 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
What are you giving it as $i?

Code:
#!/bin/bash

i="2011-18-01 40 A DWELLING"
for w in $(echo $i | tr "," "\n")
do    
   w='a'$w'a'
   echo  $w
done
works for me, printing

Code:
a2011-18-01a
a40a
aAa
aDWELLINGa
 
Old 01-18-2011, 03:57 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
If the text was edited in Windows there could be a carriage return character at the end of each line.
\n is the end of line code for Linux
\r\n is the end of line code for Windows
\n represents a newline character
\r represents a carriage return character

Code:
w=DWELLING$'\r'        # $'\r' creates a carriage return in bash
echo $w
w='a'$w'b'
echo  $w
echo  $w | tr -d '\r'  # tr can delete carriage returns

DWELLING
bDWELLING
aDWELLINGb
 
Old 01-18-2011, 11:27 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
# echo "2011-18-01 40 A DWELLING"  | ruby -e 'puts gets.split().map{|x|"a"+x}'
a2011-18-01
a40
aA
aDWELLING
 
Old 01-19-2011, 07:04 AM   #5
abercrombieande
LQ Newbie
 
Registered: Oct 2010
Location: Utica NY
Distribution: Mint, Puppy
Posts: 11

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by Kenhelm View Post
If the text was edited in Windows there could be a carriage return character at the end of each line.
Code:
echo  $w | tr -d '\r'  # tr can delete carriage returns
Thanks it was the \r.
 
  


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
[SOLVED] Concatenating an integer to a string mohabic Programming 5 10-19-2010 01:34 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 03:07 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
Problem concatenating a string in perl Singist Programming 4 03-22-2006 06:15 PM


All times are GMT -5. The time now is 01:39 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration