Adding word in blank space


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding word in blank space
# 1  
Old 06-01-2014
Adding word in blank space

Hi,

var=QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR

I have to put *.sql after every word

What I did

Code:
echo $var>/root/file1.sql
sed -i 's/ /*.sql /g' file1.sql

Code:
cat file1.sql

Output
HTML Code:
QSB*.sql SBD*.sql SDN*.sql SGJ*.sql SJP*.sql SKB*.sql SKD*.sql SLP*.sql SML*.sql SNB*.sql SRE*.sql SRG*.sql STP*.sql TAJ*.sql UMP*.sql UNO*.sql VKS*.sql VND*.sql VNS*.sql WAH*.sql ZRR
Still the Last word is not ZRR*.sql

How can we do the add "*.sql" in whole wordsSmilie

Last edited by Scrutinizer; 06-01-2014 at 09:04 AM.. Reason: code tags
# 2  
Old 06-01-2014
You can either add a space at the end of the var definition, or you mention the EOL in your sed expression:
Code:
echo $var | sed -r 's/ |$/.sql /g '

Make sure it understands EREs.
# 3  
Old 06-01-2014
Code:
printf "%s*.sql " $var; echo

output:
Code:
QSB*.sql SBD*.sql SDN*.sql SGJ*.sql SJP*.sql SKB*.sql SKD*.sql SLP*.sql SML*.sql SNB*.sql SRE*.sql SRG*.sql STP*.sql TAJ*.sql UMP*.sql UNO*.sql VKS*.sql VND*.sql VNS*.sql WAH*.sql ZRR*.sql

Code:
echo "$var" | sed 's/[^ ]*/&*.sql/g'


Last edited by Scrutinizer; 06-01-2014 at 03:21 PM.. Reason: Edited to add asterisk.. thanks Akshay
# 4  
Old 06-01-2014
Guess I'll post my take too.

Code:
#!/bin/bash
                                                                               
var="QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR"

for v in $var; do
    echo $v"*.sql"
done

# 5  
Old 06-01-2014
I think Scrutinizer , RudiC and decent, this is expected o/p

Code:
$ var="QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR"

$ awk 'gsub(/ |$/,"*.sql&")' <<<$var
QSB*.sql SBD*.sql SDN*.sql SGJ*.sql SJP*.sql SKB*.sql SKD*.sql SLP*.sql SML*.sql SNB*.sql SRE*.sql SRG*.sql STP*.sql TAJ*.sql UMP*.sql UNO*.sql VKS*.sql VND*.sql VNS*.sql WAH*.sql ZRR*.sql

These 2 Users Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 06-01-2014
Quote:
Originally Posted by kaushik02018
Hi,
var=QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR
I am curious, how does this work without quotes at the start and end of the string?
(Copy'n'paste into an editor.)
Code:
#!/bin/bash --posix
var=QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR
echo "$var"

Error result:-
Code:
Last login: Sun Jun  1 21:21:00 on ttys000
AMIGA:barrywalker~> chmod 755 str_err.sh
AMIGA:barrywalker~> ./str_err.sh
./str_err.sh: line 2: SBD: command not found

AMIGA:barrywalker~> _

---------- Post updated at 09:57 PM ---------- Previous update was at 09:30 PM ----------

Longhand using OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash --posix
var="QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR"
var_arr=($var)
var=""
n=0
while [ $n -lt 21 ]
do
	var="$var${var_arr[$n]}"'*.sql '
	n=$((n+1))
done
echo "$var"
exit 0

Results:-
Code:
Last login: Sun Jun  1 21:52:46 on ttys000
AMIGA:barrywalker~> chmod 755 str_ext.sh
AMIGA:barrywalker~> ./str_ext.sh
QSB*.sql SBD*.sql SDN*.sql SGJ*.sql SJP*.sql SKB*.sql SKD*.sql SLP*.sql SML*.sql SNB*.sql SRE*.sql SRG*.sql STP*.sql TAJ*.sql UMP*.sql UNO*.sql VKS*.sql VND*.sql VNS*.sql WAH*.sql ZRR*.sql 
AMIGA:barrywalker~> _

# 7  
Old 06-01-2014
Hi @wisecracker:
Why do you hard-code 21 instead of ${#var_arr[@]} ?
Why use arrays when you are only enumerating a list (for i in $var; do)

You could also use:
Code:
var_arr=($var)
printf "%s*.sql" "${var_arr[@]}"; echo

or
Code:
printf "%s*.sql" $var; echo

--
Note: If you are using bash, you can use arrays, but arrays are not part of the posix shell, although they happen to be supported by bash in posix mode
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing blank space using VI

Hi, How to remove blank spaces using vi (I am using AIX)? #cat siva.txt AAA BBB CCC DDD EEE FFF Need to remove space between 2 columns. Regards, Siva (7 Replies)
Discussion started by: ksgnathan
7 Replies

2. Shell Programming and Scripting

Not delete space blank

Hi everyone, i need to "grep" a file with a string with space blanks, like this: grep "XXXX XX" file.txt The problem, i need put the "XXXX XX" in a string variable. When the script executes the grep, do: gresp XXXX XX file.txt How can i solve this problem? The... (5 Replies)
Discussion started by: Xedrox
5 Replies

3. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

4. UNIX for Dummies Questions & Answers

blank space

hi everyone, i have a problem in unix script , i need to remove line that has blank , not blank line . example: mahm,,jdggkhsd,ghskj,,fshjkl can anyone help? (4 Replies)
Discussion started by: Reham.Donia
4 Replies

5. Shell Programming and Scripting

sed adding a blank line

I use the following as part of a script to correct for a faulty hostname file. # get the domain name read -r thehostname < /etc/hostname dom="$(echo $thehostname | cut -d'.' -f2)" numchar=${#dom} if then echo "It appears as though the hostname is not correctly set." echo "Hostname has... (5 Replies)
Discussion started by: bugeye
5 Replies

6. Shell Programming and Scripting

Cut last blank space

Hello, I am using this to get only directories : ls -l | grep '^d'and here is the result : drwx------ 13 so_nic sonic 13 Nov 4 13:03 GLARY drwx------ 3 so_nic sonic 3 May 6 2010 PSY2R drwx------ 15 so_nic sonic 15 Oct 14 08:47 PSYR1 But I only need to keep this... (7 Replies)
Discussion started by: Aswex
7 Replies

7. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

8. Shell Programming and Scripting

Replace exact word with blank

i have a file with the below content file1.txt ALERTADMIN.FIELD ALERTADMIN.TX_ALERTS_LOG i have another file file2 ALERTADMIN.FIELD ALERTADMIN.FIELD_WS ALERTADMIN.SECTION_FIELD_WS ALERTADMIN.TX_ACCT_PROCESSING_WORK_TABLE ALERTADMIN.TX_ACCT_REVIEW_EXEC_METRICS... (2 Replies)
Discussion started by: lavnayas
2 Replies

9. Shell Programming and Scripting

Adding a word in front of a word of each line.

Adding a word in front of a word of each line.In that line only one word will be there. pl help:( (4 Replies)
Discussion started by: Ramesh Vellanki
4 Replies

10. UNIX for Dummies Questions & Answers

adding blank line in egrep

I followed the egrep example given in the thread "parse text or complex grep ?". It is exactly what I need...except... how do I insert a blank line after the second line? My exact command is: egrep 'patt1|patt2' filename the result is: patt1 patt2 patt1 patt2 and so on. I would... (2 Replies)
Discussion started by: antalexi
2 Replies
Login or Register to Ask a Question