0

I am trying to remove the spaces from each line till the last word for each line in a file.

Example input file:

808 0    C01124       Amazon            45/234Birch         00             YYY
808 0    C01184       Flipkart          45/234Lotus         01             YYY
808 0    C01186       PrimeTime         45/23XCCCCH         08             YYY
808 0    C01125       DMART             5/23PPPPPPP         09             YYY

The format is somewhat like the above, with uneven spaces and the last word always being 'YYY'. My requirement is: there are spaces after the YYY that I want to preserve, but I want to remove all the other spaces.

Desired output:

8080C01124Amazon45/234Birch00YYY
8080C01184Flipkart45/234Lotus01YYY
8080C01186PrimeTime45/23XCCCCH08YYY
8080C01125DMART5/23PPPPPPP09YYY

I have tried using sed and tr commands, but it becomes a huge mess and all the lines become just one line. I want to segregate like above but I am not able to come to the requirement. How can I do it?

3
  • So there are spaces after the YYY that you want to preserve, but you want to remove all the other spaces, rather than just removing all the spaces? – icarus Mar 20 at 3:22
  • yes..is this doable? – Amin Mar 20 at 3:24
  • 1
    Can you share the tr command that you tried and which squashed the data? tr -d '\t ' should have worked – guest_7 Mar 21 at 0:37
3

The are a number of ways. The most obvious, at least to me, is to use a loop inside sed.

 sed  -e :loop -e 's/  *\(.*YYY\)/\1/;t loop'

(there are two spaces after the first slash). The :loop just is a label. The s/ *\(.*YYY\)/\1/ matches one or more spaces followed by something and then YYY and replaces it with the same thing without the leading spaces. The t loop says to go to the label if the substitution took place. Each time round the loop it removes one block of spaces before the YYY. Nothing touches the spaces or anything else after the YYY.

5
  • Works Perfectly!!! Thanks a lot – Amin Mar 20 at 4:05
  • I also came up with one after trying a lot: while read -r line do echo $line | sed -e 's/ //g' >> $tar done < $file – Amin Mar 20 at 4:06
  • @Amin Your while loop doesn't preserve spaces after YYY. If you don't want them preserved the tr -d" " is simple. – icarus Mar 20 at 4:11
  • @Amin if you are happy with the answer you can "accept" it after some time (to see if there is a better answer) and upvote it (assuming you think the answer is good, i.e. clear, correct, will help you answer similar questions) – icarus Mar 20 at 5:10
  • +1 for a good explanation of the subject query. – guest_7 Mar 20 at 6:13
1

With perl,

perl -pe 's{\s.*YYY}{$& =~ s/\s//gr}e'

would remove all the ASCII white space characters that are to the left of the rightmost occurrence of YYY on lines that contain YYY.

1

Here's an answer to the question you kind-of sort-of seem to be asking:

sed 's/ *\([^ ]\)/\1/g'

This assumes that you are dealing only with ordinary spaces, and not tabs.  It looks for a string of spaces, followed by a non-space character, and replaces it with just the non-space character.  And it does that globally (as many times as possible, on each line).  That will remove all spaces, except for those at the end of the line (after the last word), because they aren't followed by a non-space character.

This works no matter what the last word is (it doesn't have to be YYY).  Lines that contain only space(s) are passed through unchanged.


If you really want to remove all the spaces, use one of the simpler answers that do that.

1
awk '{$1=$1}1' OFS= your_file

This is all you need. The assigning to a field (in this case first) takes away all extras and the empty OFS stitches together the fields w/o anything separating them.

2
0

Using GNU sed stream editor in extended regex mode -E , we strip away the internal spaces, those that see a nonwhitespace on both sides. It needs a repeat as a single character nonwhitespace will stop the run.

sed -Ee '
  s/(\S)\s+(\S)/\1\2/g
  s//\1\2/g
' file
-1
sed -r "s/\s+//g" filename

output

8080C01124Amazon45/234Birch00YYY
8080C01184Flipkart45/234Lotus01YYY
8080C01186PrimeTime45/23XCCCCH08YYY
8080C01125DMART5/23PPPPPPP09YYY




 awk '{gsub(" ","",$0);print }' filename


8080C01124Amazon45/234Birch00YYY
8080C01184Flipkart45/234Lotus01YYY
8080C01186PrimeTime45/23XCCCCH08YYY
8080C01125DMART5/23PPPPPPP09YYY

Python

#!/usr/bin/python
import re
k=re.compile(r'\s')
l=open('filename','r')
for m in l:
    f=re.sub(k,"",m)
    print f.strip()
1
  • Thanks for this wonderful answer! – Amin Mar 22 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.