Trying to move a column into another position within a sed script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to move a column into another position within a sed script
# 1  
Old 04-08-2013
Trying to move a column into another position within a sed script

Currently the table looks like this

Code:
student-id,last,first,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2
pts-avail,,,100,150,100,200,100,150,300
991-78-7872,Thompson,Ken,95,143,79,185,95,135,259
123-45-6789,Richie,Dennis,99,123,89,189,97,139,279
234-56-7891,Aho,Al,78,146,75,176,88,128,285
456-78-9123,Weinberger,Peter,98,149,99,199,99,149,100
345-67-8912,Kernighan,Brian,88,144,88,188,99,111,299
567-89-1234,Joy,Bill,99,150,100,200,23,150,300
678-91-2345,Bourne,Steve,75,98,43,103,17,120,123
444-33-3456,Korn,Dave,100,143,78,123,54,140,234

This is what i am trying to do

Code:
student-id,first,last,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2
pts-avail,,,100,150,100,200,100,150,300
991-78-7872,Ken,Thompson,95,143,79,185,95,135,259
123-45-6789,Dennis,Richie,99,123,89,189,97,139,279
234-56-7891,Al,Aho,78,146,75,176,88,128,285
456-78-9123,Peter,Weinberger,98,149,99,199,99,149,100
345-67-8912,Brian,Kernighan,88,144,88,188,99,111,299
567-89-1234,Bill,Joy,99,150,100,200,23,150,300
678-91-2345,Steve,Bourne,75,98,43,103,17,120,123
444-33-3456,Dave,Korn,100,143,78,123,54,140,234

I am trying to do this within one sed shell script and have tried (not all of these are changing the whole table just a few columns just to try and see what works/doesn't)
s/^\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)\([^,]*,\)$/\1\3\2\4\5\6\7\8\9\0/g

s@^\(..\),\(..\),\(..*\),\(...\)@\1,\3,\2,\4@g

s/^\(...\),\(...\),\(...*\)/\1,\3,\2/

s/^\([^,]\),\([^,]\)\([^,]\)\([^...]\)/\1,\3,\2,\4/g

s/^\([^...]\)\([^...]\)\([^.]\),\(...\)/\1,\3\2\4/g

s/^[...],\(...\),\(...\),\(...\)/\2,\1,\3/g

is there a way to do this using the substitute or insert function of sed?

---------- Post updated at 05:17 PM ---------- Previous update was at 05:07 PM ----------

figured it out. thanks to anyone who happened to look at this the answer is

Code:
s/^\([^,]*\),\([^,]*\),\([^,]*\),\(.*\)$/\1,\3,\2,\4/


Last edited by jim mcnamara; 04-09-2013 at 12:19 AM..
This User Gave Thanks to ertang For This Post:
# 2  
Old 04-09-2013
Similarly..
Code:
 s/\([^,]*\,)\([^,]*\,)\([^,]*\,)/\1\3\2/ inputfile

# 3  
Old 04-09-2013
@michaelrozarl7: keyboard jamming?
Should be
Code:
s/\([^,]*,\)\([^,]*,\)\([^,]*,\)/\1\3\2/

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 04-09-2013
Quote:
Originally Posted by MadeInGermany
@michaelrozarl7: keyboard jamming?
Should be
Code:
s/\([^,]*,\)\([^,]*,\)\([^,]*,\)/\1\3\2/

Yes, my mistake.
Code:
sed 's/\([^,]*,\)\([^,]*,\)\([^,]*,\)/\1\3\2/' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column. Sample data: a|c b|d c|a d|b e|e f|g g|f |h |i Expected Output: a|c|1 b|d|2 c|a|3 d|b|4 (6 Replies)
Discussion started by: BrahmaNaiduA
6 Replies

2. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

4. Shell Programming and Scripting

URGENT!!!move character x of input string to some position

i have this prob I have some records in a file1 like this 1001 sajal singh tampa 1002 .... so on i have a pattern file which is like this 1,4 4,13 14,15 i have to read the first pair 1,4 and extract that from the first record so the pattern is 1001 now i have to scramble 1001... (1 Reply)
Discussion started by: spankincubus
1 Replies

5. Shell Programming and Scripting

Help to move leading negative sign to trailing position

Hi All, I am having a file which contains negative numbers, wht i am doing is re-formattting the file(moving few columns and add few hard codings between), while reformatting i would want the negative numbers to have the sign as trailing rather than leading. Existing -2400.00 34 0.00... (11 Replies)
Discussion started by: selvankj
11 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

8. Shell Programming and Scripting

move to a particular position and line

Hi, How can i move to particular line and to a particular position in a file using unix commands ? eg: in the line 30 and position 10 i want to print my name in a file. Cheers, Mohan (1 Reply)
Discussion started by: mohanpadamata
1 Replies

9. Shell Programming and Scripting

How to add a column numbers at a particular position?

Problem discription: I have many files which contain the same lines. for instance, (15 lines) file1 ..last column add by hand arbitrarily. 1.78116800 0.68396600 0.00061900 0.47641600 -0.49794500 -0.00024000 -1.70662800 0.29577100 0.67863600 -1.70647600 0.29654600 ... (9 Replies)
Discussion started by: liuzhencc
9 Replies

10. Shell Programming and Scripting

File Manipulation (Move Column Position)

Hi All, I have a comma separated value (.CSV) file like the one below. The file contains about 20000 lines. FileName EmpNo,Name,Age,Sex,Band,Spouse,Children,Salary, 1000,Arnold,24,M,B,N.A.,No,10000, 1001,Jenny,27,F,C,John,2,20000, ................................... What i need is to... (1 Reply)
Discussion started by: ultimate
1 Replies
Login or Register to Ask a Question