Adding variable number of space in between two string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding variable number of space in between two string
# 8  
Old 12-01-2014
Yes it working. Thanks.

Can we have any kind of separator like | or : or ; ?
# 9  
Old 12-01-2014
With respect R. Singh, your code has an interesting side effect if the width of the line of data is not non-decreasing, as in:
Code:
a   ,bb
abc                        ,bcb
pqr ab    ,bfg

Which results in:
Code:
a   ,bb
abc                        ,bcb
pqr ab                     ,bfg

which may or may not be desirable. For a constant width, perhaps:
Code:
awk -F ' *,' '{printf "%-20sn,%s\n", $1, $2; }' inputfile
a                   ,bb
abc                 ,bcb
pqr ab              ,bfg

Depends what the end result is supposed to be....
# 10  
Old 12-01-2014
Quote:
Originally Posted by diehard
Yes it working. Thanks.

Can we have any kind of separator like | or : or ; ?
Yes, offcourse we can have different seprators in code, also I have changed my solution a bit in previous posts in which I am reading file 2 times. Following is an example for separator ;.

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

---------- Post updated at 07:29 AM ---------- Previous update was at 06:56 AM ----------

Quote:
Originally Posted by derekludwig
With respect R. Singh, your code has an interesting side effect if the width of the line of data is not non-decreasing, as in:
Code:
a   ,bb
abc                        ,bcb
pqr ab    ,bfg

Which results in:
Code:
a   ,bb
abc                        ,bcb
pqr ab                     ,bfg

which may or may not be desirable. For a constant width, perhaps:
Code:
awk -F ' *,' '{printf "%-20sn,%s\n", $1, $2; }' inputfile
a                   ,bb
abc                 ,bcb
pqr ab              ,bfg

Depends what the end result is supposed to be....
Hi derekludwig,

I have fixed the same and mentioned in POST#10. Also you can't hardcode value of "%-20s as it is dependent on 1st field.
so we can better take length of first field and do the operation accordingly.


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-01-2014 at 07:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question