Help swapping columns with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help swapping columns with AWK
# 1  
Old 06-05-2012
Help swapping columns with AWK

Hi!

Im trying to swap 2 columns in a file.The file format is:

Code:
'ColumnA','ColumnB'
'A1','A2'
'B1','B2'
'C1','C2'

I tried to solve this using AWK, when I run this command:

Code:
awk  'BEGIN {FS=OFS=","} {temp=$1; $1=$2; $2=temp} {print}' InFile.csv >> Outfile.csv

What I get is this:

'
Code:
ColumnB'
,'ColumnA'
'A2'
,'A1'
'B2'
,'B1'
'C2'
,'C1'

My expected output should be:

Code:
'ColumnB','ColumnA'
'A2','A1'
'B2','B1'
'C2','C1'

Any help you could give me will be greatly appreciated.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags not only for code but also for terminal output or file listings to set them apart from text. Thank you.

Last edited by bakunin; 06-05-2012 at 10:02 PM..
# 2  
Old 06-05-2012
Run this before running your code:
Code:
tr '\r' '\n' < InFile.csv > InFile.csv.tmp; mv InFile.csv.tmp InFile.csv

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-05-2012
simple for awk to do this...

Code:
$ awk -F, '{print $2 "," $1}' InFile.csv 
'ColumnB','ColumnA'
'A2','A1'
'B2','B1'
'C2','C1'

This User Gave Thanks to ddreggors For This Post:
# 4  
Old 06-05-2012
ddreggors hast already given a valid solution, so i will just explain why yours didn't work:

Quote:
Originally Posted by RedSpyder
I tried to solve this using AWK, when I run this command:

Code:
awk  'BEGIN {FS=OFS=","} {temp=$1; $1=$2; $2=temp} {print}' InFile.csv >> Outfile.csv

This part: {temp=$1; $1=$2; $2=temp} will not work, because you can't assign values to "$1" or "$2". These are system variables with the first (second, ...) read field on the read line, so they are read-only for you. The first assignment works, because you can use $1 to assign another variable.

I hope this helps.

bakunin
# 5  
Old 06-06-2012
Quote:
Originally Posted by bakunin
ddreggors hast already given a valid solution, so i will just explain why yours didn't work:



This part: {temp=$1; $1=$2; $2=temp} will not work, because you can't assign values to "$1" or "$2". These are system variables with the first (second, ...) read field on the read line, so they are read-only for you. The first assignment works, because you can use $1 to assign another variable.

I hope this helps.

bakunin
Hi Bakunin,

I dont understand the highlighted part. can you explain more..

Code:
 
echo "A,B" | awk  'BEGIN {FS=OFS=","} {temp=$1; $1=$2; $2=temp}{print}'
B,A

# 6  
Old 06-06-2012
using ddreggors and bartus11 suggestion both got me the same output:


Code:
'ColumnB','ColumnA'
,
'A2','A1'
,
'B2','B1'
,
'C2','C1'

So I just used
Code:
grep -vE ^, test.csv >> test2.csv

After and voila. Got the output I needed.

A little hacky for such a simple thing but nothing else did work :/

Thanks everyone for your suggestions and fast response!

Last edited by RedSpyder; 06-06-2012 at 09:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Solaris

Swapping

Hi Guys I am using SPARC-T4 (chipid 0, clock 2998 MHz), SunOS 5.10 Generic_150400-38 sun4v. How do I see if the server was doing some swapping like yesterday? I had a java application error with java.lang.OutOfMemoryError, now I want to check if the server was not doing some swapping at... (4 Replies)
Discussion started by: Phuti
4 Replies

3. Shell Programming and Scripting

Swapping columns in specific blocks

Hi all, I hope all you guys have a great new year! I am trying to swap 2 columns in a specific block of a file. The file format is: Startpoint: in11 (input port) Endpoint: out421 (output port) Path Group: (none) Path Type: max Point ... (5 Replies)
Discussion started by: jypark22
5 Replies

4. Shell Programming and Scripting

Swapping fields

Hallo Team, This is the command that i am running : grep ",Call Forward Not Reachable" *2013* this is the output that i am getting (i did a head -10 but the files can be more than 1000) ... (8 Replies)
Discussion started by: kekanap
8 Replies

5. UNIX for Dummies Questions & Answers

Swapping the columns of a text file for a subset of rows

Hi, I'd like to swap the columns 1 and 2 of a space-delimited text file but only for the first 1000 rows. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

7. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies

8. SuSE

Swapping

Hello! Why does my SuSE GNU/Linux machine swap? I have a Gig of ram, currently 14MBs of free RAM, 724MB - buffers and caches... That is 685MB of cached RAM, then kernel really should'nt have to swap, It should release cached memory in my thinkin... It has only swaped 3MB's but still,... (3 Replies)
Discussion started by: Esaia
3 Replies

9. UNIX for Dummies Questions & Answers

how to get swapping info

Hi How can I determine if swapping is occuring on a server. Thanks, Leo (2 Replies)
Discussion started by: leo
2 Replies

10. Filesystems, Disks and Memory

Paging and Swapping

Hi Guys: Would like to know how to check system swapping and paging and some theory on how they function. I am an oracle dba and my environment is 8171 on AIX 433. We have a 1GB of RAM on the box and I am educating myself to see how much more SGA can be accommodated on the box and what are the... (2 Replies)
Discussion started by: ST2000
2 Replies
Login or Register to Ask a Question