Adding gaps to a string in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding gaps to a string in bash
# 1  
Old 01-09-2014
Adding gaps to a string in bash

I have the following string, and want to introduce additional spaces between the two %s. This will be done by specifying the gap between the %s. Example having gap=8 will put 8 spaces between the two %s.

Code:
frmt_k1d1_test="%s    %s\n"

I am doing the script in bash.

---------- Post updated at 06:12 AM ---------- Previous update was at 05:42 AM ----------

I have tried this which works. Something simpler would be useful.

Code:
echo "$frmt_k1d1_test" | awk -v g="$gap" 'BEGIN {FS=OFS=""} {for (i=1;i<=NF;i++) {if (i==3) $i=$i g}}1'

# 2  
Old 01-09-2014
Try this
Code:
aLongSpace="                                                                                                            "
gap=8
frmt_k1d1_test="%s${aLongSpace:0:$gap}%s\n"
echo "$frmt_k1d1_test"

# 3  
Old 01-09-2014
Have got another method, however I need to put $gap in the sed statement. How can I pass the gap?

Code:
echo "$frmt_k1d1_test" | sed 's/.../&$gap/g'

---------- Post updated at 06:17 AM ---------- Previous update was at 06:14 AM ----------

Quote:
Originally Posted by chacko193
Try this
Code:
aLongSpace="                                                                                                            "
gap=8
frmt_k1d1_test="%s${aLongSpace:0:$gap}%s\n"
echo "$frmt_k1d1_test"

This works, however I originally have the string

Code:
frmt_k1d1_test="%s    %s\n"

Modification should be done using this string as initial input.
# 4  
Old 01-09-2014
Quote:
Originally Posted by kristinu
Have got another method, however I need to put $gap in the sed statement. How can I pass the gap?

Code:
echo "$frmt_k1d1_test" | sed 's/.../&$gap/g'

---------- Post updated at 06:17 AM ---------- Previous update was at 06:14 AM ----------



This works, however I originally have the string

Code:
frmt_k1d1_test="%s    %s\n"

Modification should be done using this string as initial input.
Just use "" instead of ''
# 5  
Old 01-09-2014
The sed is a good way. However things are not done yet

Code:
echo "$frmt_k1d1_test" | sed "s/.../&$gap/g"

which produces

Code:
%s            %s\    n

---------- Post updated at 06:51 AM ---------- Previous update was at 06:30 AM ----------

I think the best way is to search for 4 spaces, then replace with a string of size gap.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. UNIX for Dummies Questions & Answers

Gaps and frequencies

I have this infile: >GHL8OVD01BNNCA Freq 10 TAGATGTGCCCGTGGGTTTCCCGTCAACACCGGATAGT-GCAGCA-TA >GHL8OVD01CMQVT Freq 1 TTGATGTCGTGGGTTTCCCGTCAACACCGGCAAATAGT-GCAGCA-TA >GHL8OVD01CMQVT Freq 1 TTGATGTGCCAGTTTCCCGTCTAGCAGCACTACCAGGACCTTCGC-TA >GHL8OVD01CMQVW Freq 1... (1 Reply)
Discussion started by: Xterra
1 Replies

3. Shell Programming and Scripting

Adding Content to Variable (bash)

is this possible? its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number. For instance: VAR=Tommy for all in $(blah blah) do ... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Sorting and moving file sequence with gaps

Hello, I have lots of sequentially numbered files which make up an image sequence. I'm trying to do two things with it: #1: Find gaps in the sequence and move each range of sequencial files into their own subfolder. #2: Designate a starting point (file) and move every 24th file into... (4 Replies)
Discussion started by: ex_H
4 Replies

5. Shell Programming and Scripting

bash adding multiple names

Hi all, I make this bash script in which there is one option to add a name in txt file. So when I run the script it ask me for the name. When i enter the name, that name gets added in a txt file. If i add the same name again, the message comes up, the name is already there My question is..... (2 Replies)
Discussion started by: Learnerabc
2 Replies

6. Shell Programming and Scripting

Searching for Gaps in Time

I am very new to shell scripting. We use C-Shell here and I know the issues that surround it. I hope a solution can be created using awk, sed, etc... instead of having to write a program. I have an input file that is sorted by date and time in ascending order ... (2 Replies)
Discussion started by: jclanc8
2 Replies

7. OS X (Apple)

Adding Color to bash

Hey everyone, I have come across an issue to where I am trying to create a script which changes the text color with a simple if then statement. I have seen it done with Fedora 8 but when I try and create it using my MacBook Pro running Snow Leopard it doesn't work. Funny thing is, when I use... (2 Replies)
Discussion started by: dachadster13
2 Replies

8. Linux

Searching for gaps in huge (2.2G) log file?

I've got a 2.2 Gig syslog file from our Cisco firewall appliance. The problem is that we've been seeing gaps in the syslog for anywhere from 10 minutes to 2 hours. Currently I've just been using 'less' and paging through the file to see if I can find any noticeable gaps. Obviously this isn't the... (3 Replies)
Discussion started by: deckard
3 Replies
Login or Register to Ask a Question