Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing file with awk 1

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

I am trying to alter a value in a file with awk.. I am almost there but hving some trouble..
Code:
LOCK=`cat config/comp.cfg | awk '($2==X || substr($1,2)==X) && $0!="" && substr($1,0,1)!="#" {
			
str=$5;
sub(/-/, "LOCKED", str)  <-- this line is supposed to work!

} END {print str}' X="$1"`
echo $LOCK returns the value "LOCKED" correctly but the file is not altered.. what am i missing here ?

TIA,
 
You may try something like this:
nawk -v X="$1" '
($2==X || substr($1,2)==X) && $0!="" && substr($1,0,1)!="#" { sub(/-/, "LOCKED", $5) }
{ print }
' config/comp.cfg > config/comp.$$ && mv config/comp.$$ config/comp.cfg

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

thnx.. its working but with a small problem.. (I dont hv nawk, so i substituted it with just awk and it works).

The problem is that its doing something like a trim and is destroying the format of the file (only the line which it alters)..

I have tab-spaces between entries and this is being removed in the line thats altered..

any suggestions ?
thnx
 
awk -v X="$1" '
BEGIN{OFS="\t"}
($2==X || substr($1,2)==X) && $0!="" && substr($1,0,1)!="#" { sub(/-/, "LOCKED", $5) }
{ print }
' config/comp.cfg > config/comp.$$ && mv config/comp.$$ config/comp.cfg

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
not quite there yet..

Is it possible alter the line referring to the line above or below ??

The file is structured something like:

ads mystructure outval 456 locked
ats another nval 333 -

I need to replace the "-" in the 2nd line with locked keeping the structure as it is.. The space between any 2 columns is not always the same..

If this is difficult, i can probably maintain the same number of tabspaces between the columns..

thnx again
 
Perhaps simply this:
awk -v X="$1" '
{buf=$0}
($2==X || substr($1,2)==X) && $0!="" && substr($1,0,1)!="#" { sub(/-/, "LOCKED", buf) }
{ print buf}
' config/comp.cfg > config/comp.$$ && mv config/comp.$$ config/comp.cfg

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
got another thing to work out...

typ comp cfg group status

ads mystructure outval 456 locked
ats another nval 333 -
ats another n33l 333 -
yts anotyuer nddl 333 locked

In the above structure I need to alter the status to "locked" for every record belonging to the group "333".

How can I achieve this ??

Thnx again,
 
Code:
$4 == "333" { $NF = "locked"}
1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
What exactly isn't clear?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I tried something like:

function lockGroup {

grpName=`echo $WHATIS | awk '{print $2}'`
echo $grpName // outputs 333

cat $EAI_HOME/config/components_v1.cfg | awk '
$4 == "333" { $NF = "locked"}'

but its not working.. btw, wouldnt the above try setting all fields as "locked" instead of only the 5th column ?

Thnx ,
 
You didn't copy the posting exactly and you dont' need 'cat':
Code:
awk '$4 == "333" { $NF = "locked"}[COLOR=red]1[/color]' $EAI_HOME/config/components_v1.cfg

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
thnx.. executing the command prints the fwg correctly:

ats another n33l 333 locked
yts anotyuer nddl 333 locked

but its not written to the file. Also, it remove the formatting..

TIA
 
Ah, the first problem is solved but still unable to keep the formatting..
 
awk -v g=333 '
{buf=$0}
$4==g && $5=="-" { sub(/-/, "LOCKED", buf) }
{ print buf}
' $EAI_HOME/config/components_v1.cfg > $EAI_HOME/config/components_v1.$$ &&
mv $EAI_HOME/config/components_v1.$$ $EAI_HOME/config/components_v1.cfg

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top