LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 10: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, 10:45 AM   #2
oneindelijk
LQ Newbie
 
Registered: Oct 2012
Location: Belgium
Distribution: Xubuntu
Posts: 29

Rep: Reputation: Disabled
use sed
Quote:
cat file1 | sed s/\t//g >> tempfile1
 
Old 10-11-2012, 10: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, 11:01 AM   #4
oneindelijk
LQ Newbie
 
Registered: Oct 2012
Location: Belgium
Distribution: Xubuntu
Posts: 29

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, 11:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 12:20 PM   #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, 12:42 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am curious how the '-' strips out tabs? Do you have any links explaining this?
 
Old 10-11-2012, 01: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, 09:34 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 09:36 AM. Reason: minor rewording
 
Old 10-14-2012, 09: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, 02:24 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


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


All times are GMT -5. The time now is 12:54 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