"Paste" output in Bash script not as expected...
Hi.. I have several files I am trying to 'paste' together via bash script.
Input file format: FileA.txt: HeaderA LineA1 LineA2 LineA3 LineA4 ... FileB.txt: HeaderB LineB1 LineB2 LineB3 LineB4 ... FileC.txt: HeaderC LineC1 LineC2 LineC3 LineC4 ... etc. What I want is the output file to look like this: output.txt: HeaderA HeaderB HeaderC ... LineA1 LineB1 LineC1 ... LineA2 LineB2 LineC2 ... LineA3 LineB3 LineC3 ... LineA4 LineB4 LineC4 ... ... ... ... So, this is my code: >paste FileA.txt FileB.txt FileC.txt ... > output.txt However, the result is coming out as this: output.txt: HeaderA HeaderB HeaderC ... LineA1 LineB1 LineC1 ... LineA2 LineB2 LineC2 ... LineA3 LineB3 LineC3 ... LineA4 LineB4 LineC4 ... I can't figure out why it's behaving this way.. Any ideas? Much appreciated, J |
I have no idea. I created three files exactly like the examples, you provided, and here's what I got:
Code:
:~> paste fileA.txt fileB.txt fileC.txt > output.txt |
This is quite curious, I didn't know of such command, I thought that in order to do that one would have to do some relatively complex script that would read one line of each file at a time, write to a file and go on proceeding this way.
And it worked for me as well. I have no clue on why it does not work for you, the only thing I can think, a remote possibility, regards some subtle differences on windows', linux' and mac's files, something that I think that don't even exist anymore. I don't remember details, but it used to be back in the day that you'd have to convert a windows' pure text (.txt) to linux format and vice versa, otherwise one would have no "new lines" and/or it would have extra new lines, depending on who wrote and who reads it. But I think these days are gone now, this would no longer be an issue. But I can't really tell. |
Quote:
Linux and Widnows still use different line endings. At least Mac OS uses LF newlines just like Linux since Mac OS X. |
Hi,
I tried to replicate your "error". This is the closest I got: Code:
$ cat fileA I also ran the command with windows files on Linux. The results were still correct. Is it possible that you are trying to 'paste' Linux files in Windows? Not sure, if Windows would handle Linux files correctly in this case. |
OP's post
Quote:
|
Quote:
|
Quote:
|
Thank you for the responses,
"With me it just interprets three dots as another file (and the OP probably just used that to omit more files, not as an actual command) and ends in error: "paste: ...: No such file or directory" That's correct, I used the ellipses to show more than just the three files. I actually have about 20 actual data files that go in this way. As to the environment I'm using, its Bash on a Windows XP machine. Sorry, should have included that in my original post. The problem seems to lie within the format of the data files; other 'dummy' files that I created work fine using this method. I'll have to investigate some more. |
If you have awk, you can write a paste replacement that supports any newline convention, removes leading (except on the first line) and trailing whitespace on each line, and ignores empty lines. Supply the file names separated by a pipe | at the beginning:
Code:
awk -v "files=file1|file2|fileN" -v "separator=\t" ' |
Well I would agree that paste is the tool for the job, but Nominal got me think (as usual), although I have assumed that printing will stop once the first file's data is consumed:
Code:
#!/usr/bin/awk -f Code:
./script.awk file*.txt |
Thank you for the work-around :)
To try to figure out what is going on, I took SL00b's advice and actually created the simplified version of the input files: Code:
fileA.txt Code:
>paste fileA.txt fileB.txt fileC.txt > test.txt Code:
HeaderA Code:
>paste -d* fileA.txt fileB.txt fileC.txt > test.txt Code:
HeaderA I then ran it again with the serial option: Code:
>paste -sd* fileA.txt fileB.txt fileC.txt > test.txt Code:
HeaderA |
I'd say something is goofy with the implementation of the paste command in an XP environment.
I read your post saying you were running it in Bash on XP, so I created the same three text files on my XP desktop, ran the paste command from a Cygwin client, and received the following: $ paste fileA.txt fileB.txt fileC.txt HeaderA HeaderC LineA1 LineC1 LineA2 LineC2 LineA3 LineC3 LineA4 LineC4 ... ... Same data, same command, different environment, and for some reason fileB.txt was randomly ignored. I double and triple-checked the files, and they're all the same. Since this simplified test doesn't work, that says "code problem" to me. |
in bash, you can perhaps do it like this:
Code:
#!/bin/bash or: Code:
until as a complete script: Code:
#!/bin/bash Code:
bash script.sh file1 file2 ... outputfile |
Hi,
Try dos2unix file*.txt before the paste. To control you can also use Code:
paste -d'-_' fileA.txt fileB.txt fileC.txt > test.txt Code:
HeaderA-HeaderB_HeaderC Just made the test unix2dos of my files and the result is : Code:
$ od -c test.txt |
All times are GMT -5. The time now is 07:26 PM. |