|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
How can I search and change an specific string in a file
Dear All, New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux As titled, I wonder if you can help to provide a solution to find and change an specific string in a file The file include a lots of data in following configuration but might be various in different case. Code:
c 1 0 0 0 c 2 0 0.99999997 0 c 3 0.99999997 0 0 c 4 0.99999997 0.99999997 0 c 5 0 -0.19999999 0 c 6 0.99999997 -0.19999999 0 e 1 2 1 0 e 2 4 2 0 e 3 3 4 0 e 4 5 6 2 e 5 1 5 0 e 6 6 3 0 e 7 3 1 0 r 1 silicon r 2 oxide t 1 1 1 3 2 2 -1024 4 t 2 1 3 4 2 -1024 1 -1024 t 3 2 1 5 6 -1022 4 -1024 t 4 2 1 6 3 -1024 1 3 What I want is to find the line which meets following criteria 1. Start with letter 't' 2. Having both string 3 and 4 in the middle three positions 3. Having string -1024 in the rear three positions Only one line should meet said criteria, e.g. Code:
t 2 1 3 4 2 -1024 1 -1024 (criteria are highlighted in red font) I want to find it and change one of the '-1024' into '-1022' e.g. Code:
t 2 1 3 4 2 -1024 1 -1022 (change are highlighted in blue font) Thank you and I look forward to your elegant advice.
Last edited by Corona688; 09-20-2012 at 11:12 AM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
try.. Code:
awk '{if($0~/^t/&&$4=="3"&&$5=="4"&&$NF=="-1024"){$NF="-1022";{print $0}}else{print $0}}' filename |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Do the lines to be changed always have 9 fields, or do we have to handle any line with an odd number (greater than or equal to 5) fields?
|
|
#4
|
|||
|
|||
|
Code:
awk -f a.awk infile where a.awk: Code:
/^t/ {
mf=(NF/2) + .5;
sub("[.].*", "", mf);
if ((($(mf-1) == 3 || $(mf) == 3 || $(mf+1) == 3) && ($(mf-1) == 4 || $(mf) == 4 || $(mf+1) == 4)) &&
($(NF-2) ~ /^[-]1024/ || $(NF-1) ~ /^[-]1024/ || $(NF) ~ /^[-]1024/)) {
for (i=NF; i > 0; i-- ) {
if ($(i) ~ /^[-]1024/ ) {
sub("-1024", "-1022", $(i));
break;
}
}
print $0;
}
} |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| search-word-print-specific-string | Jassz | Shell Programming and Scripting | 1 | 01-28-2011 03:19 AM |
| search a word and print specific string using awk | dragon.1431 | Shell Programming and Scripting | 7 | 09-03-2010 07:21 AM |
| Search a specific string | zooby | Shell Programming and Scripting | 10 | 11-22-2009 04:14 AM |
| Search for string between specific lines of code in vi | coolavi | UNIX for Dummies Questions & Answers | 8 | 07-19-2009 11:17 PM |
| Search all files for specific string | sureshy | UNIX for Dummies Questions & Answers | 4 | 03-06-2002 11:28 AM |
|
|