ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can it be done with awk or some other command?
Also, is it possible then do add another column in front of this one with numbers of the lines (for every previous column), like
1 a1
2 a2
. .
. .
. .
n an
1 b1
2 b2
. .
. .
. .
n bn
. .
. .
. .
1 f1
2 f2
. .
. .
. .
n fn
You could automate this a little by putting it into a script which works out how many columns are in the file, then repeats the above line until the columns are finished.
You can also do this within awk, you could make nested for loops using the NR and NF variables to go through and print all the columns. See: http://www.grymoire.com/Unix/Awk.html
where you have to substitute the fields in red with the actual values: the file name and the number of columns, respectively. It uses multiple process substitutions to feed the cat command. It assumes that columns in the original file are separated by space.
Thanks for the tip! It nicely brings them all into one column. But it adds only number 1 to each row, and not the line numbers 1,...,n (as I draw in my first post).
Can this be amended?
To colucix:
Thanks for the tip! But your command line just gives me a blank terminal window. Nothing else.
To H_TeXMeX_H:
Thanks for the tip! I have a little trouble applying NR and NF loops. I need more time to see if it works.
Uh, I'm sorry. Maybe it depends on what actually is the field separator in the original file.
The field separator is just 2 spaces. How does that influence the result?
Anyway, I used a mix of solution fromstuart_cherrington and H_TeXMeX_H and it worked just as I want it. The only thing I still lack is how to automatize several command with one loop. I tried with for loop, but failed. Here are the shell scripts I have.
This one works (there are only 6 columns in the original file, so it's not a problem):
#!/bin/sh
Hardly surprising. awk reads a file line by line and patterns / actions are performed on each line
So your second line will print the line number (NR) 5 times along with the corresponding field ($i) from that line
Hence why I have stored the results into an array to be delivered at the END of the script.
I also find it curious why you use 'cat' to pass the contents of a file to a program that reads the contents of a file???
Well of course this will affect the result seeing the delimeter being passed to cut is a single space!!
So did you bother to try my solution? It is number of columns independent.
Yes, I did. It's good, but it doesn't print out the line numbers as the first column. I tried adding the NF in printf, but it doesn't work.
Quote:
Originally Posted by grail
Hardly surprising. awk reads a file line by line and patterns / actions are performed on each line
So your second line will print the line number (NR) 5 times along with the corresponding field ($i) from that line
I understand the mistake...is there a way around it (using this script)? Maybe smth similar to yours?
Hence why I have stored the results into an array to be delivered at the END of the script.
Quote:
Originally Posted by grail
I also find it curious why you use 'cat' to pass the contents of a file to a program that reads the contents of a file???
I just copied what stuart_cherrington gave me, not thinking about it. Are you saying that it is unnecessary?
Also, a new issue arrised in the meanwhile. My data are of the format 2.33456D-05 and so on. I the end, I need them multiplied by a constant factor (decimal number 219474.6306726). But when I do that, it seems that awk doesn't understand the format of my data, and gives back incorrect results.
I know that one can ask for a certain format with printf, but how can I tell awk that the input data are of that format, so that it can read them correctly?
Also, a new issue arrised in the meanwhile. My data are of the format 2.33456D-05 and so on. I the end, I need them multiplied by a constant factor (decimal number 219474.6306726).
Awk works in double precision but it's not aware of the fortran D notation. You should first convert D to E. Following my previous suggestion (but it can be applied for all the others) you can try something like:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.