LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2013, 05:54 PM   #1
ali.abry
Member
 
Registered: Feb 2013
Posts: 74

Rep: Reputation: Disabled
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$

Last edited by ali.abry; 11-14-2013 at 05:57 PM.
 
Old 11-15-2013, 04:38 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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
 
1 members found this post helpful.
Old 11-15-2013, 05:27 AM   #3
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
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$
 
1 members found this post helpful.
Old 11-15-2013, 05:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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$
 
Old 11-15-2013, 06:05 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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

Last edited by Firerat; 11-15-2013 at 06:22 AM.
 
Old 11-15-2013, 06:20 AM   #6
ali.abry
Member
 
Registered: Feb 2013
Posts: 74

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 11-15-2013, 06:50 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 11-15-2013, 11:12 AM   #8
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
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
 
1 members found this post helpful.
Old 11-15-2013, 11:39 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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
 
Old 11-15-2013, 03:25 PM   #10
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
@druuna
You're welcome.
 
Old 11-16-2013, 12:45 AM   #11
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
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 .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ed command problem moata_u Programming 3 03-23-2011 06:41 PM
problem with auditreduce command and praudit command preetishetty Linux - Security 0 12-03-2007 09:24 AM
A tr command problem baks Programming 10 05-31-2007 08:03 AM
at Command Problem BBPS Linux - Software 2 09-06-2006 06:23 AM
problem with dd command ? MaaSTaaR Linux - General 5 06-25-2006 08:50 AM


All times are GMT -5. The time now is 09:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration