Search for an undetermined number of spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for an undetermined number of spaces
# 8  
Old 02-24-2018
Quote:
Originally Posted by bedtime
I was just about to post that I was not looking for the above code. I have used this, and it collapses return keys and other useful characters.

I only want to reduce spaces beyond a single space to a single space using awk.

Sorry for the runaround. Smilie
No worries Smilie . Another option may be to just adjust the field separator:
Code:
awk -F' *' '{$1=$1}1'

or perhaps a tiny tiny bit more efficient:
Code:
awk -F'  +' '{$1=$1}1' file

But what if you have a space and a TAB together? Do you leave the space or delete it?

Last edited by Scrutinizer; 02-24-2018 at 12:11 PM..
# 9  
Old 02-24-2018
Quote:
Originally Posted by Scrutinizer
No worries Smilie . Another option may be to just adjust the field separator:
Code:
awk -F' *' '{$1=$1}1'

or perhaps a tiny tiny bit more efficient:
Code:
awk -F'  +' '{$1=$1}1' file

But what if you have a space and a TAB together? Do you leave the space or delete it?
As far as I know there are no tabs to deal with in the program, thankfully!


I ended up using this code:
Code:
awk '{gsub (/ +/, " ")}1' file

But if your code is more efficient then I could use it instead. I just don't know how to append it to the end of my script:

Code:
awk ... blah blah blah ... {print $mytexttobedespaced} ... -F' +' '{$1=$1}1'

I don't want to use another pipe, and I cannot use this at the beginning of the code as it's a job that needs to wait til the end. Either way, the previous code works well enough.

Last edited by bakunin; 02-25-2018 at 03:31 AM.. Reason: HTML-tags changed to CODE-tags
# 10  
Old 02-25-2018
Quote:
Originally Posted by bedtime
As far as I know there are no tabs to deal with in the program, thankfully!


I ended up using this code:
Code:
awk '{gsub (/ +/, " ")}1' file

Just in case there are tabs:
Code:
awk '{gsub (/[<space><tab>]+/, " ")}1' file

Replace "<space>" and "<tab>" with literal space/tab characters when using this.

I hope this helps.

bakunin
# 11  
Old 02-25-2018
Code:
echo "This    sentence has     an undetermined    number of   spaces between 
  each word."| sed -r 's/\s+/ /g'

# 12  
Old 02-25-2018
Quote:
Originally Posted by bakunin
Just in case there are tabs:
Code:
awk '{gsub (/[<space><tab>]+/, " ")}1' file

Replace "<space>" and "<tab>" with literal space/tab characters when using this.

I hope this helps.

bakunin
If one doesn't want to use bakunin's suggestion because one is afraid the someone reading the code might not notice the literal <space> or literal <tab> in the ERE (and isn't willing to add a comment noting that the characters in that ERE are a literal <space> and <tab>), one could also use:
Code:
awk '{gsub (/[[:blank:]]+/, " ")}1' file

which does the same thing in most locales and is self-documenting. And, in locales where additional characters are members of the blank character class, you might also want this global substitution to produce replacements for them as well. If your code is likely to be used in locales like this, you should seriously consider whether you want to hard code these two characters or you want to use the character class definition.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing a specific number of spaces

when given a file name, im looking for the most efficient way to turn each letter of the file name into spaces. for instance, the way im thinking of going about this is this: MYFILE=check_disks.sh CHANUM=$(echo ${MYFILE} | awk '{ print length }') printf '%s\n' $CHANUM as you can see... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

Print a specific number of spaces

i'm looking for a command that allows me to print a specific number of spaces which i will provide. something as simple as: spaces=4 printf " " * $spaces i'm looking for somethign that'll allow me to print a specific amount of spaces on a line. can awk be used for this? (4 Replies)
Discussion started by: SkySmart
4 Replies

3. AIX

Search a string having spaces

Hi I am looking to search a string having spaces in a directory for example : my string is "summer hot" my code :for i in `cat position__list.txt` do echo $i" : " `find . -mtime -6 | xargs grep -l ":83D:$i" | xargs ls -ltr|tail -1|awk '{ print $6 , $7 , $8, $9;... (6 Replies)
Discussion started by: wedng.bell
6 Replies

4. Shell Programming and Scripting

Conduct a search or test -f over a path with spaces

I am organizing my music library on a NAS server. I want to print a list of all the directories that are missing the cover art (at least one or more jpeg file). I have successfully created a file with each line containing the path to each occurance of one or more .mp3 files. That path is also... (2 Replies)
Discussion started by: godfreydanials
2 Replies

5. Shell Programming and Scripting

search pattern that has spaces with nawk

Hi guys, I need a help ! I need do grab some string from file and then count n lines after that pattern. This is working fine, but my problem is that the string to be searched has spaces within, like an example : LINK COUNTERS what I am using is: nawk... (2 Replies)
Discussion started by: robdcb
2 Replies

6. Shell Programming and Scripting

determine the number of spaces

Here is a weird question :) i am trying to create a script written in bash that will create configuration files for nagios. As some of you aware is has to be written in the below format: define service{ option1 value1 option2 value2... (6 Replies)
Discussion started by: ppolianidis
6 Replies

7. UNIX for Dummies Questions & Answers

grep any number of spaces

which one of the following is the correct expression to ignore line with multiple spaces after any string cat file | grep -v "xyz *$" or cat file | grep -v "xyz*$" do i need "*" to specify the sapce or " *" will do? (2 Replies)
Discussion started by: manishma71
2 Replies

8. Shell Programming and Scripting

construct a string with X number of spaces

I'd like to create a variable with the value of X number of space( no Perl please), printf seems to work, but , in following example,10 spaces becomes 1 space when assinged to a variable, Why? other solutions are welcome. $printf "=%10s=\n" = = $var=$(printf "=%10s=\n") echo... (4 Replies)
Discussion started by: honglus
4 Replies

9. Shell Programming and Scripting

Split File of Number with spaces

How do i split a variable of numbers with spaces... for example echo "100 100 100 100" > temp.txt as the values can always change in temp.txt, i think it will be feasible to split the numbers in accordance to column. How is it possible to make it into $a $b $c $d? (3 Replies)
Discussion started by: dplate07
3 Replies

10. Shell Programming and Scripting

Search string with invariable spaces

I need to grep for pattern "enable_cal = true".The problem is that different file has different no of spaces in that string. for eg one file will have "enable_cal <space><space><space>=true" next file will have "enable_cal= <space><space>true" other one will have... (5 Replies)
Discussion started by: villain41
5 Replies
Login or Register to Ask a Question