|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
sed problem
I am a newbie in unix scripting so sorry if this is a dumb question. I am trying to reverse the order of first and last names if a file using sed. Here is a sample line:
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200 Please help have no idea how to start this. |
|
#2
|
|||
|
|||
|
and the output should be ??????
|
|
#3
|
|||
|
|||
|
Sorry the out put should be the first and last name reversed everything else stays the same.
|
|
#4
|
|||
|
|||
|
Code:
cat /var/tmp/asb.txt
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
sed -e 's/ /+/' /var/tmp/asb.txt
Tommy+Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
sed -e 's/ /+/' -e 's/:/+/' /var/tmp/asb.txt
Tommy+Savage+408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
sed -e 's/ /+/' -e 's/:/+/' /var/tmp/asb.txt |awk -F"+" '{print $2" "$1":"$3}'
Savage Tommy:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
HTH ![]() |
|
#5
|
|||
|
|||
|
Sorry I should have specified it should be just one sed command. No using awk.
|
|
#6
|
|||
|
|||
|
To use sed we have to pattern exactness there. We have to change the pattern with replacement there.
In your example, Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200 we knew Tommy Savage, Oxbow Court are to be changed there. But relative pattern to idenitfy them are not there. For example, if we have + symbol there then we can do sed there with identification of + there. But the symbol + must not be with other strings there. So simply to do this, sed "s/Tommy Savage/Savage Tommy/g;s/Oxbow Court/Court Oxbow/g" <inputfile> HTH. |
|
#7
|
|||
|
|||
|
miry2j
if you try to change: aaa bbb:ccccc and more to bbb aaa:ccccc and more then note you have 2 points the space between 'b a' and the : between 'a:c' IF THEY ARE CONSTANT, the way in sed is: s/^\(.*\)\( [ ]*\)\(.*\):\(.*\)/\3\2\1:\4/ NOTE the spaces. Muthukumar_K: try to be a little more abstract ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > sed problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|