Adding variable number of space in between two string


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding variable number of space in between two string
# 1  
Adding variable number of space in between two string

Hi,

I am looking for the way to add variable number of spaces between two string. e.g input line is
a ,bb
abc ,bcb
pqr ,bfg

My output should be something like this
Code:
a                                   ,bb
abc                    ,bcb
pqr                 ,bfg

This text are basically inside a file. So I tried like this

Code:
while read line
do
        IFS=','
        set $line
        printf '%s' "$1" >> out.txt
        ## Here I have to include space to arrange line properly
        printf '%s,' "$pading" >> out.txt 
        printf '%s\n' "$2" >> out.txt
 
done < $fname

Thanks

---------- Post updated at 04:18 AM ---------- Previous update was at 04:13 AM ----------

Well I am not able to show the require output properly . The only thing which I want is second column should be start at same point.Like
bg
bc

Not like
bg
"some spaces" bc
# 2  
This may help:
Code:
IFS="${IFS},"
while read f1 f2; do
        printf '%-20s%s\n' "$f1" "$f2"
done

which generates:
Code:
a                   bb
abc                 bcb
pqr                 bfg

# 3  
Thanks. But it is not working
# 4  
I checked the script:
Code:
$ sh diehard.sh < diehard.txt
a                   bb
abc                 bcb
pqr                 bfg

$ cat diehard.sh
IFS="${IFS},"
while read f1 f2; do
        printf '%-20s%s\n' "$f1" "$f2"
done

$ cat diehard.txt
a ,bb
abc ,bcb
pqr ,bfg

Can you post your version of the script and what it is (or is not) producing?
This User Gave Thanks to derekludwig For This Post:
# 5  
Hello,

Could you please try following, it may help you.

Code:
awk -F, 'FNR==NR{l=length($1) > l?length($1):l;next} {$1=sprintf("%-"l"s",$1)} 1' OFS=, Input_file Input_file

Output will be as follows.
Code:
a                                    ,bb
abc                                  ,bcb
pqr                                  ,bfg

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-01-2014 at 06:50 AM.. Reason: corrected a solution
# 6  
Well the field is separated by , so I can have pq abc , pq. In that case your script won't work.
By the way thanks for your help.
# 7  
Quote:
Originally Posted by diehard
Well the field is separated by , so I can have pq abc , pq. In that case your script won't work.
By the way thanks for your help.
Hello,

Following is working even space is there in first field, let's say we have following input file.

Input_file:
Code:
cat check_space
a                                   ,bb
abc                    ,bcb
pqr ab              ,bfg

Output will be as follows.
Code:
awk -F, 'FNR==NR{l=length($1) > l?length($1):l;next} {$1=sprintf("%-"l"s",$1)} 1' OFS=, Input_file Input_file
 
a                                     ,bb
abc                                   ,bcb
pqr ab                                ,bfg

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-01-2014 at 06:50 AM.. Reason: Corrected solution
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #292
Difficulty: Easy
In 2011, Google introduced the Chromebook, a web thin client running Android..
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

4. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

5. Shell Programming and Scripting

Checking variable with specific string and stripping number from it.

I am parsing a file and I get differnt results everytime. Sometimes I get 12s sometimes I get 54m and sometime 3h.. v1=12s or v1=54m or v1=3h 12s - 12 seconds 54m - 54 minutes 3h - 3 hour I have to write a script in such a way that it whenever v1 is in minutes, I should strip "m"... (14 Replies)
Discussion started by: jayeshpatel
14 Replies

6. Shell Programming and Scripting

Adding string variable dynamically in for loop

Hi, I need to generate the text name dynamically in for loop, ex, VAR_COPY_FILE1= file path 1 VAR_COPY_FILE2= file path 2 VAR_COPY_FILE3= file path 3 for i in 1 2 3 do if then "do some process here" fi done (3 Replies)
Discussion started by: msubash26
3 Replies

7. UNIX for Dummies Questions & Answers

Adding space to a directory?

I need to add space to certain directory. I believe I need to add space to the filesystem this directory belongs to. How can I find out what filesystem this directory belongs to? (3 Replies)
Discussion started by: NycUnxer
3 Replies

8. Shell Programming and Scripting

way to print all the string till we get a space and a number

Is there any way to print all the string till we get a space and a number and store it a variable for eg we have string java.io.IOException: An existing connection was forcibly closed by the remote host 12 All I want is to store "java.io.IOException: An existing connection was forcibly closed... (13 Replies)
Discussion started by: villain41
13 Replies

9. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

10. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

Featured Tech Videos