Changing file with rows and columns into just 1 column | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Changing file with rows and columns into just 1 column

Shell Programming and Scripting


Tags
bash, column, loop, row, shell

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-16-2010
ac130pilot ac130pilot is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 16 February 2010, 4:01 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #2  
Old 02-16-2010
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by ac130pilot View Post
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.

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

Sponsored Links
    #3  
Old 02-16-2010
ac130pilot ac130pilot is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 16 February 2010, 4:01 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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!
    #4  
Old 02-16-2010
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by ac130pilot View Post
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?

It's not clear what you want.
Please give an example of the input and the expected output.
Quote:
Is there a command that reads through a file one word at a time like in MATLAB?

What's MATLAB? What does it have to do with shell scripting?
Sponsored Links
    #5  
Old 02-16-2010
ac130pilot ac130pilot is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 16 February 2010, 4:01 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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)
Sponsored Links
    #6  
Old 02-16-2010
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 8 October 2014, 1:35 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
paste -d" "  - - - - < file

Sponsored Links
    #7  
Old 02-16-2010
ac130pilot ac130pilot is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 16 February 2010, 4:01 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by anbu23 View Post
Code:
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
Convert Columns to Rows in a File chrismt Shell Programming and Scripting 3 09-18-2008 06:11 AM
How to changes rows to columns in a file oracle123 Shell Programming and Scripting 4 07-31-2008 04:38 AM
How to convert a single column into several rows and columns? ashton_smith UNIX for Dummies Questions & Answers 5 05-24-2008 04:44 PM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM



All times are GMT -4. The time now is 09:34 AM.