LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Stripping out tabs in file before redirecting to command. (https://www.linuxquestions.org/questions/linux-newbie-8/stripping-out-tabs-in-file-before-redirecting-to-command-4175431677/)

smturner1 10-11-2012 09:27 AM

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

oneindelijk 10-11-2012 09:45 AM

use sed
Quote:

cat file1 | sed s/\t//g >> tempfile1

smturner1 10-11-2012 09:58 AM

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.

oneindelijk 10-11-2012 10:01 AM

maybe multitail is what you're looking for ?
I'm not sure it's capable of comparing though

grail 10-11-2012 10:16 AM

So is it just a case of putting it all together?
Code:

lpr < diff <(sed 's/\t//g' file1) <(sed 's/\t//g' file2)

smturner1 10-11-2012 11:20 AM

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

grail 10-11-2012 11:42 AM

I am curious how the '-' strips out tabs? Do you have any links explaining this?

smturner1 10-11-2012 12:12 PM

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

David the H. 10-13-2012 08:34 AM

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.

smturner1 10-14-2012 08:01 PM

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

David the H. 10-15-2012 01:24 AM

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.


All times are GMT -5. The time now is 05:52 AM.