Reading colums from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading colums from a text file
# 1  
Old 03-31-2014
Reading colums from a text file

Hi all,

I have a text file that has (4) columns. There are about 300 lines on this file. It is a plain text file. I am looking to write a simple script that will read each line from the file and generate another text file. The file looks something like this:

These are the columns:

StudentName Grade Class Teacher
StudentName2 Grade Class Teacher
...
...
...
StudentName300 Grade Class Teacher

What I need to do is extract the text from this file and re-arrange it into another file.

For example, this is how the new file would be arranged:

Teacher Class Grade StudentName

I can easily do it when a file has just a single line, like this:
-------------------
StudentName = `cat /fileName | awk '{print $1}'
Grade = `cat /fileName | awk '{print $2}'
Class = `cat /fileName | awk '{print $3}'
Teacher = `cat /fileName | awk '{print $4}'

echo "${Teacher} ${Class} ${Grade} ${StudentName}" >> /NewFile
-------------------

Where I am struggling is how would I do this for a file that has 300 lines instead of just one?

I am sure it is something simple, I just need to be pointed in the right direction.

Thanks in advance!
# 2  
Old 03-31-2014
Print the fields in reverse:
Code:
[user@host ~]$ cat file
a b c d
a b c d
a b c d
a b c d
[user@host ~]$ awk '{for (i=NF;i>=1;i--){printf "%s ",$i} {print""}}' file
d c b a
d c b a
d c b a
d c b a
[user@host ~]$

# 3  
Old 03-31-2014
Hint: How about using "one" awk command to read in all the records from the input file, print them in the required format and then output to new file?
# 4  
Old 03-31-2014
Hi all, thanks for the quick responses. I guess I should have mentioned that that I also intend for the script to be able to use each filed as a variable aside from the sorting of the columns. For example, the sorting of the columns will be used to print a report, however, I would also like the script to generate the following text at the bottom of the report:

"The student, ${StudentName} is failing ${Teacher}'s ${Class} with a grade of ${Grade}"

Of course I will add a mechanism to check if the grade is passing or failing, but that I know how to do, It is the assigning variable values from multiple lines that is giving me trouble.

Thanks again in advanced.
# 5  
Old 03-31-2014
You could do something like this to read the fields into a variable:
Code:
while read studentname grade class teacher
do
  echo "${teacher} ${class} ${grade} ${studentname}" >> /path/to/your/new_file
done </path/to/your/file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. Shell Programming and Scripting

Reading a value from another text file

I am having a text file best = 100 genre = 75 group = 53 . . and so on I need to read those values (100,75,53,...) from my shell script. I have to do the same function for all the variables. So in my script i used for loop to do the same. for { a=best b=100 } Video tutorial on... (3 Replies)
Discussion started by: kishorekumar87
3 Replies

4. Programming

reading a text file in c++

hello all , im trying to read a text file and display its contents. While i got the code running and the output was displayed perfectly for sometime , i started getting Abort(core dump) error . Am i missing something here ? im using HP-UX. #include <iostream.h> #include <fstream.h> #include... (1 Reply)
Discussion started by: vishy_85
1 Replies

5. Shell Programming and Scripting

Help with Reading Text from file

Hello, I am having a tough time reading data from an input text file. The input file has lines of data which is comma seperated. Each line represents a record/row, but unfortunately the data in the individual column/fields have spaces and `cat filename`is not returning entire line ( to read... (4 Replies)
Discussion started by: yajaykumar
4 Replies

6. UNIX for Dummies Questions & Answers

Help with reading text file

How can i have a while loop as follows while read inputline do <task> done < name_list and also store the values (delimited) on each line to temp variables so as to print them on screen as follows while read inputline do set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
Discussion started by: bilal05
1 Replies

7. Shell Programming and Scripting

Reading from Text file.....

Hi Im MZ.... please help me with my requirements..... Requirement: I have a text file named information.txt which contains information about Oracle Instances, I want to fetch data's from that text file and want to display an output using shell script. Explanation : i.e. when I execute that... (0 Replies)
Discussion started by: user__user3110
0 Replies

8. Shell Programming and Scripting

Reading text from a file

Guys, I am trying to read text from a file, into arrays. The format of the file is: @DATABASE femotest @PACKAGE_SPECS /usr/home/oracle92/sosa/scripts/test.pks /usr/home/oracle92/sosa/scripts/rep.pks @PACKAGE_BODIES ... (12 Replies)
Discussion started by: LiquidChild
12 Replies

9. Shell Programming and Scripting

[Shell] How make colums in text file ??

hi, i have little pb, i would like make a colums, but my server not recongize "\t" or i write wrong.... and iam little noobs and no know awk... #!/bin/ksh #---------------------------------------------------------------------------- # Fichiers : ctrl.sh et ctrl2005.txt ... (6 Replies)
Discussion started by: parola
6 Replies

10. Shell Programming and Scripting

reading text file

I have a text file with 50 munbers. I wanna read these numbers and append "@yahoo.com" and send emails to them using shell scripting.......... How do i read the inetegres from the text file. (1 Reply)
Discussion started by: jaan
1 Replies
Login or Register to Ask a Question