Interchange location of words in a line of csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interchange location of words in a line of csv file
# 1  
Old 08-03-2012
Interchange location of words in a line of csv file

Hi,
I have a file with some info as example below. Its a csv file and i want to interchange the location of the times and dates in the 3rd and 4th fields only. I am using SunOS and csh script and would like to incorporate sed or nawk in the script (or any that would be able to accomplish this). Another important thing, My file would contain upto 200k lines ++.
Appreciate the help.

original:
Code:
Random,Words,10:12:59 12-Aug-2011,11:13:59 12-Aug-2011,Separated,By,Comma,,0,
Words,Comma,08:12:14 15-Aug-2011,05:44:48 16-Aug-2011,Random,0,Separated,By,,
Extra,Nothing,05:18:54 19-Aug-2011,14:48:23 19-Aug-2011,0,5,Change,now,,
0,Full,23:42:14 29-Aug-2011,01:23:47 30-Aug-2011,Empty,None,Random,Enclosed,,

Expected:
Code:
Random,Words,12-Aug-2011 10:12:59,12-Aug-2011 11:13:59,Separated,By,Comma,,0,
Words,Comma,15-Aug-2011 08:12:14,16-Aug-2011 05:44:48,Random,0,Separated,By,,
Extra,Nothing,19-Aug-2011 05:18:54,19-Aug-2011 14:48:23,0,5,Change,now,,
0,Full,29-Aug-2011 23:42:14,30-Aug-2011 01:23:47,Empty,None,Random,Enclosed,,

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 08-03-2012 at 12:09 PM..
# 2  
Old 08-03-2012
How about
Code:
awk -F, '{split($3" "$4,d," ");$3=d[2]" "d[1];$4=d[4]" "d[3]}1' OFS=, infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 08-03-2012
Through Sed
Code:
sed 's/\([^,]*,[^,]*,\)\([^ ]*\) \([^,]*\),\([^ ]*\) \([^,]*\)/\1\3 \2,\5 \4/' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 08-03-2012
Quote:
Originally Posted by Chubler_XL
How about
Code:
awk -F, '{split($3" "$4,d," ");$3=d[2]" "d[1];$4=d[4]" "d[3]}1' OFS=, infile

--> works fine! Thank you!

Quote:
Originally Posted by michaelrozar17
Through Sed
Code:
sed 's/\([^,]*,[^,]*,\)\([^ ]*\) \([^,]*\),\([^ ]*\) \([^,]*\)/\1\3 \2,\5 \4/' inputfile


--> works fine as well! Thank you!

Now, what would be faster nawk or sed???
Many thanks guys!
# 5  
Old 08-03-2012
You could test and tell us Smilie Or try as
Code:
time awk -F, '{split($3" "$4 ... 
time sed 's/\([^,]*,[^,]* ...

# 6  
Old 08-03-2012
Quote:
Originally Posted by viper1503
Now, what would be faster nawk or sed?
This is a good question; unfortunately there's not a straight answer. Different systems have different implementations of awk and sed and sometimes the answer is sed, sometimes the answer is awk. And it depends so much on what you're doing with them, that the answers could easily be reversed for you.

I think the awk solution looks more elegant -- it's more obvious what it's doing. That's the one I'd pick. That's completely opinion though, and in the end it probably doesn't matter. Neither of them is going to be slow here -- 200,000 lines is how much? A handful of megabytes? They should chew through that easily enough.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

3. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

4. Shell Programming and Scripting

Honey, I broke awk! (duplicate line removal in 30M line 3.7GB csv file)

I have a script that builds a database ~30 million lines, ~3.7 GB .cvs file. After multiple optimzations It takes about 62 min to bring in and parse all the files and used to take 10 min to remove duplicates until I was requested to add another column. I am using the highly optimized awk code: awk... (34 Replies)
Discussion started by: Michael Stora
34 Replies

5. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

6. Red Hat

How to insert recursive words in a file on every line

Hi Guys, If someone can help me edit my script using sed or awk that will be great. I am trying to get a list of folders and their sizes upto 2 levels along with the week number by using following - (I am currently putting this information in a file and plan to run this script EVERY week) ... (1 Reply)
Discussion started by: rockf1bull
1 Replies

7. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

8. Shell Programming and Scripting

How can i delete some words in every line in a file

Hi, I need help to delete a few words in every line in my file. This is how the file look like: VDC DQ 14900098,,,,157426.06849776753,786693.2919373367 10273032,,,,157525.49445429695,776574.5546672409 VDC DG ,10273033,,3er55,,149565.57096061576,801778.9379555212 AS174 892562,,,,, ... (2 Replies)
Discussion started by: andy_s
2 Replies

9. Shell Programming and Scripting

Count words on each line in file using xargs

Hi, im having a problem with xargs, i want to cout word of each line in file, and i HAVE to use xargs, i tried: cat file | xargs wc -w .....that uses all words in file like name of files and passed then to wc so it worte wc :somewordformfile is not i afile or directory cat file | xargs -I{} wc... (3 Replies)
Discussion started by: Qwetek
3 Replies

10. UNIX for Dummies Questions & Answers

Listing words from a file on a Separate Line

Hi, I want to list all the words in my file on a separate line. I am using the bourne(sh)/bourne again shell(bash). Thanks, theA (2 Replies)
Discussion started by: Astudent
2 Replies
Login or Register to Ask a Question