LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with unexpand command (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-unexpand-command-4175484611/)

ali.abry 11-14-2013 04:54 PM

problem with unexpand command
 
Hi every body
why unexpand command not working for me using ubuntu 12.04 ?
Code:

aliali@lp:/tmp$ cat test.txt
 A B C
aliali@lp:/tmp$ unexpand test.txt
 A B C
aliali@lp:/tmp$ unexpand -t8 test.txt
 A B C
aliali@lp:/tmp$ unexpand -a -t8 test.txt
 A B C
aliali@lp:/tmp$


druuna 11-15-2013 03:38 AM

I have struggled in the past to understand how unexpand actually works and gave up on it (there are enough alternatives available).

Here's one: tr
This replaces all spaces into tabs:
Code:

tr ' ' '\t' < test.txt

Madhu Desai 11-15-2013 04:27 AM

unexpand doesn't insert tabs, but instead converts spaces to tab.

For example, the following are contents of file 'one', which i added in vi editor which has tab width of 8 characters.

Code:

$ cat one
ubuntu        deb        13.10
redhat  rpm    06.40

Note that in first line spaces between words are actually tabs. However, in second line, the spaces are actualy spaces only.

verify:
Code:

$ cat -vet one
ubuntu^Ideb^I13.10$
redhat  rpm    06.40$

where ^I represents tab, $ represents newline

Now, i unexpand the file and the result is self-explanatory:
Code:

$ unexpand -a -t8 one > two

$ cat two
ubuntu        deb        13.10
redhat        rpm        06.40

$ cat -vet two
ubuntu^Ideb^I13.10$
redhat^Irpm^I06.40$


druuna 11-15-2013 04:39 AM

@mddesai: I understand from the manual page that it replaces space(s) with tab(s), or so they say...

Try explaining this:
Code:

$ cat -vet one
ubuntu deb 13.10$
redhat rpm 06.40$
$ unexpand -t8 one > two
$ cat -vet two
ubuntu deb 13.10$
redhat rpm 06.40$

# or:
$ cat -vet one
ubuntu  deb  13.10$
redhat  rpm  06.40$
$ unexpand -t8 one > two
$ cat -vet two
ubuntu^I deb  13.10$
redhat^I rpm  06.40$

Both do not do what I (wrongly??) expect it to do, this does:
Code:

$ cat -vet one
ubuntu  deb  13.10$
redhat  rpm  06.40$
$ tr ' ' '\t' < one > two
$ cat -vet two
ubuntu^I^I^Ideb^I^I^I13.10$
redhat^I^I^Irpm^I^I^I06.40$


Firerat 11-15-2013 05:05 AM

I don't know unexpand

from the posts I read here I assume -t8 is 'setting' the number of spaces a tab is worth



so, a sed equivalent
Code:

sed 's/ \{8\}/\t/g'
every 'block' of 8 spaces is replaced by a tab

if the input only had 7 spaces, it wouldn't do anything

if 9to15, a tab followed by n-8 spaces


so...
@ali try -t1
but expect odd results with real input data

Edit:
actually, I just tested unexpand
it didn't work quite as I expected
since I can only think of 'converting' indentation in source code as a use for it, I would use the sed :) , maybe change 8 to 4

ali.abry 11-15-2013 05:20 AM

thanks every one . now i completely understand the way unexpand command work .
I was thinking that it change each space to a tab which was completely wrong .

Firerat 11-15-2013 05:50 AM

I'm still uncertain as to what unexpand does, or its practical use ...

but yeah, i think it is the wrong tool for what you want.

tr, ed, sed or awk

are probably more suited

Madhu Desai 11-15-2013 10:12 AM

unexpand doesn't format/align texts in file for output purpose. it just converts spaces to tabs in source file. It only converts every letter/word/space which has a total width of 8 (default) with leading whitespace. Rest of the whitespaces are not changed.

Some possible situations where unexpand can be used could be:
  1. You are using some application which recognises tabs for whatever reason and so you want to replace spaces with tabs in files.
  2. To reduce file size.

Code:

$ cat -n aa
    1        123456781234567812345678
    2        A      B      C
    3        A      B        C
    4        A      B  C
    5

In the above example,
  1. For scale
  2. Both A and B have whitespace of 8 (including letter A and B)
  3. Same as above, but B has 2 extra whitespaces.
  4. There are not enough spaces between B and C
  5. Full of whitespaces.

Code:

$ unexpand -a aa > bb
$ cat -n bb
    1        123456781234567812345678
    2        A        B        C
    3        A        B          C
    4        A        B  C
    5

No difference between files aa and bb.

But the difference is clearly apparent:
Code:

$ cat -nvet aa
    1        123456781234567812345678$
    2        A      B      C$
    3        A      B        C$
    4        A      B  C$
    5                                $

$ cat -nvet bb
    1        123456781234567812345678$
    2        A^IB^IC$
    3        A^IB^I  C$
    4        A^IB  C$
    5        ^I^I^I $

$ ls -l aa bb
-rw-rw-r--. 1 user user 102 Nov 15 21:20 aa
-rw-rw-r--. 1 user user  51 Nov 15 21:23 bb


druuna 11-15-2013 10:39 AM

@mddesai: Thanks, I understand what its doing now.

Still, like Firerat, I'm not sure where this command comes in handy. But then again, I've never been a fan of tabs and if possible I always change them to spaces. Maybe if one works a lot with tabs a practical use becomes obvious.

David MacKenzie must have had a reason, otherwise he wouldn't have written it ;)

Madhu Desai 11-15-2013 02:25 PM

@druuna
You're welcome.

Madhu Desai 11-15-2013 11:45 PM

One situation where it comes handy is, which i wished i knew then was, few years back when i was doing a project on bank, i had to regularly convert database to text file format, so that i can send copy of them through internet from regional office to head office since they had no software integration. Those text files were in several hundred of lines and each column of width from 10 to 30 spaces (no tabs). That made file size bulky and took long time to send (dial-up network). I could have applied unexpand to make file size small. But again, it was on windows xp. i didn't knew anything about linux then :p.


All times are GMT -5. The time now is 01:53 AM.