swaping sections of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting swaping sections of lines
# 1  
Old 10-05-2008
swaping sections of lines

Is it possible to do this with sed?

Go from this:
958211 XXYYXXYYXX file1 file2 file3
958897 XXYYXXYYXX file1 file2

To this:
file1 file2 file3 XXYYXXYYXX 958211
file1 file2 XXYYXXYYXX 958897

The only thing consistant is XXYYXXYYXX. The numbers and files vary in length. I have seen I can reverse letters but this is a different story. If not sed any other thoughts? If I knew enough I could write a C program, but I do not.
# 2  
Old 10-05-2008

Code:
sed 's/\(.*\) \(XXYYXXYYXX\) \(.*\)/\3 \2 \1/'

# 3  
Old 10-05-2008
One more in awk,

Code:
awk '{for(i=NF;--i>0;) if($i=="XXYYXXYYXX"){printf $i FS} else{ printf $i FS} print ""}' file

# 4  
Old 10-05-2008
with awk,
Code:
awk -v FS="XXYYXXYYXX" '{print $2,FS,$1}' file

# 5  
Old 10-05-2008
Another way without a pattern, to move the 1st column to the end and the 2nd column before the 1st column:

Code:
sed 's/\([^ ]*\) \([^ ]*\) \(.*\)/\3 \2 \1/' file

Regards
# 6  
Old 10-05-2008
Wow, you all are amazing. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing sections from listener.ora

Hi, I am trying to write a script or command to add/remove a section from listener.ora file in the following example I would like: 1. to remove sid_alias2 sections 2. to add a new alias sections called sid_alias4, it can be placed after sid_alias3 sections $ cat listener.ora ... (18 Replies)
Discussion started by: ynixon
18 Replies

2. Shell Programming and Scripting

Extract certain sections of a line

I have a log that looks like below sc.mng_10_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE : Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323141427349 dvcsfx: 1 sc.mng_4_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE : Failed to change dvc_trx_sts from PE to BE for srvtrx:... (6 Replies)
Discussion started by: senormarquez
6 Replies

3. Shell Programming and Scripting

How to remove sections of a filename?

Hello, I need some help with renaming some files by removing a certain portion of the filename. The current file name is: ABC_2013186197_20130708_203556.95336 I need to remove the 5 digits after the first "_". The new file name should be: ABC_197_20130708_203556.95336 I'm not quite... (5 Replies)
Discussion started by: bbbngowc
5 Replies

4. Post Here to Contact Site Administrators and Moderators

Complete Code, or just Problematic Sections??

In general, would most experts, moderators and programmers, like to see the original entire script (even if very long), or just a simplified version of the problematic parts? I currently have a GNU bash function (with a lot of issues). Is it more advisable to make separate posts for each... (3 Replies)
Discussion started by: AlphaLexman
3 Replies

5. Programming

extract different sections of a file

Hi All, I have a file with the data 10;20;30;40;50;60;70;80;123;145;156;345. the output i want is the first fourth sixth elements and everything from there on. How do i achieve this. (1 Reply)
Discussion started by: raghu_shekar
1 Replies

6. Shell Programming and Scripting

How to edit file sections that cross multiple lines?

Hello, I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex. ... (8 Replies)
Discussion started by: Narnie
8 Replies

7. Shell Programming and Scripting

Removing sections

I have a file like this %( PHASES P %) %( SOURCES (10,0.0) (13,0.0) (16,0.0) (19,0.0) (22,0.0) (25,0.0) (28,0.0) (31,0.0) (34,0.0) (37,0.0) (40,0.0) (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

Modify sections of the line in a file

Hello.. I have a line in a file which I have to edit: the line looks like: <!]> Sometimes, the section of the line can have only one entry for cn, or maybe more than 2 like below: <!]> I have a variable which has the following value: CN="(cn=MNO)(cn=XYZ)" I need to replace the part... (4 Replies)
Discussion started by: chiru_h
4 Replies

9. Shell Programming and Scripting

swaping of first 2 words in every line using sed

Please any one can help me how to swap first 2 words in every line in a file using sed Thankk you (4 Replies)
Discussion started by: web123
4 Replies

10. UNIX for Dummies Questions & Answers

Number Swaping.... in file

I have a file having entries like 1270 (about 55000 entries like this). i want to swap these numbers like 2107... Any Idea (3 Replies)
Discussion started by: muneebr
3 Replies
Login or Register to Ask a Question