3

I have a little problem in Linux with removing spaces from selected lines from my file.

For example:

f h t s q e g h c h t
f h t r f h v f d g n
q a z x s w e d c v f
f h b c h t h b v h f
p l k o i j u h y g t
t f r d c v b h n j u

and now, how should I delete spaces from lines number 2, 4 and 5? Additionally, how's it going to look for the third line?

6

Using awk

To remove blanks from lines 2, 4, and 5:

$ awk 'NR==2 || NR==4 || NR==5 {gsub(/ /,"");} 1' file 
f h t s q e g h c h t
fhtrfhvfdgn
q a z x s w e d c v f
fhbchthbvhf
plkoijuhygt
t f r d c v b h n j u

In awk, NR is the line number. The logical expression NR==2 || NR==4 || NR==5 selects the lines of interest to you. The command gsub(/ /,"") removes blanks from those lines. The condition 1 is awk's cryptic shorthand for print the line.

Using sed

To remove blanks from lines 2, 4, and 5:

$ sed '3n; 2,5 s/ //g' file 
f h t s q e g h c h t
fhtrfhvfdgn
q a z x s w e d c v f
fhbchthbvhf
plkoijuhygt
t f r d c v b h n j u

Here, we use slightly different logic: we remove blanks for all lines from 2 to 5 except line 3. This works as follows: the expression 3n tells sed that, when it comes to line 3, print it and skip to the next line. Otherwise, the command `2,5 s/ //g tells sed to remove blanks from all lines from 2 to 5.

To remove all whitespace, not just blanks

awk 'NR==2 || NR==4 || NR==5 {gsub(/[[:space:]]/,"");} 1' file

Or:

sed '3n; 2,5 s/[[:space:]]//g' file
  • I need "sed" version, but I have also another question: How it's gona look like for third line? – warszawiaczek Oct 8 '15 at 20:56
  • OK. The sed version leaves the third line unchanged. (For the third line, the n command is used. This causes sed to print that line unchanged and move on.) – John1024 Oct 8 '15 at 20:57
  • $ sed 'n; 3 s/ //g' file Is it ok? – warszawiaczek Oct 8 '15 at 21:01
  • @warszawiaczek That command would leave all lines unchanged. That is because n tells sed to skip the rest of the commands and, consequently, the 3 s/ //g command would never be executed. – John1024 Oct 8 '15 at 21:03
  • sooooooo $ sed '3n; 3 s/ //g' file remove all blanks in third line? – warszawiaczek Oct 8 '15 at 21:17
5

Other variant for awk

awk 'NR~/^[245]$/{$1=$1}1' OFS='' file
  • the answer has it too but what is the '1' before the OFS doing ? It doesn't work without it ? – Bor Oct 9 '15 at 7:50
  • 1
    @Bor From man awk: One, but not both, of pattern {action} can be omitted. If {action} is omitted it is implicitly { print }. So in the case pattern == 1 (mean true for each line) and action is omitted that is mean { print }. In other word 1 is shortening True { print $0 } – Costas Oct 9 '15 at 7:57
2

You could do it with a shell. It might even be worth doing that way in some cases. Probably not often, though.

sh -fc  <in >out '
    for IFS do read -r l
        printf %s $l;echo
    done; exec cat
' -- '' \  '' \  \ 

f h t s q e g h c h t
fhtrfhvfdgn
q a z x s w e d c v f
fhbchthbvhf
plkoijuhygt
t f r d c v b h n j u
0

With :

perl -lpe 'grep { /^$.$/ } (2,4,5) and s/\s//g'

or with the experimental smartmatch operator :

perl -lpe '$. ~~ [2,4,5] and s/\s//g'

With :

awk 'NR==2 || NR==4 || NR==5{gsub(/\s/, "")} 1' file

Output :

 f h t s q e g h c h t
fhtrfhvfdgn
 q a z x s w e d c v f
fhbchthbvhf
plkoijuhygt
 t f r d c v b h n j u
0

Somebody needed to represent 'tr' properly, declare a solution therewith, so here goes:

 tr -d [:space:]

OR

 tr -d [:blank:]

Assuming the text source is a file called ich:

 cat ich | tr -d [:space:]

This produces one line, no space:

fhtsqeghchtfhtrfhvfdgnqazxswedcvffhbchthbvhfplkoijuhygttfrdcvbhnju

If you wish to preserve the lines, then employ this variant:

 cat ./ich | tr -d [:blank:]

which produces this output ( minus the extra newline between the lines )

fhtsqeghcht

fhtrfhvfdgn

qazxswedcvf

fhbchthbvhf

plkoijuhygt

tfrdcvbhnju

Brilliant answers, my esteemed peers, each and every one!

F.

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.