Switch string in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switch string in the file
# 1  
Old 11-07-2012
Switch string in the file

Dear all,
I have file input like this,

file input:

input.txt

Code:
ROUTE  0001,
                              (  10021)=X   71Y 1605,
      X 1486Y 2220,X 1720Y 2380,X 1962Y 2553,X 2112Y 2650,X 2289Y 2746,
      YF 5000,
                              (  10022)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (  10023)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE  0002,
                              (  10021)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (  10022)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (  10023)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE  0003,
                              (  10021)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (  10022)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (  10023)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,

.......
.......

I would like to switch ROUTE number with POINT (in the bracket).

Desired output;
output.txt

Code:
ROUTE  10021,
                              (   0001)=X   71Y 1605,
      X 1486Y 2220,X 1720Y 2380,X 1962Y 2553,X 2112Y 2650,X 2289Y 2746,
      YF 5000,
                              (   0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (   0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE  10022,
                              (   0001)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (   0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (   0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE  10023,
                              (   0001)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (   0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                              (   0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
..........
..........

Please help.
Thanks for advance.

Attila
# 2  
Old 11-07-2012
Hi

Code:
$ sed '/=X /{N;N;s/\n/#/g;}' file | awk '$0 !~ /^ROUTE/{print x, $0;next}{x=$0;}' | sort -t"(" -k2n,2 | \
> sed 's/ROUTE *\([0-9]*\)\(.*\)(  \([0-9]*\)\(.*\)/ROUTE \3\2(  \1\4/' | \
> awk  '$0 ~ x{sub(/^[^,]*,/,"");print;next}{match($0,/^[^,]*/,a);x=a[0];print;}' x="@@" | \
> sed '/ROUTE/s/,/\n/' | sed 's/#/\n/g'
ROUTE 10021
                               (  0001)=X   71Y 1605,
      X 1486Y 2220,X 1720Y 2380,X 1962Y 2553,X 2112Y 2650,X 2289Y 2746,
      YF 5000,
                               (  0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                               (  0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE 10022
                               (  0001)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                               (  0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                               (  0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
ROUTE 10023
                               (  0001)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                               (  0002)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,
                               (  0003)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      YF 5000,

Guru.
# 3  
Old 11-07-2012
thank you guruprasadpr,

but it will little different when it found string like this,

.....
.....

Code:
.........
.........
ROUTE    1002,
                              (  10021)=X   44Y 1584,
      X  410Y 1692,X  696Y 1732,X 1067Y 1898,X 1402Y 2068,X 1592Y 2161,
      X 1804Y 2298,X 1967Y 2382,X 2103Y 2461,X 2174Y 2491,X 5769Y 4991,
      YF 5000,
                              (  10022)=X   52Y 1612,
      X  419Y 1734,X  630Y 1804,X  939Y 1918,X 1195Y 2038,X 1376Y 2142,
      X 1512Y 2200,X 1640Y 2289,X 1834Y 2384,X 1976Y 2481,X 2165Y 2582,
      X 2253Y 2637,X 5766Y 4993,
      YF 5000,
                              (  10023)=X   71Y 1605,
      X  498Y 1771,X  860Y 1939,X 1036Y 2009,X 1160Y 2050,X 1292Y 2115,
      X 1486Y 2220,X 1720Y 2380,X 1962Y 2553,X 2112Y 2650,X 2289Y 2746,
      X 5772Y 4992,
      YF 5000,

............
............

Any idea?

Thanks for advance,

Attila
# 4  
Old 11-07-2012
Hi

Altering the first part of the command:

Code:
$ awk '/=X/{if(x)print x;x="";}{if(x)x=x"#"$0;else x=$0;}END{print x}' file | awk '$0 !~ /^ROUTE/{print x, $0;next}{x=$0;}' | sort -t"(" -k2n,2 | \
> sed 's/ROUTE *\([0-9]*\)\(.*\)(  \([0-9]*\)\(.*\)/ROUTE \3\2(  \1\4/' | \
> awk  '$0 ~ x{sub(/^[^,]*,/,"");print;next}{match($0,/^[^,]*/,a);x=a[0];print;}' x="@@" | \
> sed '/ROUTE/s/,/\n/' | sed 's/#/\n/g'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 11-07-2012
work fine, thank you Guru,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. Shell Programming and Scripting

Switch line in txt file

Hi I have problem with replace line in txt file , I have this string: 144185 DISK Piece qqr8ot6l_1_1 -- 144186 DISK Piece ukr8pf2e_1_1 -- 144187 DISK Piece ter8p9gc_1_1 -- 144188 DISK Piece 4er8qb84_1_1 and (8 Replies)
Discussion started by: primo102
8 Replies

3. Shell Programming and Scripting

Regular Expression - Switch Entire String

I would like to be able to use a regular expression to find and replace entire strings, but not replace if the string is a substring in a larger string. Example: $string = "ABC ABCDEF ABC ABCDEF ABC"; Something like - $string =~ s/ABC/XYZ/g; ->Desired: $string = "XYZ ABCDEF XYZ ABCDEF... (3 Replies)
Discussion started by: rjulich
3 Replies

4. Shell Programming and Scripting

Error while executing switch case for find and replace specific file. Help Me...

case "$inputs" in sapte) find /pools/home_unix/sapte/work/models/model -name "*.xml" exec rm -i {} \;; ckm1) find /pools/home_unix/ckm1/work/models/model -name "*.xml" exec rm -i {} \;; I am getting error like as below. ./menu1.sh: line 144: syntax error near unexpected token `)'... (4 Replies)
Discussion started by: lathigara
4 Replies

5. UNIX for Dummies Questions & Answers

Switch and replace columns in a file

HP-UX I have a fixed length file like this 9921190625797AE2560 20091001US20091001@@NEWSITE @@ 20091013001X X 01NEW00DNA00007081 @@ SPRINGFIELD @@ ... (2 Replies)
Discussion started by: rs1969
2 Replies

6. UNIX for Advanced & Expert Users

vimdiff jump to other file or switch windows

This took me quite awhile to find so I wanted to share this with other people. To switch windows in vimdiff or to navigate windows in vimdiff or to change windows in vimdiff try the following: The ":vertical" command can be inserted before another command that splits a window. ... (0 Replies)
Discussion started by: cokedude
0 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

what is the switch to let sed edit and save file

I remember there is a sed switch i can use to edit and save the file at the same time, but i cannot recall it at all. so instead of -> sed 's/A/B/' file > file-tmp -> mv file-tmp file what can i do to just let sed edit and save the "file" (4 Replies)
Discussion started by: fedora
4 Replies

9. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

10. Shell Programming and Scripting

Help to switch dates from a file

Hi. I need some assistance with a file who at the end i must import to EXCEL. The problem is that i have a file with this inside: Output: JOBID START TIME END TIME ELAPSED CPU ------------------------------------------------------------ AVERAGE: ... (2 Replies)
Discussion started by: osramos
2 Replies
Login or Register to Ask a Question