stuck on first script with sed issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stuck on first script with sed issue
# 1  
Old 08-18-2011
stuck on first script with sed issue

Hi,

I'm developing my first bash script and have made good progress but stuck at this point.

I've run sed on a text file to extract some data and have saved it into a variable e.g.

$blah

the variable contains emails as follows e.g.
Code:
<a@b.com> <b@c.com>

I'm now trying to edit the values in this variable so that if there is more than one email address, then they should be separated by commas instead of spaces.

I've currently got this far:
Code:
#$num is how many words there are in the variable
if (($num==1))
then
#insert sed command to put single mail address in ok (i will add this later)
else
#below i'm trying to substitute the spaces for commas but it's only adding #a comma to the last email address
blahnew=$(echo $blah | sed 's/[^ ].*/&,/g')
echo $blahnew
fi

so i get:
Code:
 <a@b.com> <b@c.com>

,

Would appreciate any help please.

Last edited by Franklin52; 08-19-2011 at 03:07 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-18-2011
Just change the space to comma:
Code:
NewBlah=$(echo $blah | sed 's/ /,/')

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 08-18-2011
Hi, that does seem like a great idea but it didn't work when I tried it, it left the spaces in. Smilie

Quote:
Originally Posted by Shell_Life
Just change the space to comma:
Code:
NewBlah=$(echo $blah | sed 's/ /,/')

---------- Post updated at 03:06 PM ---------- Previous update was at 03:01 PM ----------

Ah great, it did work!

it just needed the g flag at the end.! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stuck process when using sed command

$BIN/sql.py "select * from reporting.V_AMSNB_OPT_PB_LOAD" | sed -n "2,$ p" | sed "s/ /,/g" > $DATADIR/OPT_PB_LOAD_AMSNB.csv if The processes that get created from the above code gets stuck...as in the processes get created but then never completes. (or at least we assume it never completes... (1 Reply)
Discussion started by: mimiyj
1 Replies

2. Shell Programming and Scripting

sed command issue in script

Hi, I am using sed command to extract data from my log file for a certain time interval. From and To "time" are my input arguments. Now if i use the sed command on command line. I get the desired results and If i use it in script.It fails. sed command as command line: sed -n '/04-Mar-2015... (6 Replies)
Discussion started by: oberoi1403
6 Replies

3. Shell Programming and Scripting

sed script to parse logs issue

I have this script to parse some logs: #!/bin/bash id=$1 shift sed "/(id=$id)/,/^$/!d" "$@" Usage: ./script.sh 1234 logfile The logs have an empty line before the logged events/timestamps -- most of the time. And this is my issue, since when there is no empty line, it will catch things... (4 Replies)
Discussion started by: KidCactus
4 Replies

4. Shell Programming and Scripting

Error in script, STUCK :(

Hi All, I am beginner in scripting. I wrote a simple script to perform some task. It seem to have some error in command line, Kindly somebody help. Thanks #!/bin/bash date cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch function pause(){ read -p "$*" } FILE=$1... (22 Replies)
Discussion started by: nrjrasaxena
22 Replies

5. Shell Programming and Scripting

Sed if statement (totally stuck) apache

I have a irritating problem with a "if"-statement or what you should call it, in sed. I would love some help here since I am very, very stuck. I have this statement that I want to do: if line contains a: do this if none of the lines contained a: do this The problem with this... (8 Replies)
Discussion started by: hjalle
8 Replies

6. Shell Programming and Scripting

I am stuck in my script

Hi All I have script that find 777 dir with specific extension like .php .Now after finding all 777 directory i will place in httpd.conf using a directory directive ,Now i was not do that,if directory entry exitst in httpd.conf then script ignor it dont show me at stdout else if it dont find... (2 Replies)
Discussion started by: aliahsan81
2 Replies

7. UNIX for Dummies Questions & Answers

stuck with a script

Hi There I am pretty new to UNIX and have only been using it from a basic point of view,I now want to start using it and learning more , have got a whole lot of books and documentation from the web and am slowly learning.I have written a get script in windows :- lcd E:\MAIN\PRO\FILES\MAINDB... (1 Reply)
Discussion started by: FOCKER
1 Replies

8. Shell Programming and Scripting

Issue with sed in script

I have a loop in a script that is given me an error but, when I do it on the command line it works perfectly. The sed statement has to use the variables from a file so the file is partitioned correctly. I am running on HP: <Begin error>: + cat /u01/bteam/CNAM/1121/.partition + read line + +... (3 Replies)
Discussion started by: bthomas
3 Replies

9. Shell Programming and Scripting

simple script but am stuck

hey i am a bit stuck here. i just started work experience and i need to create a simple script which delete all files in a specify folder which are older then 2 days. :mad: i tried but it never works!!! anyone! i dont know much but unix since i mostly work on NT here but i dont wanna disapoint my... (2 Replies)
Discussion started by: GermanJulian
2 Replies

10. Shell Programming and Scripting

stuck on ksh script

hi, i need help to write script in korn shell that will display info. on system paging, system process table.system file table inf. thank you (1 Reply)
Discussion started by: neer45
1 Replies
Login or Register to Ask a Question