LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-11-2012, 09:27 AM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
Stripping out tabs in file before redirecting to command.


Good morning All,

Real simple question, but for some reason I cannot figure it out. I have 2 files <file1> <file2> that I need to compare and then send to a command. However, the file contains tabs that I must strip out prior to sending to the command.

This is what I got so far:
Code:
lpr < diff file1 file2
I want this to run on the CLI, no perl, no .sh, etc....

Basically I need to now what the argument is that I place next to the '<' to strip out the tabs.

I look forward to your response.

s
 
Old 10-11-2012, 09:45 AM   #2
oneindelijk
Member
 
Registered: Oct 2012
Location: Belgium
Distribution: Xubuntu
Posts: 32

Rep: Reputation: Disabled
use sed
Quote:
cat file1 | sed s/\t//g >> tempfile1
 
Old 10-11-2012, 09:58 AM   #3
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Thumbs down

oneindelijk said,
Quote:
cat file1 | sed s/\t//g >> tempfile1
I appreciate your effort but that is not what i am looking for. Yes it removes the tabs out of file1, but it does not compare the files of file1 and file2, nor does it send the output to lpr.

I am aware of sed and all of its abilities, removing the tabs or comparing files or redirecting the output to lpr individually is not the problem. I am looking for an elegant method of doing this in one line.
 
Old 10-11-2012, 10:01 AM   #4
oneindelijk
Member
 
Registered: Oct 2012
Location: Belgium
Distribution: Xubuntu
Posts: 32

Rep: Reputation: Disabled
maybe multitail is what you're looking for ?
I'm not sure it's capable of comparing though
 
Old 10-11-2012, 10:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So is it just a case of putting it all together?
Code:
lpr < diff <(sed 's/\t//g' file1) <(sed 's/\t//g' file2)
 
Old 10-11-2012, 11:20 AM   #6
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
grail said,
Quote:
So is it just a case of putting it all together?
I also appreciate your attempt, but no cigar. However, I have figured it out.
Code:
lpr <<- diff file1 file2
Can I add to my rep? lol

I appreciate all of your help. Thank you.
s
 
Old 10-11-2012, 11:42 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious how the '-' strips out tabs? Do you have any links explaining this?
 
Old 10-11-2012, 12:12 PM   #8
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Smile

Here you go:

http://publib.boulder.ibm.com/infoce...put_inline.htm

However, it is not a concise definition of the operator/attribute. It just tells you what <<- does. Unfortunately that does not help, but it does what it says it is supposed to.

Thanks again for the help.
S
 
Old 10-13-2012, 08:34 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
lpr <<- diff file1 file2
That's not any kind of shell syntax I've ever seen, and I doubt very highly it actually works the way you expect it to. Are you using a different shell; c-shell perhaps? We usually assume Linux and bash unless stated otherwise, so you should always mention it when you are using a non-standard environment.

In any case, the link you gave also appears to describe a standard here document, as used by bash and most other shells:

Code:
command <<-ENDSTRING
<tab>a block of text to send
<tab>to the command's stdin
ENDSTRING
This is not what you used above. Heredocs need two identical end strings. And embedded commands don't automatically expand inside the heredoc either. They need to be placed in command substitution brackets.

Also, the extra "-" on the heredoc only strips any leading tabs off of the beginning of each line inside the heredoc, allowing you to indent the contents for better readability. It doesn't strip out any other tabs in the text.

grail's post used bash-specific process substitution, btw, which wouldn't work in any other shell. You would have to use something like temporary files and named pipes to do it otherwise.

Finally, I'd recommend tr instead of sed for removing individual characters from text. It's the most common option for such things.

Last edited by David the H.; 10-13-2012 at 08:36 AM. Reason: minor rewording
 
Old 10-14-2012, 08:01 PM   #10
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
David the H said,
Quote:
That's not any kind of shell syntax I've ever seen, and I doubt very highly it actually works the way you expect it to. Are you using a different shell; c-shell perhaps? We usually assume Linux and bash unless stated otherwise, so you should always mention it when you are using a non-standard environment.
I will admit that I used this in UNIX, however it was in sh. I assure you that it works and did the job. A lot better and more elegantly than sed and tr. It stripped out the leading tabs as you have mentioned in your post, not trailing, etc. This is the desired outcome. I did notice that I was not specific about that - sorry.

As far as what you have and/or have not seen before, I am not aware of. I am learning the nuances of UNIX/Linux and love to get input from professional individuals. Brow-beating or condescending view points I have no time for. If I have misread the tone of your post please forgive my directness.

I do appreciate the lesson on where I was lacking in my initial post. In the future, I will be more diligent in my verbiage.

s
 
Old 10-15-2012, 01:24 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
But that's just it. The line you gave in post #6 simply can not work in sh, or any variation of it that I know of. And the link you gave doesn't describe the syntax you used or the actions you prescribe to it either.

Either the actual command you used is different from what you posted here, or the command isn't actually doing what you think it's doing. In other words, it's just plain wrong, and only coincidentally giving you what you want in this particular instance.

The best I can guess that's happening is that the shell is erroring out on the improperly designed heredoc, ignoring it and the "diff" word, which it thinks is the delimiting string (in a script it appears to just read to the end of the file), and simply sending the raw text files straight into lpr.
 
  


Reply



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
redirecting sound file to audio device from command line kmvarier Linux - General 1 09-19-2011 08:42 PM
Stripping whitespace in a find command kj6loh Linux - General 22 12-12-2009 06:13 PM
Stripping lines versus stripping bytes in a bash subshell. poorman_installer Programming 9 10-21-2009 08:36 AM
Redirecting text from a file into a command simplified Linux - Newbie 5 07-13-2009 09:12 AM
bash command redirecting output to file freeindy Programming 10 11-08-2007 05:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration