awk: printing newline with last column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: printing newline with last column
# 1  
Old 09-11-2012
awk: printing newline with last column

I was trying to simplify this from what I'm actually doing, but I started getting even more confused so I gave up. Here is the content of my input file:

Code:
Academic year,Term,Course name,Period,Last name,Nickname
2012-2013,First Semester,English 12,7th Period,Davis,Lucille

When I do this:

Code:
awk 'BEGIN { FS = "," } NR>1{print $6,$5}' CLASSES.csv

I get this output:

Code:
 Davise

Note the whitespace before "Davise."
I expect to see this
Code:
Lucille Davis

assume it has something to do with the newline that is part of the last column, because if I change the data to:
Code:
Academic year,Term,Course name,Period,Last name,Nickname
2012-2013,First Semester,English 12,7th Period,Davis,Lucy,0

It works as expected. What am I doing wrong?
# 2  
Old 09-11-2012
I can only assume there's something weird about your input data which doesn't post because it works perfectly here.

Perhaps your data is full of carriage returns? That can do weird things when printed to a console, because the carriage return would return the cursor to the beginning of the line.

That would fit actually. It prints 'lucille\r', returning the cursor to the beginning of the line, then prints a space, then prints 'Davis\n', which with the space is one character short of overwriting 'lucille'.

So the lesson here is, stop editing your input files in Microsoft Notepad Smilie
# 3  
Old 09-11-2012
It's probably not a newline but a carriage return. Try running
Code:
dos2unix CLASSES.csv

to fix your csv file.
# 4  
Old 09-11-2012
Ah, that makes sense. This file was output in excel format by a Windows app called Blackbaud and then exported to CSV from excel. I'll try it. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove newline character from column spread over multiple lines in a file

Hi, I came across one issue recently where output from one of the columns of the table from where i am creating input file has newline characters hence, record in the file is spread over multiple lines. Fields in the file are separated by pipe (|) delimiter. As header will never have newline... (4 Replies)
Discussion started by: Prathmesh
4 Replies

2. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

3. UNIX for Dummies Questions & Answers

Entering a newline into a header in awk

I want to create a header with awk like this: gawk 'BEGIN {print "List of Events"} Desired output: List of Events Tennis Baseball But I am at a loss on how to do this. I can make a list like this: List of Events Tennis Baseball But I can't get a space to appear. I have... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

awk delete newline after other replacements

Dear All, could you please help me to remove \n characters after all other replacements have been done as in the code below: { #remove punctuation and starting whitespaces gsub("]"," "); $1=$1; } { #print lines containing 'whatever' if ($1=="whatever") {print} #print... (3 Replies)
Discussion started by: shivacoder
3 Replies

5. Shell Programming and Scripting

Column printing in awk

Experts, i have a following file containing data in following manner. 1 2480434.4 885618.6 0.00 1948.00 40.00 1952.00 ... (6 Replies)
Discussion started by: Amit.saini333
6 Replies

6. Shell Programming and Scripting

Printing another column using awk and input data

Hi, I have data of the following type, chr1 234 678 39 852 638 abcd 7895 chr1 526 326 33 887 965 kilj 5849 Now, I would like to have something like this chr1 234 678 39 852 638 abcd 7895 <a href="http://unix.com/thread=chr1:234-678">Link</a> chr1 526 326 33 887 965 kilj 5849 <a... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

7. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

8. UNIX for Dummies Questions & Answers

creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving. I would like to convert the following file: human pool1_12 10e-02 45 67 human pool1_1899 10e-01 45 29 human pool1_1829 10e-01 43 26 horse pool1_343 10e-20 65 191 horse pool1_454 10e-09 44 43... (5 Replies)
Discussion started by: viralnerd
5 Replies

9. Shell Programming and Scripting

Printing 1st column to lower case using awk

I want to print the 1st field in a comma seperated file to lower case and the rest the case they are. I tried this nawk -F"," '{print tolower($0)}' OFS="," file this converts whole line in to lower case i just want the first column to be converted. The below doesnt work because in... (11 Replies)
Discussion started by: pinnacle
11 Replies

10. Shell Programming and Scripting

Awk not printing the last combined column

nawk -F "|" 'FNR==NR {a=$2 OFS $3 OFS $4 OFS $5 OFS $6;next}\ {if ($5 in a)print $1,"test",$5,a, $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 ; \ else print $1,"Database",$5 OFS OFS OFS OFS OFS OFS $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 }' OFS="|" \ file1 file2 > file3 This... (5 Replies)
Discussion started by: pinnacle
5 Replies
Login or Register to Ask a Question