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*
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.
can somebody explain the working of the awk option
what is {$1=$1}1 in the above option doing . Thanks in advance
AWK programs are pairs of patterns and actions. Either the pattern or the action may be absent, but not both. A missing pattern evaluates to true for all lines. A missing action (the code withing curly braces) defaults to "{print $0}", which prints the line.
That code represents two pattern-action pairs.
The first pair is "{$1=$1}". This pair is missing the pattern, so it will match on every line read. The action is the code between the curly braces. For each line read, it assigns the value of $1 to $1. This does not change the field's value, but it does cause $0 to be recomputed, with each field separated by OFS (output field separator, whose default value is a space). This leads to the loss of leading and trailing whitespace, and replacement of whatever strings were originally field separators (one space, multiple spaces, tabs, combinations thereof) with one OFS.
The second pair is simply the traling one, "1", nothing more. The pattern is the "1", which is a boolean true and will cause its corresponding action to execute for every line read by AWK. The action however is missing, so it defaults to printing the line (as stated above). You see this idiom used often by AWKers trying to save a few characters; it is shorthand for printing the current line ($0).
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)
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)
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)
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)
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 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)
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)
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)
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)
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)