Changing file with rows and columns into just 1 column

Hi I need a bash shell script that will take text files with 4 rows and different numbers of columns in each row and convert each one into a text file with just one column.

I then subtract 1.5 from each number in the column (I have that part already)

The next step after that is I want to have the script run a loop through the 1 column file, replacing each number in the original text file with the converted number from the 1-column file.

Appreciate it!

tr -s ' ' '\012' < filename > newfile

Thanks awesome. For the next step, how would I make a loop that would go through each row, saving the integer in that row to a variable, and replacing its counterpart in the original file with 4 rows and different numbers of columns?

Is there a command that reads through a file one word at a time like in MATLAB?

Thanks!

It's not clear what you want.
Please give an example of the input and the expected output.

What's MATLAB? What does it have to do with shell scripting?

there are 2 inputs. One file contains the integers I want in 1 column, the other one is ordered into 4 rows with different numbers of columns (I need to switch the numbers from the single-column file into their respective counterparts in the 4-row different-number-of-columns file)

one-column file input:
4.5454
6.46643
9.23423
15.42362
141.43535
151.35235
193.353

multiple column file input:

6.5432 8.54353 14.3535 16.34324
21.5235 45.4363 75.454

output would look like:

4.5454 6.46643 9.23423 15.42362
141.43535 151.35235 193.353

(same column/row structure as the 2nd file but with the numbers from the 1st file)

paste -d" "  - - - - < file

What would this do? if the file that has the proper column/row structure is called "original.1D" and the one-column file that has the proper integers is called "onecolumn.1D" then how would this code be written? which input is "file"?

Thanks

---------- Post updated at 02:34 PM ---------- Previous update was at 02:20 PM ----------

What's MATLAB? What does it have to do with shell scripting?
[/quote]

What I meant is that I know in the programming language Matlab when it reads a text file it reads it one word at a time and the word that its on can be used as a variable and has an assigned name. Then theres another command that goes to the next word of the file so that it can go through each word in a file, do something with it, and move onto the next word. Thats what I want to do.

I want to go through the onecolumn file integer by integer (while at the same time going through the other file integer by integer) and switch the onecolumn integer into the integer of the multicolumn file one word at a time.

awk 'NR==FNR{A[++i]=$1;j=1;next}{for(i=1;i<=NF;i++)printf A[j++]" ";print ""}' file1 file2
4.5454 6.46643 9.23423 15.42362
141.43535 151.35235 193.353

Hmmm when I used that I got just a lot of blank space.

I just thought of another way I can do it...

For the one-column file, the numbers follow this sort of scheme:

1.242
2.2424
3.42412
4.2424
1.242
2.242
3.424

needs to be:
1.242 2.2424 3.42412 4.2424
1.242 2.242 3.424

(I need to create a new line wherever the number below the previous number is less than the one above). How would I go about doing this?

  1. Maybe I should first convert the rows into columns
  2. Have it read 2 fields at a time and compares them, making a new line where the succeeding number is less than the preceeding one.

If you are on Solaris, use /usr/xpg4/bin/awk or nawk instead of awk.

Worked perfectly, thanks!!!!