The AWK proposal should definitely not be used. The goal is to squeeze multiple spaces into one, but that AWK oneliner will delete all leading and trailing whitespace (even if it's just a single space). Worse, it will delete any tabs in the data.
A nitpick regarding the sed, \+ I believe is a gnu basic regular expression extension. In standardized basic regular expressions, a plus sign is an ordinary character and a backslash will not make it into a multiplier. Instead of x\+, it would be more portable to use either x\{1,\} or xx*
I vote for the tr solution
Cheers,
Alister
Hi Alister,
After seing your comment i have tested my code below with leading and trailing spaces and/or with tabs again, fortunately it worked well in all of this cases
But good to remember that we can use "tr" too.
I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"):
e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA
When I read this record and either echo/print to screen or write to... (4 Replies)
Greetings,
I have a bunch of music files that I want to strip the underscores out, and leave only spaces. All that I've found on the web is how to add underscores to files that have spaces, and reversing the "tr" command does not make a difference.
Here is how to convert spaces to... (6 Replies)
Hey all, I have a list in the format ;
variable length with spaces
more variable information
some more variable information
and I would like to transform that 'column' into rows ;
variable length with spaces more variable information some more variable information
Any... (8 Replies)
consider the small piece of code
while read line
do
echo $line
done < example
content of example file
sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr
the output is like
sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr
the... (4 Replies)
I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command:
tr ' ' ' ' w.txt
The output is:
tr: extra operand `w.txt'
Try `tr --help' for more information.
Can someone please help? :wall:
... (2 Replies)
I neead a script which converts low values to the spaces, When I used
sed -e 's/\x00/\x20/g' inputfile command it is removing the low values but not replacing it with spaces. Please help me. Its Uregent. Thanks
Sam (12 Replies)
I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes (").
In its simplest form, it would look like this:
sed -e 's# # #g'... (4 Replies)
Hi Friends,
I looked up online, but couldn't figure out a proper solution.
I have an input file where the columns are separated by multiple spaces and the column content is separated by single space.
For example,
Chr1 hello world unix is fun
In the above example, chr1 is first... (3 Replies)
Hi All.
Attached are two files.
I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt "
I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line.
But in two... (3 Replies)
I have the following file I wanted to convert mutiple spaces to tab:
I tried cat filename | tr ' ' '\t' or sed 's/ */ /' FILE
but it looses the format
5557263102 5557263102 5552074858 5726310211 5557263102 5557263102
5557263103 5557263103 2142406768 ... (2 Replies)
Perl::Critic::Policy::CodeLayout::ProhibitHardTabs(3) User Contributed Perl Documentation Perl::Critic::Policy::CodeLayout::ProhibitHardTabs(3)NAME
Perl::Critic::Policy::CodeLayout::ProhibitHardTabs - Use spaces instead of tabs.
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
Putting hard tabs in your source code (or POD) is one of the worst things you can do to your co-workers and colleagues, especially if those
tabs are anywhere other than a leading position. Because various applications and devices represent tabs differently, they can cause you
code to look vastly different to other people. Any decent editor can be configured to expand tabs into spaces. Perl::Tidy also does this
for you.
This Policy catches all tabs in your source code, including POD, quotes, and HEREDOCs. The contents of the "__DATA__" section are not
examined.
CONFIGURATION
Hard tabs in a string are always forbidden (use " " instead). But hard tabs in a leading position are allowed when they are used to
indent code statements, "qw()" word lists, and regular expressions with the "/x" modifier. However, if you want to forbid all tabs
everywhere, then add this to your .perlcriticrc file:
[CodeLayout::ProhibitHardTabs]
allow_leading_tabs = 0
NOTES
Beware that Perl::Critic may report the location of the string that contains the tab, not the actual location of the tab, so you may need
to do some hunting. I'll try and fix this in the future.
AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
perl v5.16.3 2014-06-09 Perl::Critic::Policy::CodeLayout::ProhibitHardTabs(3)